onInit(); } /** * @return void * @throws NotFindClassException * @throws ReflectionException */ public function onHandlerListener(): void { $this->on('Shutdown', $this->createHandler('shutdown')); $this->on('Start', $this->createHandler('start')); if ($this->setting['task_worker_num'] ?? 0 > 0) { $this->on('Finish', $this->createHandler('finish')); $this->on('Task', $this->createHandler('task')); } $this->onManager(); } /** * @throws \ReflectionException * @throws \Snowflake\Exception\NotFindClassException */ private function onManager() { $this->on('ManagerStart', $this->createHandler('managerStart')); $this->on('ManagerStop', $this->createHandler('managerStop')); $this->onWorker(); $this->onOther(); } /** * @throws \ReflectionException * @throws \Snowflake\Exception\NotFindClassException */ private function onWorker() { $this->on('WorkerStop', $this->createHandler('workerStop')); $this->on('WorkerExit', $this->createHandler('workerExit')); $this->on('WorkerStart', $this->createHandler('workerStart')); $this->on('WorkerError', $this->createHandler('workerError')); } /** * @throws \ReflectionException * @throws \Snowflake\Exception\NotFindClassException */ private function onOther() { $this->on('PipeMessage', $this->createHandler('pipeMessage')); $this->on('BeforeReload', $this->createHandler('BeforeReload')); $this->on('AfterReload', $this->createHandler('AfterReload')); } /** * @param $eventName * @return array * @throws NotFindClassException * @throws ReflectionException * @throws Exception */ protected function createHandler($eventName): array { $classPrefix = 'HttpServer\Events\On' . ucfirst($eventName); if (!class_exists($classPrefix)) { throw new Exception('class not found.'); } $class = Snowflake::createObject($classPrefix, [Snowflake::app()]); return [$class, 'onHandler']; } }