龙空技术网

基于swoole框架hyperf源码分析(2)

底层的仰望 49

前言:

眼前各位老铁们对“swoolefy”大致比较讲究,看官们都需要学习一些“swoolefy”的相关内容。那么小编在网络上汇集了一些有关“swoolefy””的相关资讯,希望看官们能喜欢,你们一起来了解一下吧!

php bin/hyperf.php start

来看看该命令做了什么在Hyperf\Server\Command类定义了该类的执行方法

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();    }

把配置文件的server.php,servers全部取出,启动相应的服务,第一个是直接new swoole,后面的通过addListener加入进来,同名的server名字后面的会覆盖前面的

foreach ($servers as $server) {            $name = $server->getName();            $type = $server->getType();            $host = $server->getHost();            $port = $server->getPort();            $sockType = $server->getSockType();            $callbacks = $server->getCallbacks();            if (! $this->server instanceof SwooleServer) {                $this->server = $this->makeServer($type, $host, $port, $config->getMode(), $sockType);                $callbacks = array_replace($this->defaultCallbacks(), $config->getCallbacks(), $callbacks);                $this->registerSwooleEvents($this->server, $callbacks, $name);                $this->server->set(array_replace($config->getSettings(), $server->getSettings()));                ServerManager::add($name, [$type, current($this->server->ports)]);                if (class_exists(BeforeMainServerStart::class)) {                    // Trigger BeforeMainServerStart event, this event only trigger once before main server start.                    $this->eventDispatcher->dispatch(new BeforeMainServerStart($this->server, $config->toArray()));                }            } else {                /** @var \Swoole\Server\Port $slaveServer */                $slaveServer = $this->server->addlistener($host, $port, $sockType);                if (! $slaveServer) {                    throw new \RuntimeException("Failed to listen server port [{$host}:{$port}]");                }                $server->getSettings() && $slaveServer->set($server->getSettings());                $this->registerSwooleEvents($slaveServer, $callbacks, $name);                ServerManager::add($name, [$type, $slaveServer]);            }            // Trigger beforeStart event.            if (isset($callbacks[SwooleEvent::ON_BEFORE_START])) {                [$class, $method] = $callbacks[SwooleEvent::ON_BEFORE_START];                if ($this->container->has($class)) {                    $this->container->get($class)->{$method}();                }            }            if (class_exists(BeforeServerStart::class)) {                // Trigger BeforeEventStart event.                $this->eventDispatcher->dispatch(new BeforeServerStart($name));            }        }

注册事件时,会给相应的事件处理方法加上路由和中间件

if ($class instanceof MiddlewareInitializerInterface) {                    $class->initCoreMiddleware($serverName);                } public function initCoreMiddleware(string $serverName): void    {        $this->serverName = $serverName;        //注册中间件        $this->coreMiddleware = $this->createCoreMiddleware();        //注册路由分发器        $this->routerDispatcher = $this->createDispatcher($serverName);        $config = $this->container->get(ConfigInterface::class);        $this->middlewares = $config->get('middlewares.' . $serverName, []);        $this->exceptionHandlers = $config->get('exceptions.handler.' . $serverName, $this->getDefaultExceptionHandler());    }

路由是用了nikic/fast-route路由组件下编重点介绍 aop实现

标签: #swoolefy