| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- <?php
- declare(strict_types=1);
- /**
- * This file is part of Hyperf.
- *
- * @link https://www.hyperf.io
- * @document https://hyperf.wiki
- * @contact group@hyperf.io
- * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
- */
- namespace Hyperf\Server\Command;
- use Hyperf\Contract\ConfigInterface;
- use Hyperf\Contract\StdoutLoggerInterface;
- use Hyperf\Engine\Coroutine;
- use Hyperf\Server\ServerFactory;
- use Hyperf\Support\Composer;
- use InvalidArgumentException;
- use Psr\Container\ContainerInterface;
- use Psr\EventDispatcher\EventDispatcherInterface;
- use Symfony\Component\Console\Command\Command;
- use Symfony\Component\Console\Input\InputInterface;
- use Symfony\Component\Console\Output\OutputInterface;
- use function Hyperf\Support\swoole_hook_flags;
- class StartServer extends Command
- {
- public function __construct(private ContainerInterface $container)
- {
- parent::__construct('start');
- $this->setDescription('Start hyperf servers.');
- }
- protected function execute(InputInterface $input, OutputInterface $output): int
- {
- $this->checkEnvironment($output);
- $serverFactory = $this->container->get(ServerFactory::class)
- ->setEventDispatcher($this->container->get(EventDispatcherInterface::class))
- ->setLogger($this->container->get(StdoutLoggerInterface::class));
- $serverConfig = $this->container->get(ConfigInterface::class)->get('server', []);
- if (! $serverConfig) {
- throw new InvalidArgumentException('At least one server should be defined.');
- }
- $serverFactory->configure($serverConfig);
- Coroutine::set(['hook_flags' => swoole_hook_flags()]);
- $serverFactory->start();
- return 0;
- }
- private function checkEnvironment(OutputInterface $output)
- {
- if (! extension_loaded('swoole') || ! Composer::hasPackage('hyperf/polyfill-coroutine')) {
- return;
- }
- /**
- * 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 function names must be disabled before the server starts, please set swoole.use_shortname='Off' in your php.ini.");
- exit(SIGTERM);
- }
- }
- }
|