StartServer.php 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\Server\Command;
  12. use Hyperf\Contract\ConfigInterface;
  13. use Hyperf\Contract\StdoutLoggerInterface;
  14. use Hyperf\Engine\Coroutine;
  15. use Hyperf\Server\ServerFactory;
  16. use Hyperf\Support\Composer;
  17. use InvalidArgumentException;
  18. use Psr\Container\ContainerInterface;
  19. use Psr\EventDispatcher\EventDispatcherInterface;
  20. use Symfony\Component\Console\Command\Command;
  21. use Symfony\Component\Console\Input\InputInterface;
  22. use Symfony\Component\Console\Output\OutputInterface;
  23. use function Hyperf\Support\swoole_hook_flags;
  24. class StartServer extends Command
  25. {
  26. public function __construct(private ContainerInterface $container)
  27. {
  28. parent::__construct('start');
  29. $this->setDescription('Start hyperf servers.');
  30. }
  31. protected function execute(InputInterface $input, OutputInterface $output): int
  32. {
  33. $this->checkEnvironment($output);
  34. $serverFactory = $this->container->get(ServerFactory::class)
  35. ->setEventDispatcher($this->container->get(EventDispatcherInterface::class))
  36. ->setLogger($this->container->get(StdoutLoggerInterface::class));
  37. $serverConfig = $this->container->get(ConfigInterface::class)->get('server', []);
  38. if (! $serverConfig) {
  39. throw new InvalidArgumentException('At least one server should be defined.');
  40. }
  41. $serverFactory->configure($serverConfig);
  42. Coroutine::set(['hook_flags' => swoole_hook_flags()]);
  43. $serverFactory->start();
  44. return 0;
  45. }
  46. private function checkEnvironment(OutputInterface $output)
  47. {
  48. if (! extension_loaded('swoole') || ! Composer::hasPackage('hyperf/polyfill-coroutine')) {
  49. return;
  50. }
  51. /**
  52. * swoole.use_shortname = true => string(1) "1" => enabled
  53. * swoole.use_shortname = "true" => string(1) "1" => enabled
  54. * swoole.use_shortname = on => string(1) "1" => enabled
  55. * swoole.use_shortname = On => string(1) "1" => enabled
  56. * swoole.use_shortname = "On" => string(2) "On" => enabled
  57. * swoole.use_shortname = "on" => string(2) "on" => enabled
  58. * swoole.use_shortname = 1 => string(1) "1" => enabled
  59. * swoole.use_shortname = "1" => string(1) "1" => enabled
  60. * swoole.use_shortname = 2 => string(1) "1" => enabled
  61. * swoole.use_shortname = false => string(0) "" => disabled
  62. * swoole.use_shortname = "false" => string(5) "false" => disabled
  63. * swoole.use_shortname = off => string(0) "" => disabled
  64. * swoole.use_shortname = Off => string(0) "" => disabled
  65. * swoole.use_shortname = "off" => string(3) "off" => disabled
  66. * swoole.use_shortname = "Off" => string(3) "Off" => disabled
  67. * swoole.use_shortname = 0 => string(1) "0" => disabled
  68. * swoole.use_shortname = "0" => string(1) "0" => disabled
  69. * swoole.use_shortname = 00 => string(2) "00" => disabled
  70. * swoole.use_shortname = "00" => string(2) "00" => disabled
  71. * swoole.use_shortname = "" => string(0) "" => disabled
  72. * swoole.use_shortname = " " => string(1) " " => disabled.
  73. */
  74. $useShortname = ini_get_all('swoole')['swoole.use_shortname']['local_value'];
  75. $useShortname = strtolower(trim(str_replace('0', '', $useShortname)));
  76. if (! in_array($useShortname, ['', 'off', 'false'], true)) {
  77. $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.");
  78. exit(SIGTERM);
  79. }
  80. }
  81. }