hyperf-box,一款直接把php应用打包为二进制的工具

admin 2023-05-15 09:13:57 1377

大家再第一次使用box的时候可能会比较懵,我这里简单说下

1.安装box,先wget下载,再放到自己喜欢的目录,最后设置环境变量,也就是在你的 profile文件里,添加一行内容“export PATH=$PATH:~/box”,如果你用了zsh,则直接在~/.zshrc 里添加即可。

# 编辑环境变量文件(也可以是 vi /etc/profile)
vi ~/.zshrc
# 在行末输入以下(其实任何行都行)
export PATH=$PATH:~/box
# wq 保存

2.source ~/.zshrc(如果你是zsh的话,如果是profile,就是source profile) 让你的环境变量生效,这时候可以尝试执行box命令

# 使环境变量生效
source ~/.zshrc
# 执行box
box
# 这里可能会报错,权限问题,执行下面命令,比如我的路径是~/box/box
chmod 755 ~/box/box
# 再次执行 box
➜  ~ box
Console Tool

Usage:
  command  [options]  [arguments]

Options:
  -h, --help            Display help for the given command. When no command is given display help for the list command
  -q, --quiet           Do not output any message
  -V, --version         Display this application version
      --ansi|--no-ansi  Force (or disable --no-ansi) ANSI output
  -n, --no-interaction  Do not ask any interactive question
  -v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Available commands:
  build               Build the application as a bin.
  build-prepare       Prepare the enviroments for build the box cli.
  build-self          Build the application as a bin.
  completion          Dump the shell completion script
  composer            The proxy command of `php composer`.
  config              Config of box.
  cs-fix              The proxy command of `php cs-fix`.
  debug               Debug.
  get                 Get the runtime or library into your project.
  help                Display help for a command
  hyperf              The hyperf proxy command.
  info                Dump the server info.
  list                List commands
  php                 The php proxy command.
  php-cs-fixer        The proxy command of `php php-cs-fixer`.
  phpstan             The proxy command of `php phpstan`.
  phpunit             The proxy command of `php phpunit`.
  pint                The proxy command of `php pint`.
  reverse-proxy       Start the reverse proxy server for upstreams.
  self-update         Upgrade box to the latest version.
  start               Start hyperf servers.
  version             Dump the version of box.
 describe
  describe:aspects    Describe the aspects.
  describe:listeners  Describe the events and listeners.
  describe:routes     Describe the routes information.
 gen
  gen:amqp-consumer   Create a new amqp consumer class
  gen:amqp-producer   Create a new amqp producer class
  gen:aspect          Create a new aspect class
  gen:command         Create a new command class
  gen:constant        Create a new constant class
  gen:controller      Create a new controller class
  gen:job             Create a new job class
  gen:kafka-consumer  Create a new kafka consumer class
  gen:listener        Create a new listener class
  gen:middleware      Create a new middleware class
  gen:nats-consumer   Create a new nats consumer class
  gen:nsq-consumer    Create a new nsq consumer class
  gen:process         Create a new process class
  gen:request         Create a new form request class
  gen:resource        create a new resource
 phar
  phar:build          Pack your project into a Phar package.
 vendor
  vendor:publish      Publish any publishable configs from vendor packages.

3.box安装好了,这时候就得安装环境了(php)

# 安装php 会下载 ~/.box/php8.1
box get php@8.1
# 安装 composer 会下载 ~/.box/composer.phar
box get composer
# 安装hyperf
box composer create-project hyperf/swow-skeleton
# 后面就是正常开发了,如果打包二进制,看下面的官方文档即可。

原文地址和文档:hyperf.wiki/3.0/#/zh-cn/eco/box?id=box-by-hyperf

Box 致力于帮助提升 PHP 应用程序的编程体验,尤其有助于 Hyperf 应用,管理 PHP 环境和相关依赖,同时提供将 PHP 应用程序打包为二进制程序的能力,还提供反向代理服务来管理和部署 Swoole/Swow 服务。

使用

安装 box

Mac
wget https://github.com/hyperf/box/releases/download/v0.5.5/box_x86_64_macos -O box
sudo mv ./box /usr/local/bin/box
sudo chmod 755 /usr/local/bin/box
// 确保 /usr/local/bin/box 在你的 $PATH 环境中,或者将 `box` 放到你想要的任意 $PATH 路径中
Linux x86_64
wget https://github.com/hyperf/box/releases/download/v0.5.5/box_x86_64_linux -O box
sudo mv ./box /usr/local/bin/box
sudo chmod 755 /usr/local/bin/box
// 确保 /usr/local/bin/box 在你的 $PATH 环境中,或者将 `box` 放到你想要的任意 $PATH 路径中
Linux aarch64

目前我们缺少 AARCH64 Github Actions Runner,所以无法及时构建 AARCH64 版本的 bin 文件。

wget https://github.com/hyperf/box/releases/download/v0.0.3/box_php8.1_aarch64_linux -O box
sudo mv ./box /usr/local/bin/box
sudo chmod 755 /usr/local/bin/box
// 确保 /usr/local/bin/box 在你的 $PATH 环境中,或者将 `box` 放到你想要的任意 $PATH 路径中
Windows
curl -o box.exe https://github.com/hyperf/box/releases/download/v0.5.5/box_x64_windows.exe
// 将 `box.exe` 放到你想要的任意 Path 环境变量路径中,同时 Windows 版本在执行时需要在命令行中使用 `box.exe` 而不是 `box`

初始化 Github Access Token

Box 需要一个 Github 访问令牌来请求 Github API,以检索包的版本。

  1. 创建 Github Access Tokenworkflow 范围需要勾选;
  2. 运行 box config set github.access-token <Your Token> 命令来设置您的 token;
  3. 玩得开心 ~

设置 Box Kernel

默认情况下,Box 由 Swow Kernel 提供支持,但是我们也提供了 Swoole Kernel,您可以通过 box config set kernel swoole 来切换为 Swoole Kernel,但是需要注意的是,Swoole Kernel 仅支持 PHP 8.1 版本,且不支持构建二进制程序功能和 Windows 系统环境。

// 设置为 Swow Kernel  [默认]
box config set kernel swow

// 设置为 Swoole Kernel (不支持 Windows)
box config set kernel swoole

命令

  • box get pkg@version从远程安装包,pkg是包名,version是包的版本,box get pkg表示安装最新版本的 pkg,例如, 运行 box get php@8.1 安装 PHP 8.1, 运行 box get composer 安装最新的 composer bin
  • box build-prepare  build  build-self 命令做好相关环境的准备
  • box build-self 构建 box bin 本身
  • box build <path> 将 Hyperf 应用程序构建成二进制文件
  • box self-update  box bin 更新至最新版本
  • box config list 输出 box 配置文件的所有内容
  • box config get <key> 从配置文件中按键检索值
  • box config set <key> <value>通过 key 设置 value 到配置文件中
  • box config unset <key>按 key 删除配置值
  • box config set-php-version <version>设置 box 的当前 PHP 版本,可用值:8.0 | 8.1
  • box config get-php-version <version>获取 box 的当前设置的 PHP 版本
  • box reverse-proxy -u <upsteamHost:upstreamPort> 启动一个反向代理 HTTP 服务器,用于将 HTTP 请求转发到指定的多个上游服务器
  • box php <argument> 通过当前 box 的 PHP 版本运行任何 PHP 命令
  • box composer <argument>通过当前 box 的 PHP 版本运行任何 Composer 命令,composer bin 的版本取决于最后执行的 get composer 命令
  • box php-cs-fixer <argument> 通过当前 box 的 PHP 版本运行任何 php-cs-fixer 命令,composer bin 的版本取决于最后执行的 get php-cs-fixer 命令
  • box cs-fix <argument> 通过当前 box 的 PHP 版本运行 php-cs-fixer fix 命令,composer bin 的版本取决于最后执行的 get php-cs-fixer 命令
  • box phpstan <argument> 通过当前 box 的 PHP 版本运行任何 phpstan 命令,composer bin 的版本取决于最后执行的 get phpstan 命令,此命令仅在 box v0.3.0 及以上的版本中可用
  • box pint <argument> 通过当前 box 的 PHP 版本运行任何 pint 命令,composer bin 的版本取决于最后执行的 get pint 命令,此命令仅在 box v0.3.0 及以上的版本中可用
  • box version 输出当前 box bin 的版本号

关于 Swow-Skeleton

希望体验 Box 完整功能的朋友,需要通过 Swow Kernel 来运行,因此您需要基于 hyperf/swow-skeleton 来运行您的项目,可通过 box composer create-project hyperf/swow-skeleton 命令来创建一个基于 Hyperf 3.0 版的 Swow 骨架项目。

可爱猫?Telegram电报群 https://t.me/ikeaimao

社区声明 1、本站提供的一切软件、教程和内容信息仅限用于学习和研究目的
2、本站资源为用户分享,如有侵权请邮件与我们联系处理敬请谅解!
3、本站信息来自网络,版权争议与本站无关。您必须在下载后的24小时之内,从您的电脑或手机中彻底删除上述内容
最新回复 (0)

您可以在 登录 or 注册 后,对此帖发表评论!

返回