This commit is contained in:
2021-09-02 11:13:11 +08:00
parent d605b4b218
commit 4e2bb41358
3 changed files with 249 additions and 224 deletions
+3 -3
View File
@@ -21,6 +21,7 @@
"ext-xml": "*", "ext-xml": "*",
"ext-curl": "*", "ext-curl": "*",
"ext-openssl": "*", "ext-openssl": "*",
"symfony/console": "^2.4 || ^3.0 || ^4.0",
"amphp/amp": "v1.2.2", "amphp/amp": "v1.2.2",
"psr/log": "1.*", "psr/log": "1.*",
"ext-sockets": "*", "ext-sockets": "*",
@@ -34,7 +35,8 @@
"psr/cache": "^3.0", "psr/cache": "^3.0",
"psr/http-client": "^1.0", "psr/http-client": "^1.0",
"psr/simple-cache": "^1.0", "psr/simple-cache": "^1.0",
"game-worker/kiri-event": "v1.0" "game-worker/kiri-event": "v1.0",
"linkorb/etcd-php": "^1.6"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {
@@ -49,7 +51,5 @@
"error.php", "error.php",
"function.php" "function.php"
] ]
},
"require-dev": {
} }
} }
+189 -176
View File
@@ -17,11 +17,9 @@ use Database\DatabasesProviders;
use Exception; use Exception;
use Http\Command; use Http\Command;
use Http\Context\Response; use Http\Context\Response;
use Http\Server;
use Http\ServerProviders; use Http\ServerProviders;
use Kiri\Abstracts\BaseApplication; use Kiri\Abstracts\BaseApplication;
use Kiri\Abstracts\Config; use Kiri\Abstracts\Config;
use Kiri\Abstracts\Input;
use Kiri\Abstracts\Kernel; use Kiri\Abstracts\Kernel;
use Kiri\Crontab\CrontabProviders; use Kiri\Crontab\CrontabProviders;
use Kiri\Exception\NotFindClassException; use Kiri\Exception\NotFindClassException;
@@ -31,6 +29,8 @@ use Server\ResponseInterface;
use stdClass; use stdClass;
use Swoole\Process; use Swoole\Process;
use Swoole\Timer; use Swoole\Timer;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Output\ConsoleOutput;
/** /**
* Class Init * Class Init
@@ -42,218 +42,231 @@ use Swoole\Timer;
class Application extends BaseApplication class Application extends BaseApplication
{ {
/** /**
* @var string * @var string
*/ */
public string $id = 'uniqueId'; public string $id = 'uniqueId';
public string $state = ''; public string $state = '';
/** @var array<array<Process>> */ /** @var array<array<Process>> */
private array $_process = []; private array $_process = [];
/** /**
* @throws NotFindClassException * @throws NotFindClassException
*/ */
public function init() public function init()
{ {
$this->import(ConsoleProviders::class); $this->import(ConsoleProviders::class);
$this->import(ServerProviders::class); $this->import(ServerProviders::class);
} }
/** /**
* @throws NotFindClassException * @throws NotFindClassException
*/ */
public function withDatabase() public function withDatabase()
{ {
$this->import(DatabasesProviders::class); $this->import(DatabasesProviders::class);
} }
/** /**
* @throws NotFindClassException * @throws NotFindClassException
*/ */
public function withCrontab() public function withCrontab()
{ {
$this->import(CrontabProviders::class); $this->import(CrontabProviders::class);
} }
/** /**
* @param string $class * @param string $class
* @param Process $process * @param Process $process
*/ */
public function addProcess(string $class, Process $process) public function addProcess(string $class, Process $process)
{ {
if (!isset($this->_process[$class])) { if (!isset($this->_process[$class])) {
$this->_process[$class] = []; $this->_process[$class] = [];
} }
$this->_process[$class][] = $process; $this->_process[$class][] = $process;
} }
/** /**
* @return Process[] * @return Process[]
*/ */
public function getProcess(): array public function getProcess(): array
{ {
return $this->_process; return $this->_process;
} }
/** /**
* @param string $class * @param string $class
* @return Process|null * @return Process|null
*/ */
public function getProcessName(string $class): ?Process public function getProcessName(string $class): ?Process
{ {
return $this->_process[$class] ?? null; return $this->_process[$class] ?? null;
} }
/** /**
* @throws NotFindClassException * @throws NotFindClassException
* @throws ReflectionException * @throws ReflectionException
* @throws Exception * @throws Exception
*/ */
public function withFileChangeListen() public function withFileChangeListen()
{ {
$manager = $this->getServer(); $manager = $this->getServer();
$manager->addProcess(FileChangeCustomProcess::class); $manager->addProcess(FileChangeCustomProcess::class);
enable_file_modification_listening(); enable_file_modification_listening();
} }
/** /**
* @param Closure|array $closure * @param Closure|array $closure
* @return $this * @return $this
* @throws Exception * @throws Exception
*/ */
public function middleware(Closure|array $closure): static public function middleware(Closure|array $closure): static
{ {
$this->getRouter()->setMiddleware($closure); $this->getRouter()->setMiddleware($closure);
return $this; return $this;
} }
/** /**
* @param bool $useTree * @param bool $useTree
* @return $this * @return $this
* @throws Exception * @throws Exception
*/ */
public function setUseTree(bool $useTree): static public function setUseTree(bool $useTree): static
{ {
$this->getRouter()->setUseTree($useTree); $this->getRouter()->setUseTree($useTree);
return $this; return $this;
} }
/** /**
* @param string $service * @param string $service
* @return $this * @return $this
* @throws * @throws
*/ */
public function import(string $service): static public function import(string $service): static
{ {
if (!class_exists($service)) { if (!class_exists($service)) {
throw new NotFindClassException($service); throw new NotFindClassException($service);
} }
$class = Kiri::getDi()->get($service); $class = Kiri::getDi()->get($service);
if (method_exists($class, 'onImport')) { if (method_exists($class, 'onImport')) {
$class->onImport($this); $class->onImport($this);
} }
return $this; return $this;
} }
/** /**
* @param Kernel $kernel * @param Kernel $kernel
* @return $this * @return $this
*/ */
public function commands(Kernel $kernel): static public function commands(Kernel $kernel): static
{ {
foreach ($kernel->getCommands() as $command) { foreach ($kernel->getCommands() as $command) {
$this->register($command); $this->register($command);
} }
return $this; return $this;
} }
/** /**
* @param string $command * @param string $command
* @throws * @throws
*/ */
public function register(string $command) public function register(string $command)
{ {
/** @var Console $abstracts */ /** @var Console $abstracts */
$abstracts = $this->get('console'); // $abstracts = $this->get('console');
$abstracts->register($command); // $abstracts->register($command);
}
$console = di(\Symfony\Component\Console\Application::class);
$console->add(di($command));
}
/** /**
* @param Input $argv * @param array $argv
* @return void * @return void
* @throws Exception * @throws Exception
*/ */
public function execute(Input $argv): void public function execute(array $argv): void
{ {
try { try {
$this->register(Runtime::class); $this->register(Runtime::class);
$manager = Kiri::app()->get('console'); $input = new ArgvInput($argv);
$class = $manager->setParameters($argv)->search();
$this->enableFileChange($class); $console = di(\Symfony\Component\Console\Application::class);
$command = $console->find($input->getFirstArgument());
$data = $this->getBuilder($manager->exec($class)); // /** @var Console $manager */
} catch (\Throwable $exception) { // $manager = Kiri::app()->get('console');
$data = $this->getBuilder(jTraceEx($exception)); // $class = $manager->setParameters($argv)->search();
} finally {
print_r($data); $command->run($input, $output = new ConsoleOutput());
Timer::clearAll();
} $this->enableFileChange($command);
}
$data = $output->getStream();
// $data = $this->getBuilder($manager->exec($class));
} catch (\Throwable $exception) {
$data = $this->getBuilder(jTraceEx($exception));
} finally {
print_r($data);
Timer::clearAll();
}
}
/** /**
* @throws NotFindClassException * @throws NotFindClassException
* @throws ReflectionException * @throws ReflectionException
*/ */
private function enableFileChange($class): void private function enableFileChange($class): void
{ {
if (!($class instanceof Command)) { if (!($class instanceof Command)) {
scan_directory(directory('app'), 'App'); scan_directory(directory('app'), 'App');
} }
} }
/** /**
* @param $data * @param $data
* @return Response|ResponseInterface * @return Response|ResponseInterface
* @throws NotFindClassException * @throws NotFindClassException
* @throws ReflectionException * @throws ReflectionException
* @throws Exception * @throws Exception
*/ */
private function getBuilder($data): Response|ResponseInterface private function getBuilder($data): Response|ResponseInterface
{ {
return di(Response::class)->getBuilder($data); return di(Response::class)->getBuilder($data);
} }
/** /**
* @param $className * @param $className
* @param null $abstracts * @param null $abstracts
* @return stdClass * @return stdClass
* @throws Exception * @throws Exception
*/ */
public function make($className, $abstracts = null): stdClass public function make($className, $abstracts = null): stdClass
{ {
return make($className, $abstracts); return make($className, $abstracts);
} }
} }
+57 -45
View File
@@ -9,73 +9,85 @@ use Exception;
use Kiri\Abstracts\Input; use Kiri\Abstracts\Input;
use Kiri\Events\EventProvider; use Kiri\Events\EventProvider;
use Kiri\Exception\ConfigException; use Kiri\Exception\ConfigException;
use Kiri\Exception\NotFindClassException;
use Kiri\Kiri; use Kiri\Kiri;
use ReflectionException;
use Server\Events\OnBeforeWorkerStart; use Server\Events\OnBeforeWorkerStart;
use Server\Events\OnWorkerStart;
use Server\Worker\OnServerWorker; use Server\Worker\OnServerWorker;
use Server\Worker\OnWorkerStart as WorkerDispatch; use Server\Worker\OnWorkerStart as WorkerDispatch;
use Server\Events\OnWorkerStart;
/** /**
* Class Command * Class Command
* @package Http * @package Http
*/ */
class Command extends \Console\Command class Command extends \Symfony\Component\Console\Command\Command
{ {
public string $command = 'sw:server'; public string $command = 'sw:server';
public string $description = 'server start|stop|reload|restart'; public string $description = 'server start|stop|reload|restart';
const ACTIONS = ['start', 'stop', 'restart']; const ACTIONS = ['start', 'stop', 'restart'];
/** /**
* @var \Kiri\Events\EventProvider * @var EventProvider
*/ */
#[Inject(EventProvider::class)] #[Inject(EventProvider::class)]
public EventProvider $eventProvider; public EventProvider $eventProvider;
/** /**
* @param Input $dtl *
* @return string */
* @throws Exception protected function configure()
* @throws ConfigException {
*/ $this->setName('swoole')
public function onHandler(Input $dtl): string ->setDescription('server start|stop|reload|restart');
{ }
$manager = Kiri::app()->getServer();
$manager->setDaemon($dtl->get('daemon', 0));
if (!in_array($dtl->get('action'), self::ACTIONS)) {
return 'I don\'t know what I want to do.';
}
if ($manager->isRunner() && $dtl->get('action') == 'start') {
return 'Service is running. Please use restart.';
}
$manager->shutdown();
if ($dtl->get('action') == 'stop') {
return 'shutdown success.';
}
return $this->generate_runtime_builder($manager);
}
/** /**
* @param $manager * @param Input $dtl
* @return mixed * @return string
* @throws \Kiri\Exception\NotFindClassException * @throws Exception
* @throws \ReflectionException * @throws ConfigException
*/ */
private function generate_runtime_builder($manager) public function onHandler(Input $dtl): string
{ {
exec(PHP_BINARY . ' ' . APP_PATH . 'kiri.php runtime:builder'); $manager = Kiri::app()->getServer();
$manager->setDaemon($dtl->get('daemon', 0));
if (!in_array($dtl->get('action'), self::ACTIONS)) {
return 'I don\'t know what I want to do.';
}
if ($manager->isRunner() && $dtl->get('action') == 'start') {
return 'Service is running. Please use restart.';
}
$manager->shutdown();
if ($dtl->get('action') == 'stop') {
return 'shutdown success.';
}
return $this->generate_runtime_builder($manager);
}
$this->eventProvider->on(OnBeforeWorkerStart::class, [di(OnServerWorker::class), 'setConfigure']);
$this->eventProvider->on(OnWorkerStart::class, [di(WorkerDispatch::class), 'dispatch']);
return $manager->start(); /**
} * @param $manager
* @return mixed
* @throws NotFindClassException
* @throws ReflectionException
*/
private function generate_runtime_builder($manager): mixed
{
exec(PHP_BINARY . ' ' . APP_PATH . 'kiri.php runtime:builder');
$this->eventProvider->on(OnBeforeWorkerStart::class, [di(OnServerWorker::class), 'setConfigure']);
$this->eventProvider->on(OnWorkerStart::class, [di(WorkerDispatch::class), 'dispatch']);
return $manager->start();
}
} }