Files
kiri-core/HttpServer/Service/Abstracts/Server.php
T

123 lines
2.9 KiB
PHP
Raw Normal View History

2020-09-02 11:38:47 +08:00
<?php
2020-10-29 18:17:25 +08:00
declare(strict_types=1);
2020-09-02 11:38:47 +08:00
namespace HttpServer\Service\Abstracts;
use Exception;
use ReflectionException;
2021-08-11 01:04:57 +08:00
use Kiri\Application;
use Kiri\Exception\NotFindClassException;
use Kiri\Kiri;
2020-09-02 11:38:47 +08:00
2021-07-11 03:57:25 +08:00
/**
* Trait Server
* @package HttpServer\Service\Abstracts
*/
2020-09-02 11:38:47 +08:00
trait Server
{
2021-07-11 03:57:25 +08:00
public ?Application $application = null;
/**
* Server constructor.
* @param $host
* @param null $port
* @param null $mode
* @param null $sock_type
*/
public function __construct($host, $port = null, $mode = null, $sock_type = null)
{
parent::__construct($host, $port, $mode, $sock_type);
}
/**
* @param array $settings
*/
public function set(array $settings)
{
parent::set($settings); // TODO: Change the autogenerated stub
$this->onInit();
}
/**
* @return void
* @throws NotFindClassException
* @throws ReflectionException
*/
public function onHandlerListener(): void
{
$this->on('Shutdown', $this->createHandler('shutdown'));
$this->on('Start', $this->createHandler('start'));
2021-07-12 17:22:57 +08:00
if (($this->setting['task_worker_num'] ?? 0) > 0) {
2021-07-11 03:57:25 +08:00
$this->on('Finish', $this->createHandler('finish'));
$this->on('Task', $this->createHandler('task'));
}
$this->onManager();
}
/**
2021-07-12 17:22:57 +08:00
* @throws ReflectionException
* @throws NotFindClassException
2021-07-11 03:57:25 +08:00
*/
private function onManager()
{
$this->on('ManagerStart', $this->createHandler('managerStart'));
$this->on('ManagerStop', $this->createHandler('managerStop'));
$this->onWorker();
$this->onOther();
}
/**
2021-07-12 17:22:57 +08:00
* @throws ReflectionException
* @throws NotFindClassException
2021-07-11 03:57:25 +08:00
*/
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'));
}
/**
2021-07-12 17:22:57 +08:00
* @throws ReflectionException
* @throws NotFindClassException
2021-07-11 03:57:25 +08:00
*/
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.');
}
2021-08-11 01:04:57 +08:00
$class = Kiri::createObject($classPrefix, [Kiri::app()]);
2021-07-11 03:57:25 +08:00
return [$class, 'onHandler'];
}
2020-09-02 11:38:47 +08:00
}