From 835a7b8dd5ee3662cca8b750e62f07b4db3b0cbf Mon Sep 17 00:00:00 2001 From: huangzhhui Date: Mon, 7 Oct 2019 01:46:04 +0800 Subject: [PATCH 1/2] Optimized the detect logical of swoole shortname --- src/server/src/Command/StartServer.php | 48 +++++++++++++++++++------- src/server/src/ConfigProvider.php | 4 +++ 2 files changed, 39 insertions(+), 13 deletions(-) diff --git a/src/server/src/Command/StartServer.php b/src/server/src/Command/StartServer.php index 5a3e2cb89b..ef4dc72a4d 100644 --- a/src/server/src/Command/StartServer.php +++ b/src/server/src/Command/StartServer.php @@ -12,20 +12,18 @@ namespace Hyperf\Server\Command; -use Hyperf\Command\Annotation\Command; use Hyperf\Contract\ConfigInterface; use Hyperf\Contract\StdoutLoggerInterface; use Hyperf\Server\ServerFactory; +use InvalidArgumentException; use Psr\Container\ContainerInterface; use Psr\EventDispatcher\EventDispatcherInterface; -use Symfony\Component\Console\Command\Command as SymfonyCommand; +use Swoole\Runtime; +use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; -/** - * @Command - */ -class StartServer extends SymfonyCommand +class StartServer extends Command { /** * @var ContainerInterface @@ -34,15 +32,14 @@ class StartServer extends SymfonyCommand public function __construct(ContainerInterface $container) { - parent::__construct('start'); $this->container = $container; - - $this->setDescription('Start swoole server.'); + parent::__construct('start'); + $this->setDescription('Start hyperf servers.'); } protected function execute(InputInterface $input, OutputInterface $output) { - \Swoole\Runtime::enableCoroutine(true, swoole_hook_flags()); + Runtime::enableCoroutine(true, swoole_hook_flags()); $this->checkEnvironment($output); @@ -52,7 +49,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $serverConfig = $this->container->get(ConfigInterface::class)->get('server', []); if (! $serverConfig) { - throw new \InvalidArgumentException('At least one server should be defined.'); + throw new InvalidArgumentException('At least one server should be defined.'); } $serverFactory->configure($serverConfig); @@ -61,8 +58,33 @@ protected function execute(InputInterface $input, OutputInterface $output) private function checkEnvironment(OutputInterface $output) { - if (ini_get_all('swoole')['swoole.use_shortname']['local_value'] !== 'Off') { - $output->writeln('ERROR Swoole short name have to disable before start server, please set swoole.use_shortname = \'Off\' into your php.ini.'); + /** + * swoole.use_shortname = true => string(1) "1" => enabled + * swoole.use_shortname = "true" => string(1) "1" => enabled + * swoole.use_shortname = on => string(1) "1" => enabled + * swoole.use_shortname = On => string(1) "1" => enabled + * swoole.use_shortname = "On" => string(2) "On" => enabled + * swoole.use_shortname = "on" => string(2) "on" => enabled + * swoole.use_shortname = 1 => string(1) "1" => enabled + * swoole.use_shortname = "1" => string(1) "1" => enabled + * swoole.use_shortname = 2 => string(1) "1" => enabled + * swoole.use_shortname = false => string(0) "" => disabled + * swoole.use_shortname = "false" => string(5) "false" => disabled + * swoole.use_shortname = off => string(0) "" => disabled + * swoole.use_shortname = Off => string(0) "" => disabled + * swoole.use_shortname = "off" => string(3) "off" => disabled + * swoole.use_shortname = "Off" => string(3) "Off" => disabled + * swoole.use_shortname = 0 => string(1) "0" => disabled + * swoole.use_shortname = "0" => string(1) "0" => disabled + * swoole.use_shortname = 00 => string(2) "00" => disabled + * swoole.use_shortname = "00" => string(2) "00" => disabled + * swoole.use_shortname = "" => string(0) "" => disabled + * swoole.use_shortname = " " => string(1) " " => disabled + */ + $useShortname = ini_get_all('swoole')['swoole.use_shortname']['local_value']; + $useShortname = strtolower(trim(str_replace('0', '', $useShortname))); + if (! in_array($useShortname, ['', 'off', 'false'], true)) { + $output->writeln('ERROR Swoole short name have to disable before start server, please set swoole.use_shortname = off into your php.ini.'); exit(0); } } diff --git a/src/server/src/ConfigProvider.php b/src/server/src/ConfigProvider.php index 6faa997f02..300053b732 100644 --- a/src/server/src/ConfigProvider.php +++ b/src/server/src/ConfigProvider.php @@ -12,6 +12,7 @@ namespace Hyperf\Server; +use Hyperf\Server\Command\StartServer; use Hyperf\Server\Listener\InitProcessTitleListener; use Swoole\Server as SwooleServer; @@ -26,6 +27,9 @@ public function __invoke(): array 'listeners' => [ InitProcessTitleListener::class, ], + 'commands' => [ + StartServer::class, + ], 'annotations' => [ 'scan' => [ 'paths' => [ From 2cbe581edf0fb937f34faa4338e66f1129a90a38 Mon Sep 17 00:00:00 2001 From: huangzhhui Date: Mon, 7 Oct 2019 01:47:20 +0800 Subject: [PATCH 2/2] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c3728a08a7..a6df8b5fe0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -84,6 +84,7 @@ Now: ## Optimized - [#644](https://github.com/hyperf-cloud/hyperf/pull/644) Optimized annotation scan process, separate to two scan parts `app` and `vendor`, greatly decrease the elapsed time. +- [#653](https://github.com/hyperf-cloud/hyperf/pull/653) Optimized the detect logical of swoole shortname. ## Fixed