123 lines
2.9 KiB
PHP
123 lines
2.9 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace HttpServer\Service\Abstracts;
|
|
|
|
|
|
use Exception;
|
|
use ReflectionException;
|
|
use Kiri\Application;
|
|
use Kiri\Exception\NotFindClassException;
|
|
use Kiri\Kiri;
|
|
|
|
|
|
/**
|
|
* Trait Server
|
|
* @package HttpServer\Service\Abstracts
|
|
*/
|
|
trait Server
|
|
{
|
|
|
|
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'));
|
|
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 NotFindClassException
|
|
*/
|
|
private function onManager()
|
|
{
|
|
$this->on('ManagerStart', $this->createHandler('managerStart'));
|
|
$this->on('ManagerStop', $this->createHandler('managerStop'));
|
|
|
|
$this->onWorker();
|
|
$this->onOther();
|
|
}
|
|
|
|
|
|
/**
|
|
* @throws ReflectionException
|
|
* @throws 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 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 = Kiri::createObject($classPrefix, [Kiri::app()]);
|
|
return [$class, 'onHandler'];
|
|
}
|
|
|
|
|
|
}
|