Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimized the detect logical of swoole shortname #653

Merged
merged 2 commits into from Oct 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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

Expand Down
48 changes: 35 additions & 13 deletions src/server/src/Command/StartServer.php
Expand Up @@ -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
Expand All @@ -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);

Expand All @@ -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);
Expand All @@ -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>ERROR</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>ERROR</error> Swoole short name have to disable before start server, please set swoole.use_shortname = off into your php.ini.');
exit(0);
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/server/src/ConfigProvider.php
Expand Up @@ -12,6 +12,7 @@

namespace Hyperf\Server;

use Hyperf\Server\Command\StartServer;
use Hyperf\Server\Listener\InitProcessTitleListener;
use Swoole\Server as SwooleServer;

Expand All @@ -26,6 +27,9 @@ public function __invoke(): array
'listeners' => [
InitProcessTitleListener::class,
],
'commands' => [
StartServer::class,
],
'annotations' => [
'scan' => [
'paths' => [
Expand Down