Skip to content

Commit baedfa2

Browse files
authored
Merge pull request #44 from huangzhhui/kernel-switch
Kernel switch, also support Swoole Kernel
2 parents 4d59cb8 + 2e236ed commit baedfa2

11 files changed

Lines changed: 87 additions & 76 deletions

README-CN.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@ Box 致力于帮助提升 Hyperf 应用程序的编程体验,管理 PHP 环境
66

77
## 目前还是早期实验版本,欢迎试玩 ~
88

9-
请注意 box 内的 `php` **仅支持 Swow**, 暂 **不支持** Swoole,故你的项目骨架应由 [hyperf/swow-skeleton](https://github.com/hyperf/swow-skeleton) 项目创建或其它 Swow 骨架创建。
10-
如果要使用 Swoole,请使用 `swoole-cli` 代替 `php` 命令,同时其他 PHP 代理命令将无效。
11-
提示:运行 `box get swoole-cli` 可获取最新版本的 swoole-cli。
12-
139
### 使用
1410

1511
#### 安装 box
@@ -50,6 +46,18 @@ Box 需要一个 Github 访问令牌来请求 Github API,以检索包的版本
5046
2. 运行 `box config set github.access-token <Your Token>` 命令来设置您的 token;
5147
3. 玩得开心 ~
5248

49+
#### 设置 Box Kernel
50+
51+
默认情况下,Box 由 Swow Kernel 提供支持,但是我们也提供了 Swoole Kernel,您可以通过 `box config set kernel swoole` 来切换为 Swoole Kernel,但是需要注意的是,Swoole Kernel 仅支持 PHP 8.1 版本,且不支持构建二进制程序功能。
52+
53+
```bash
54+
// 设置为 Swow Kernel [默认]
55+
box config set kernel swow
56+
57+
// 设置为 Swoole Kernel
58+
box config set kernel swoole
59+
```
60+
5361
### 命令
5462

5563
- `box get pkg@version`从远程安装包,`pkg`是包名,`version`是包的版本,`box get pkg`表示安装最新版本的 pkg,例如, 运行 `box get php@8.1` 安装 PHP 8.1, 运行 `box get composer` 安装最新的 composer bin

README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@ Box is committed to helping improve the programming experience of Hyperf applica
66

77
## This is still an early experimental version, have fun ~
88

9-
Please notice that the `php` which in box **ONLY** supports for **Swow**, but **NOT** Swoole, so your Hyperf application should created by [hyperf/swow-skeleton](https://github.com/hyperf/swow-skeleton) or other else swow skeleton.
10-
If you want to use Swoole, please use `swoole-cli` instead of `php` command, also the other PHP proxy commands are invalid.
11-
Tips: run `box get swoole-cli` to get the latest version swoole-cli..
12-
139
### Usage
1410

1511
#### Install box
@@ -50,6 +46,18 @@ Box needs a Github Access Token to request github api, to retrieve the versions
5046
2. Run `box config set github.access-token <Your Token>` to init the token.
5147
3. Have fun ~
5248

49+
#### Setting the Box Kernel
50+
51+
By default, Box is supported by Swow Kernel, but we also provide Swoole Kernel, you can switch to Swoole Kernel by `box config set kernel swoole`, but it should be noted that Swoole Kernel only supports PHP 8.1 version, and The Build Binaries feature is not supported.
52+
53+
```bash
54+
// set to Swow Kernel [default]
55+
box config set kernel swow
56+
57+
// set to Swoole Kernel
58+
box config set kernel swoole
59+
````
60+
5361
### Commands
5462

5563
- `box get pkg@version` to install the package from remote automatically, `pkg` is the package name, and `version` is the version of package, `box get pkg` means to install the latest version of pkg, for example, run `box get php@8.1` to install the PHP 8.1, run `box get composer` to install the latest composer bin

src/app/Command/AbstractCommand.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
namespace App\Command;
1414

1515
use App\Config;
16+
use Hyperf\Contract\StdoutLoggerInterface;
1617
use RuntimeException;
1718
use Hyperf\Di\Annotation\Inject;
1819
use Hyperf\Command\Command as HyperfCommand;
@@ -22,6 +23,9 @@ abstract class AbstractCommand extends HyperfCommand
2223
#[Inject]
2324
protected Config $config;
2425

26+
#[Inject]
27+
protected StdoutLoggerInterface $logger;
28+
2529
protected function liveCommand(string $command)
2630
{
2731
if ($this->isFunctionExists('passthru')) {

src/app/Command/AbstractPhpCallProxyCommand.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ public function handle()
3838
protected function buildBinCommand(): string
3939
{
4040
$path = $this->getRuntimePath();
41-
return $path . '/php' . $this->getCurrentPhpVersion() . ' ' . $path . '/' . $this->proxyBin;
41+
$kernel = strtolower($this->config->getConfig('kernel', 'swow'));
42+
$currentPhpVersion = $this->getCurrentPhpVersion();
43+
if ($kernel === 'swoole') {
44+
$bin = $path . '/swoole-cli';
45+
if ($currentPhpVersion < '8.1') {
46+
$this->logger->warning(sprintf('Current setting PHP version is %s, but the kernel is Swoole and Swoole only support 8.1, so the PHP version is forced to 8.1.', $currentPhpVersion));
47+
}
48+
} else {
49+
$bin = $path . '/php' . $currentPhpVersion;
50+
}
51+
return $bin . ' ' . $path . '/' . $this->proxyBin;
4252
}
4353
}

src/app/Command/BuildCommand.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ public function configure()
3232

3333
public function handle()
3434
{
35+
$kernel = strtolower($this->config->getConfig('kernel', 'swow'));
36+
if ($kernel === 'swoole') {
37+
$this->logger->error('The build command is not supported in Swoole kernel.');
38+
return;
39+
}
3540
$path = $this->input->getArgument('path');
3641
$binName = $this->input->getOption('name');
3742
$outputPath = $this->input->getOption('output');

src/app/Command/GetCommand.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,11 @@ public function handle()
6161
}
6262
if ($versions) {
6363
$versions = $this->downloadManager->versions($pkg, $options);
64-
$this->output->writeln($versions);
64+
if (empty($versions)) {
65+
$this->output->writeln('No versions found.');
66+
} else {
67+
$this->output->writeln($versions);
68+
}
6569
} else {
6670
$this->downloadManager->get($pkg, $version, $options);
6771
}

src/app/Command/HyperfCommand.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,18 @@ public function configure()
3838
public function handle()
3939
{
4040
$path = $this->config->getConfig('path.runtime', getenv('HOME') . '/.box');
41+
$kernel = strtolower($this->config->getConfig('kernel', 'swow'));
4142
$currentPhpVersion = $this->config->getConfig('versions.php', '8.1');
4243
$hyperfBin = $this->config->getConfig('hyperf.bin', './bin/hyperf.php');
43-
$bin = $path . '/php' . $currentPhpVersion . ' ' . $hyperfBin;
44+
if ($kernel === 'swoole') {
45+
$closeShortname = "-d swoole.use_shortname='Off'";
46+
$bin = $path . '/swoole-cli ' . $closeShortname . ' ' . $hyperfBin;
47+
if ($currentPhpVersion < '8.1') {
48+
$this->logger->warning(sprintf('Current setting PHP version is %s, but the kernel is Swoole and Swoole only support 8.1, so the PHP version is forced to 8.1.', $currentPhpVersion));
49+
}
50+
} else {
51+
$bin = $path . '/php' . $currentPhpVersion . ' ' . $hyperfBin;
52+
}
4453
$command = Str::replaceFirst('hyperf ', '', (string) $this->input);
4554
$fullCommand = sprintf('%s %s', $bin, $command);
4655
$this->liveCommand($fullCommand);

src/app/Command/PhpCommand.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,16 @@ public function configure()
3434
public function handle()
3535
{
3636
$path = $this->config->getConfig('path.runtime', getenv('HOME') . '/.box');
37+
$kernel = strtolower($this->config->getConfig('kernel', 'swow'));
3738
$currentPhpVersion = $this->config->getConfig('versions.php', '8.1');
38-
$bin = $path . '/php' . $currentPhpVersion;
39+
if ($kernel === 'swoole') {
40+
$bin = $path . '/swoole-cli';
41+
if ($currentPhpVersion < '8.1') {
42+
$this->logger->warning(sprintf('Current setting PHP version is %s, but the kernel is Swoole and Swoole only support 8.1, so the PHP version is forced to 8.1.', $currentPhpVersion));
43+
}
44+
} else {
45+
$bin = $path . '/php' . $currentPhpVersion;
46+
}
3947
$command = Str::replaceFirst('php ', '', (string) $this->input);
4048
$fullCommand = sprintf('%s %s', $bin, $command);
4149
$this->liveCommand($fullCommand);

src/app/Command/SwooleCliCommand.php

Lines changed: 0 additions & 42 deletions
This file was deleted.

src/app/DownloadHandler/AbstractDownloadHandler.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,19 @@
1212

1313
namespace App\DownloadHandler;
1414

15-
use App\Box;
1615
use App\Config;
1716
use App\Exception\BoxException;
1817
use App\Exception\NotSupportVersionsException;
1918
use App\GithubClient;
2019
use App\PkgDefinition\Definition;
2120
use App\PkgDefinitionManager;
2221
use GuzzleHttp\Client;
23-
use GuzzleHttp\TransferStats;
2422
use Hyperf\Context\Context;
2523
use Hyperf\Contract\StdoutLoggerInterface;
2624
use Hyperf\Di\Annotation\Inject;
2725
use Hyperf\Engine\Channel;
2826
use Hyperf\Engine\Coroutine;
2927
use Hyperf\Utils\Str;
30-
use Psr\Http\Message\ResponseInterface;
3128
use SplFileInfo;
3229
use Symfony\Component\Console\Helper\ProgressBar;
3330
use Symfony\Component\Console\Output\OutputInterface;

0 commit comments

Comments
 (0)