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-curl": "*",
"ext-openssl": "*",
"symfony/console": "^2.4 || ^3.0 || ^4.0",
"amphp/amp": "v1.2.2",
"psr/log": "1.*",
"ext-sockets": "*",
@@ -34,7 +35,8 @@
"psr/cache": "^3.0",
"psr/http-client": "^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": {
"psr-4": {
@@ -49,7 +51,5 @@
"error.php",
"function.php"
]
},
"require-dev": {
}
}
+189 -176
View File
@@ -17,11 +17,9 @@ use Database\DatabasesProviders;
use Exception;
use Http\Command;
use Http\Context\Response;
use Http\Server;
use Http\ServerProviders;
use Kiri\Abstracts\BaseApplication;
use Kiri\Abstracts\Config;
use Kiri\Abstracts\Input;
use Kiri\Abstracts\Kernel;
use Kiri\Crontab\CrontabProviders;
use Kiri\Exception\NotFindClassException;
@@ -31,6 +29,8 @@ use Server\ResponseInterface;
use stdClass;
use Swoole\Process;
use Swoole\Timer;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Output\ConsoleOutput;
/**
* Class Init
@@ -42,218 +42,231 @@ use Swoole\Timer;
class Application extends BaseApplication
{
/**
* @var string
*/
public string $id = 'uniqueId';
/**
* @var string
*/
public string $id = 'uniqueId';
public string $state = '';
public string $state = '';
/** @var array<array<Process>> */
private array $_process = [];
/** @var array<array<Process>> */
private array $_process = [];
/**
* @throws NotFindClassException
*/
public function init()
{
$this->import(ConsoleProviders::class);
$this->import(ServerProviders::class);
}
/**
* @throws NotFindClassException
*/
public function init()
{
$this->import(ConsoleProviders::class);
$this->import(ServerProviders::class);
}
/**
* @throws NotFindClassException
*/
public function withDatabase()
{
$this->import(DatabasesProviders::class);
}
/**
* @throws NotFindClassException
*/
public function withDatabase()
{
$this->import(DatabasesProviders::class);
}
/**
* @throws NotFindClassException
*/
public function withCrontab()
{
$this->import(CrontabProviders::class);
}
/**
* @throws NotFindClassException
*/
public function withCrontab()
{
$this->import(CrontabProviders::class);
}
/**
* @param string $class
* @param Process $process
*/
public function addProcess(string $class, Process $process)
{
if (!isset($this->_process[$class])) {
$this->_process[$class] = [];
}
$this->_process[$class][] = $process;
}
/**
* @param string $class
* @param Process $process
*/
public function addProcess(string $class, Process $process)
{
if (!isset($this->_process[$class])) {
$this->_process[$class] = [];
}
$this->_process[$class][] = $process;
}
/**
* @return Process[]
*/
public function getProcess(): array
{
return $this->_process;
}
/**
* @return Process[]
*/
public function getProcess(): array
{
return $this->_process;
}
/**
* @param string $class
* @return Process|null
*/
public function getProcessName(string $class): ?Process
{
return $this->_process[$class] ?? null;
}
/**
* @param string $class
* @return Process|null
*/
public function getProcessName(string $class): ?Process
{
return $this->_process[$class] ?? null;
}
/**
* @throws NotFindClassException
* @throws ReflectionException
* @throws Exception
*/
public function withFileChangeListen()
{
$manager = $this->getServer();
$manager->addProcess(FileChangeCustomProcess::class);
/**
* @throws NotFindClassException
* @throws ReflectionException
* @throws Exception
*/
public function withFileChangeListen()
{
$manager = $this->getServer();
$manager->addProcess(FileChangeCustomProcess::class);
enable_file_modification_listening();
}
enable_file_modification_listening();
}
/**
* @param Closure|array $closure
* @return $this
* @throws Exception
*/
public function middleware(Closure|array $closure): static
{
$this->getRouter()->setMiddleware($closure);
return $this;
}
/**
* @param Closure|array $closure
* @return $this
* @throws Exception
*/
public function middleware(Closure|array $closure): static
{
$this->getRouter()->setMiddleware($closure);
return $this;
}
/**
* @param bool $useTree
* @return $this
* @throws Exception
*/
public function setUseTree(bool $useTree): static
{
$this->getRouter()->setUseTree($useTree);
return $this;
}
/**
* @param bool $useTree
* @return $this
* @throws Exception
*/
public function setUseTree(bool $useTree): static
{
$this->getRouter()->setUseTree($useTree);
return $this;
}
/**
* @param string $service
* @return $this
* @throws
*/
public function import(string $service): static
{
if (!class_exists($service)) {
throw new NotFindClassException($service);
}
$class = Kiri::getDi()->get($service);
if (method_exists($class, 'onImport')) {
$class->onImport($this);
}
return $this;
}
/**
* @param string $service
* @return $this
* @throws
*/
public function import(string $service): static
{
if (!class_exists($service)) {
throw new NotFindClassException($service);
}
$class = Kiri::getDi()->get($service);
if (method_exists($class, 'onImport')) {
$class->onImport($this);
}
return $this;
}
/**
* @param Kernel $kernel
* @return $this
*/
public function commands(Kernel $kernel): static
{
foreach ($kernel->getCommands() as $command) {
$this->register($command);
}
return $this;
}
/**
* @param Kernel $kernel
* @return $this
*/
public function commands(Kernel $kernel): static
{
foreach ($kernel->getCommands() as $command) {
$this->register($command);
}
return $this;
}
/**
* @param string $command
* @throws
*/
public function register(string $command)
{
/** @var Console $abstracts */
$abstracts = $this->get('console');
$abstracts->register($command);
}
/**
* @param string $command
* @throws
*/
public function register(string $command)
{
/** @var Console $abstracts */
// $abstracts = $this->get('console');
// $abstracts->register($command);
$console = di(\Symfony\Component\Console\Application::class);
$console->add(di($command));
}
/**
* @param Input $argv
* @return void
* @throws Exception
*/
public function execute(Input $argv): void
{
try {
$this->register(Runtime::class);
/**
* @param array $argv
* @return void
* @throws Exception
*/
public function execute(array $argv): void
{
try {
$this->register(Runtime::class);
$manager = Kiri::app()->get('console');
$class = $manager->setParameters($argv)->search();
$input = new ArgvInput($argv);
$this->enableFileChange($class);
$console = di(\Symfony\Component\Console\Application::class);
$command = $console->find($input->getFirstArgument());
$data = $this->getBuilder($manager->exec($class));
} catch (\Throwable $exception) {
$data = $this->getBuilder(jTraceEx($exception));
} finally {
print_r($data);
Timer::clearAll();
}
}
// /** @var Console $manager */
// $manager = Kiri::app()->get('console');
// $class = $manager->setParameters($argv)->search();
$command->run($input, $output = new ConsoleOutput());
$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 ReflectionException
*/
private function enableFileChange($class): void
{
if (!($class instanceof Command)) {
scan_directory(directory('app'), 'App');
}
}
/**
* @throws NotFindClassException
* @throws ReflectionException
*/
private function enableFileChange($class): void
{
if (!($class instanceof Command)) {
scan_directory(directory('app'), 'App');
}
}
/**
* @param $data
* @return Response|ResponseInterface
* @throws NotFindClassException
* @throws ReflectionException
* @throws Exception
*/
private function getBuilder($data): Response|ResponseInterface
{
return di(Response::class)->getBuilder($data);
}
/**
* @param $data
* @return Response|ResponseInterface
* @throws NotFindClassException
* @throws ReflectionException
* @throws Exception
*/
private function getBuilder($data): Response|ResponseInterface
{
return di(Response::class)->getBuilder($data);
}
/**
* @param $className
* @param null $abstracts
* @return stdClass
* @throws Exception
*/
public function make($className, $abstracts = null): stdClass
{
return make($className, $abstracts);
}
/**
* @param $className
* @param null $abstracts
* @return stdClass
* @throws Exception
*/
public function make($className, $abstracts = null): stdClass
{
return make($className, $abstracts);
}
}
+57 -45
View File
@@ -9,73 +9,85 @@ use Exception;
use Kiri\Abstracts\Input;
use Kiri\Events\EventProvider;
use Kiri\Exception\ConfigException;
use Kiri\Exception\NotFindClassException;
use Kiri\Kiri;
use ReflectionException;
use Server\Events\OnBeforeWorkerStart;
use Server\Events\OnWorkerStart;
use Server\Worker\OnServerWorker;
use Server\Worker\OnWorkerStart as WorkerDispatch;
use Server\Events\OnWorkerStart;
/**
* Class Command
* @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
*/
#[Inject(EventProvider::class)]
public EventProvider $eventProvider;
/**
* @var EventProvider
*/
#[Inject(EventProvider::class)]
public EventProvider $eventProvider;
/**
* @param Input $dtl
* @return string
* @throws Exception
* @throws ConfigException
*/
public function onHandler(Input $dtl): string
{
$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);
}
/**
*
*/
protected function configure()
{
$this->setName('swoole')
->setDescription('server start|stop|reload|restart');
}
/**
* @param $manager
* @return mixed
* @throws \Kiri\Exception\NotFindClassException
* @throws \ReflectionException
*/
private function generate_runtime_builder($manager)
{
exec(PHP_BINARY . ' ' . APP_PATH . 'kiri.php runtime:builder');
/**
* @param Input $dtl
* @return string
* @throws Exception
* @throws ConfigException
*/
public function onHandler(Input $dtl): string
{
$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();
}
}