Skip to content

Commit

Permalink
Merge pull request #50 from huangzhhui/win-box
Browse files Browse the repository at this point in the history
Optimized
  • Loading branch information
huangzhhui authored Oct 11, 2022
2 parents 10ca55f + 49ac9dc commit f4a0365
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 39 deletions.
25 changes: 25 additions & 0 deletions src/app/Command/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace App\Command;

use App\Config;
use App\Exception\BoxException;
use Hyperf\Contract\StdoutLoggerInterface;
use RuntimeException;
use Hyperf\Di\Annotation\Inject;
Expand Down Expand Up @@ -70,4 +71,28 @@ protected function isFunctionExists(string|array $functions): bool
}
return $isExists;
}

protected function buildBinPath(): string
{
$path = $this->getRuntimePath();
$kernel = strtolower($this->config->getConfig('kernel', 'swow'));
$currentPhpVersion = $this->getCurrentPhpVersion();
$os = PHP_OS_FAMILY;
if ($kernel === 'swoole') {
if ($os === 'Windows') {
throw new BoxException('The command is not supported in Swoole kernel on Windows.');
}
if ($currentPhpVersion < '8.1') {
$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));
}
$bin = $path . DIRECTORY_SEPARATOR . 'swoole-cli';
} else {
$extension = '';
if ($os === 'Windows') {
$extension = '.exe';
}
$bin = $path . DIRECTORY_SEPARATOR . 'php' . $currentPhpVersion . $extension;
}
return $bin;
}
}
22 changes: 2 additions & 20 deletions src/app/Command/AbstractPhpCallProxyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,7 @@ public function handle()

protected function buildBinCommand(): string
{
$path = $this->getRuntimePath();
$kernel = strtolower($this->config->getConfig('kernel', 'swow'));
$currentPhpVersion = $this->getCurrentPhpVersion();
$os = PHP_OS_FAMILY;
if ($kernel === 'swoole') {
if ($os === 'Windows') {
throw new BoxException('Swoole kernel is not supported on Windows.');
}
if ($currentPhpVersion < '8.1') {
$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));
}
$bin = $path . DIRECTORY_SEPARATOR . 'swoole-cli';
} else {
$extension = '';
if ($os === 'Windows') {
$extension = '.exe';
}
$bin = $path . DIRECTORY_SEPARATOR . 'php' . $currentPhpVersion . $extension;
}
return $bin . ' ' . $path . DIRECTORY_SEPARATOR . $this->proxyBin;
$bin = $this->buildBinPath();
return $bin . ' ' . $this->getRuntimePath() . DIRECTORY_SEPARATOR . $this->proxyBin;
}
}
13 changes: 2 additions & 11 deletions src/app/Command/PhpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace App\Command;

use App\Exception\BoxException;
use Hyperf\Command\Annotation\Command;
use Hyperf\Utils\Str;
use Psr\Container\ContainerInterface;
Expand All @@ -33,17 +34,7 @@ public function configure()

public function handle()
{
$path = $this->config->getConfig('path.runtime', getenv('HOME') . '/.box');
$kernel = strtolower($this->config->getConfig('kernel', 'swow'));
$currentPhpVersion = $this->config->getConfig('versions.php', '8.1');
if ($kernel === 'swoole') {
$bin = $path . '/swoole-cli';
if ($currentPhpVersion < '8.1') {
$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));
}
} else {
$bin = $path . '/php' . $currentPhpVersion;
}
$bin = $this->buildBinPath();
$command = Str::replaceFirst('php ', '', (string) $this->input);
$fullCommand = sprintf('%s %s', $bin, $command);
$this->liveCommand($fullCommand);
Expand Down
1 change: 1 addition & 0 deletions src/app/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public function init(): void
'versions' => [
'php' => '8.1',
],
'kernel' => 'swow',
];
$this->setConfigContent($content);
}
Expand Down
14 changes: 6 additions & 8 deletions src/app/PkgDefinitionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,11 @@ class PkgDefinitionManager
/**
* All the definitions is array data, not Definition instance.
*/
protected array $pkgs = [];

public function __construct()
{
$this->fetchPkgs();
}
protected ?array $pkgs = null;

public function getDefinition(string $pkg): ?Definition
{
foreach ($this->pkgs as $name => $item) {
foreach ($this->getPkgs() as $name => $item) {
if ($name === $pkg) {
return new Definition($name, $item);
}
Expand All @@ -37,11 +32,14 @@ public function getDefinition(string $pkg): ?Definition

public function hasDefinition(string $pkg): bool
{
return isset($this->pkgs[$pkg]);
return isset($this->getPkgs()[$pkg]);
}

public function getPkgs(): array
{
if (is_null($this->pkgs)) {
$this->fetchPkgs();
}
return $this->pkgs;
}

Expand Down

0 comments on commit f4a0365

Please sign in to comment.