Hyperf 的指令管理預設由
symfony/console
提供支援 (如果您希望更換該元件您也可以通過改變 skeleton 的入口檔案更換成您希望使用的元件), 在執行
php bin/hyperf.php start
後,将由
Hyperf\Server\Command\StartServer
指令類接管,并根據配置檔案
config/autoload/server.php
内定義的 Server 逐個啟動
其實最開始的文檔已經寫清楚,隻是太着急沒時間看,到了後來才發現 衆裡尋他千百度,蓦然回首,那人卻在燈火闌珊處.
Hyperf\Server\Command\StartServer.php
public function __construct(ContainerInterface $container)
{
$this->container = $container;
parent::__construct('start');
$this->setDescription('Start hyperf servers.');
}
這裡是定義start的地方
protected function execute(InputInterface $input, OutputInterface $output)
{
$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);
Runtime::enableCoroutine(true, swoole_hook_flags());
$serverFactory->start();
return 0;
}
這裡運作的時候可以看到 $output的内容
object(Symfony\Component\Console\Output\ConsoleOutput)#77168 (5) {
["stderr":"Symfony\Component\Console\Output\ConsoleOutput":private]=>
object(Symfony\Component\Console\Output\StreamOutput)#76982 (3) {
["stream":"Symfony\Component\Console\Output\StreamOutput":private]=>
resource(3636) of type (stream)
["verbosity":"Symfony\Component\Console\Output\Output":private]=>
int(32)
["formatter":"Symfony\Component\Console\Output\Output":private]=>
object(Symfony\Component\Console\Formatter\OutputFormatter)#77677 (3) {
["decorated":"Symfony\Component\Console\Formatter\OutputFormatter":private]=>
bool(true)
["styles":"Symfony\Component\Console\Formatter\OutputFormatter":private]=>
array(4) {
["error"]=>
object(Symfony\Component\Console\Formatter\OutputFormatterStyle)#77063 (5) {
["foreground":"Symfony\Component\Console\Formatter\OutputFormatterStyle":private]=>
array(2) {
["set"]=>
int(37)
["unset"]=>
int(39)
}
["background":"Symfony\Component\Console\Formatter\OutputFormatterStyle":private]=>
array(2) {
["set"]=>
int(41)
["unset"]=>
int(49)
}
["href":"Symfony\Component\Console\Formatter\OutputFormatterStyle":private]=>
NULL
["options":"Symfony\Component\Console\Formatter\OutputFormatterStyle":private]=>
array(0) {
}
["handlesHrefGracefully":"Symfony\Component\Console\Formatter\OutputFormatterStyle":private]=>
NULL
}
["info"]=>
object(Symfony\Component\Console\Formatter\OutputFormatterStyle)#77102 (5) {
["foreground":"Symfony\Component\Console\Formatter\OutputFormatterStyle":private]=>
array(2) {
["set"]=>
int(32)
["unset"]=>
int(39)
}
["background":"Symfony\Component\Console\Formatter\OutputFormatterStyle":private]=>
NULL
["href":"Symfony\Component\Console\Formatter\OutputFormatterStyle":private]=>
NULL
["options":"Symfony\Component\Console\Formatter\OutputFormatterStyle":private]=>
array(0) {
}
["handlesHrefGracefully":"Symfony\Component\Console\Formatter\OutputFormatterStyle":private]=>
NULL
}
["comment"]=>
object(Symfony\Component\Console\Formatter\OutputFormatterStyle)#77462 (5) {
["foreground":"Symfony\Component\Console\Formatter\OutputFormatterStyle":private]=>
array(2) {
["set"]=>
int(33)
["unset"]=>
int(39)
}
["background":"Symfony\Component\Console\Formatter\OutputFormatterStyle":private]=>
NULL
["href":"Symfony\Component\Console\Formatter\OutputFormatterStyle":private]=>
NULL
["options":"Symfony\Component\Console\Formatter\OutputFormatterStyle":private]=>
array(0) {
}
["handlesHrefGracefully":"Symfony\Component\Console\Formatter\OutputFormatterStyle":private]=>
NULL
}
...
并且要求至少啟動一個server,由配置檔案 config/autoload/server.php 定義的server逐個啟動,例如主程式啟動使用9501,websocket啟動使用9502,Grpc啟動使用9503...這樣服務依次啟動,互不影響,友善使用
而且這個操作預設是在入口檔案config/container.php裡面實作了的,也就是上文講到的重點