Files
kiri-core/http-server/Server.php
T

138 lines
2.4 KiB
PHP
Raw Normal View History

2020-08-31 01:27:08 +08:00
<?php
2021-09-02 13:59:00 +08:00
namespace Server;
2020-08-31 01:27:08 +08:00
2021-08-17 18:38:38 +08:00
use Annotation\Inject;
2021-04-27 16:42:44 +08:00
use Exception;
2021-08-17 16:43:50 +08:00
use Http\Abstracts\HttpService;
2021-07-20 11:24:15 +08:00
use JetBrains\PhpStorm\Pure;
2021-08-11 01:04:57 +08:00
use Kiri\Abstracts\Config;
use Kiri\Error\LoggerProcess;
2021-08-17 18:38:38 +08:00
use Kiri\Events\EventDispatch;
2021-08-11 01:04:57 +08:00
use Kiri\Exception\ConfigException;
2021-09-06 14:20:04 +08:00
use Kiri\Rpc\RpcProvider;
2021-08-17 18:38:38 +08:00
use Server\Events\OnShutdown;
2020-08-31 01:27:08 +08:00
2021-03-01 15:09:36 +08:00
defined('PID_PATH') or define('PID_PATH', APP_PATH . 'storage/server.pid');
2020-08-31 01:27:08 +08:00
/**
* Class Server
2021-08-17 16:43:50 +08:00
* @package Http
2020-08-31 01:27:08 +08:00
*/
2021-02-20 17:30:45 +08:00
class Server extends HttpService
2020-08-31 01:27:08 +08:00
{
2021-03-26 01:05:07 +08:00
2021-07-21 15:37:33 +08:00
private array $process = [
LoggerProcess::class
];
2021-07-03 21:12:21 +08:00
2021-08-18 11:56:19 +08:00
/**
* @Inject ServerManager
* @var null|ServerManager
*/
#[Inject(ServerManager::class)]
2021-08-18 13:38:45 +08:00
public ?ServerManager $manager = null;
2021-08-18 11:56:19 +08:00
2021-08-12 15:39:33 +08:00
private mixed $daemon = 0;
2021-07-03 21:12:21 +08:00
2021-08-17 18:46:56 +08:00
/** @var EventDispatch */
2021-08-17 18:38:38 +08:00
#[Inject(EventDispatch::class)]
public EventDispatch $eventDispatch;
2021-07-21 15:37:33 +08:00
/**
*
*/
public function init()
{
}
2021-07-03 21:12:21 +08:00
2021-07-20 11:29:18 +08:00
/**
* @param $process
*/
2021-07-21 15:37:33 +08:00
public function addProcess($process)
{
$this->process[] = $process;
}
/**
* @return string start server
*
* start server
* @throws ConfigException
* @throws Exception
*/
public function start(): string
{
2021-08-17 16:38:58 +08:00
$this->manager->initBaseServer(Config::get('server', [], true), $this->daemon);
2021-07-21 15:37:33 +08:00
$rpcService = Config::get('rpc', []);
if (!empty($rpcService)) {
2021-09-06 14:20:04 +08:00
RpcProvider::addRpcListener($this->manager, $rpcService);
2021-07-21 15:37:33 +08:00
}
$processes = array_merge($this->process, Config::get('processes', []));
foreach ($processes as $process) {
$this->manager->addProcess($process);
}
return $this->manager->getServer()->start();
}
/**
* @return void
*
* start server
* @throws Exception
*/
public function shutdown()
{
2021-08-17 18:35:11 +08:00
$configs = Config::get('server', [], true);
2021-08-17 18:46:56 +08:00
foreach ($this->manager->sortService($configs['ports'] ?? []) as $config) {
2021-08-17 18:35:11 +08:00
$this->manager->stopServer($config['port']);
}
2021-08-17 18:38:38 +08:00
$this->eventDispatch->dispatch(new OnShutdown());
2021-08-17 18:35:11 +08:00
}
/**
* @return bool
* @throws ConfigException
*/
public function isRunner(): bool
{
return $this->manager->isRunner();
2021-07-21 15:37:33 +08:00
}
/**
* @param $daemon
* @return Server
*/
public function setDaemon($daemon): static
{
if (!in_array($daemon, [0, 1])) {
return $this;
}
$this->daemon = $daemon;
return $this;
}
/**
* @return \Swoole\Http\Server|\Swoole\Server|\Swoole\WebSocket\Server|null
*/
#[Pure] public function getServer(): \Swoole\Http\Server|\Swoole\Server|\Swoole\WebSocket\Server|null
{
return $this->manager->getServer();
}
2021-07-03 21:12:21 +08:00
2020-08-31 01:27:08 +08:00
}