diff --git a/Annotation/Event.php b/Annotation/Event.php index 2f9b4698..9a146c66 100644 --- a/Annotation/Event.php +++ b/Annotation/Event.php @@ -5,9 +5,7 @@ namespace Annotation; use Exception; -use Kiri\Exception\ComponentException; -use Kiri\Kiri; -use Kiri\Event as SEvent; +use Kiri\Events\EventProvider; /** @@ -34,10 +32,10 @@ use Kiri\Event as SEvent; * @return bool * @throws Exception */ - public function execute(mixed $class, mixed $method = null): bool - { - // TODO: Implement execute() method. - SEvent::on($this->name, [$class, $method]); + public function execute(mixed $class, mixed $method = null): bool + { + $pro = di(EventProvider::class); + $pro->on($this->name, [$class, $method]); return true; } diff --git a/Annotation/LocalService.php b/Annotation/LocalService.php index 35dd3c86..b65d5c4e 100644 --- a/Annotation/LocalService.php +++ b/Annotation/LocalService.php @@ -6,7 +6,9 @@ namespace Annotation; use Exception; use Kiri\Event; +use Kiri\Events\EventProvider; use Kiri\Kiri; +use Server\Events\OnWorkerExit; /** * Class LocalService @@ -28,9 +30,10 @@ use Kiri\Kiri; if ($this->async_reload !== true) { return; } - Event::on(Event::SERVER_WORKER_EXIT, function () { - Kiri::app()->remove($this->service); - }); + $pro = di(EventProvider::class); + $pro->on(OnWorkerExit::class, function () { + di(\Kiri\Di\LocalService::class)->remove($this->service); + },0); } diff --git a/Console/AbstractConsole.php b/Console/AbstractConsole.php index 7c4a744f..4ae32dca 100644 --- a/Console/AbstractConsole.php +++ b/Console/AbstractConsole.php @@ -9,6 +9,8 @@ use Kiri\Abstracts\Component; use Kiri\Abstracts\Input; use Kiri\Event; use Kiri\Kiri; +use Server\Events\OnAfterCommandExecute; +use Server\Events\OnBeforeCommandExecute; /** * Class AbstractConsole @@ -58,11 +60,11 @@ abstract class AbstractConsole extends Component */ public function execCommand(Command $command): mixed { - fire(Event::BEFORE_COMMAND_EXECUTE); + fire(new OnBeforeCommandExecute()); $data = $command->onHandler($this->parameters); - fire(Event::AFTER_COMMAND_EXECUTE, [$data]); + fire(new OnAfterCommandExecute($data)); return $data; } diff --git a/Server/Events/OnAfterCommandExecute.php b/Server/Events/OnAfterCommandExecute.php new file mode 100644 index 00000000..03805b62 --- /dev/null +++ b/Server/Events/OnAfterCommandExecute.php @@ -0,0 +1,16 @@ +on($key, $value,0); return; } if (is_array($value)) { if (is_object($value[0]) && !($value[0] instanceof \Closure)) { - Event::on($key, $value, true); + $eventProvider->on($key, $value,0); return; } if (is_string($value[0])) { $value[0] = Kiri::createObject($value[0]); - Event::on($key, $value, true); + $eventProvider->on($key, $value,0); return; } @@ -244,7 +242,7 @@ abstract class BaseApplication extends Component if (!is_callable($item, true)) { throw new InitException("Class does not hav callback."); } - Event::on($key, $item, true); + $eventProvider->on($key, $item,0); } } diff --git a/System/Async.php b/System/Async.php index 17c5d6e6..3671d8fe 100644 --- a/System/Async.php +++ b/System/Async.php @@ -8,6 +8,7 @@ use Exception; use HttpServer\IInterface\Task; use ReflectionException; use Kiri\Abstracts\Component; +use Server\ServerManager; /** * Class Async @@ -37,22 +38,8 @@ class Async extends Component */ public function dispatch(string $name, array $params = []) { - $server = Kiri::app()->getSwoole(); - if (!isset($server->setting['task_worker_num'])) { - return; - } - - if (!isset(static::$_absences[$name])) { - return; - } - - /** @var Task $class */ - $class = Kiri::createObject(static::$_absences[$name]); - $class->setParams($params); - - $randWorkerId = random_int(0, $server->setting['task_worker_num'] - 1); - - $server->task(serialize($class), $randWorkerId); + $context = ServerManager::getContext(); + $context->task(static::$_absences[$name], $params); } } diff --git a/System/Error/ErrorHandler.php b/System/Error/ErrorHandler.php index 7e2cdae1..b3117618 100644 --- a/System/Error/ErrorHandler.php +++ b/System/Error/ErrorHandler.php @@ -14,7 +14,9 @@ use HttpServer\IInterface\IFormatter; use Kiri\Abstracts\Component; use Kiri\Core\Json; use Kiri\Event; +use Kiri\Events\EventDispatch; use Kiri\Kiri; +use Server\Events\OnAfterRequest; /** * Class ErrorHandler @@ -74,7 +76,7 @@ class ErrorHandler extends Component implements ErrorInterface { $this->category = 'exception'; - Event::trigger(Event::SYSTEM_RESOURCE_CLEAN); + di(EventDispatch::class)->dispatch(new OnAfterRequest()); $this->sendError($exception->getMessage(), $exception->getFile(), $exception->getLine()); } @@ -99,7 +101,8 @@ class ErrorHandler extends Component implements ErrorInterface Kiri::app()->error($data, 'error'); - Event::trigger(Event::SYSTEM_RESOURCE_CLEAN); + di(EventDispatch::class)->dispatch(new OnAfterRequest()); + throw new \ErrorException($error[1], $error[0], 1, $error[2], $error[3]); } diff --git a/System/Error/LoggerProcess.php b/System/Error/LoggerProcess.php index 30943674..78adb913 100644 --- a/System/Error/LoggerProcess.php +++ b/System/Error/LoggerProcess.php @@ -9,9 +9,9 @@ use JetBrains\PhpStorm\Pure; use Server\SInterface\CustomProcess; use Kiri\Core\Json; use Kiri\Exception\ComponentException; -use Kiri\Process\Process; use Kiri\Kiri; use Swoole\Coroutine; +use Swoole\Process; /** @@ -24,7 +24,7 @@ class LoggerProcess implements CustomProcess /** * @return string */ - #[Pure] public function getProcessName(\Swoole\Process $process): string + #[Pure] public function getProcessName(Process $process): string { // TODO: Implement getProcessName() method. return get_called_class(); @@ -32,10 +32,10 @@ class LoggerProcess implements CustomProcess /** - * @param \Swoole\Process $process + * @param Process $process * @throws ComponentException */ - public function onHandler(\Swoole\Process $process): void + public function onHandler(Process $process): void { // TODO: Implement onHandler() method. $this->message($process); @@ -43,11 +43,11 @@ class LoggerProcess implements CustomProcess /** - * @param \Swoole\Process $process + * @param Process $process * @throws ComponentException * @throws Exception */ - public function message(\Swoole\Process $process) + public function message(Process $process) { $message = Json::decode($process->read()); if (!empty($message)) { diff --git a/System/Kiri.php b/System/Kiri.php index d4cd834b..aa57b005 100644 --- a/System/Kiri.php +++ b/System/Kiri.php @@ -17,8 +17,9 @@ use Kiri\Abstracts\Config; use Kiri\Core\Json; use Kiri\Di\Container; use Kiri\Exception\NotFindClassException; -use Kiri\Process\Process; +use Server\ServerManager; use Swoole\Coroutine; +use Swoole\Process; use Swoole\WebSocket\Server; @@ -119,26 +120,18 @@ class Kiri return static::$service; } - /** - * @param $name - * @return bool - */ - #[Pure] public static function has($name): bool + /** + * @param $name + * @return bool + * @throws NotFindClassException + * @throws ReflectionException + */ + public static function has($name): bool { return static::$service->has($name); } - /** - * @param $className - * @param $id - */ - public static function setAlias($className, $id) - { - static::$service->setAlias($className, $id); - } - - /** * @param $port * @return bool @@ -448,30 +441,21 @@ class Kiri /** * @param string $class * @param array $params - * @throws NotFindClassException * @throws ReflectionException * @throws Exception */ public static function async(string $class, array $params = []) { - $server = static::app()->getSwoole(); - if (!isset($server->setting['task_worker_num']) || !class_exists($class)) { - return; - } - - /** @var Task $class */ - $class = static::createObject($class); - $class->setParams($params); - - $server->task(swoole_serialize($class)); + $manager = ServerManager::getContext(); + $manager->task(new $class(...$params)); } - /** - * @param $v1 - * @param $v2 - * @return float - */ + /** + * @param array $v1 + * @param array $v2 + * @return float + */ #[Pure] public static function distance(array $v1, array $v2): float { $maxX = max($v1['x'], $v2['x']); diff --git a/System/Process/Biomonitoring.php b/System/Process/Biomonitoring.php index 11ec186a..ba02c46a 100644 --- a/System/Process/Biomonitoring.php +++ b/System/Process/Biomonitoring.php @@ -9,6 +9,7 @@ use JetBrains\PhpStorm\Pure; use Server\SInterface\CustomProcess; use Kiri\Kiri; use Swoole\Timer; +use Swoole\Process; /** * Class Biomonitoring @@ -19,10 +20,10 @@ class Biomonitoring implements CustomProcess /** - * @param \Swoole\Process $process + * @param Process $process * @return string */ - #[Pure] public function getProcessName(\Swoole\Process $process): string + #[Pure] public function getProcessName(Process $process): string { // TODO: Implement getProcessName() method. return get_called_class(); @@ -30,10 +31,10 @@ class Biomonitoring implements CustomProcess /** - * @param \Swoole\Process $process + * @param Process $process * @throws Exception */ - public function onHandler(\Swoole\Process $process): void + public function onHandler(Process $process): void { $server = Kiri::app()->getSwoole(); Timer::tick(1000, function () use ($server) { diff --git a/System/Process/Process.php b/System/Process/Process.php deleted file mode 100644 index b937f9e3..00000000 --- a/System/Process/Process.php +++ /dev/null @@ -1,63 +0,0 @@ -pid); - - putenv('environmental=' . Kiri::PROCESS); - - fire(Event::SERVER_WORKER_START); - if (Kiri::getPlatform()->isLinux()) { - name($this->pid, $this->getProcessName()); - } - if (method_exists($this, 'before')) { - $this->before($process); - } - $this->onHandler($process); - } - - - /** - * @return string - */ - #[Pure] private function getPrefix(): string - { - return static::class; - } - - -} diff --git a/function.php b/function.php index c823e6f9..ce1aa1a4 100644 --- a/function.php +++ b/function.php @@ -15,6 +15,8 @@ use Kiri\Application; use Kiri\Core\ArrayAccess; use Kiri\Error\Logger; use Kiri\Event; +use Kiri\Events\EventDispatch; +use Kiri\Events\EventProvider; use Kiri\Exception\ConfigException; use Kiri\Exception\NotFindClassException; use Kiri\Kiri; @@ -335,14 +337,13 @@ if (!function_exists('fire')) { /** - * @param string $event - * @param array $params - * @throws Exception - * @throws Exception + * @param object $event + * @throws NotFindClassException + * @throws ReflectionException */ - function fire(string $event, array $params = []) + function fire(object $event) { - Event::trigger($event, $params); + di(EventDispatch::class)->dispatch($event); } } @@ -751,7 +752,8 @@ if (!function_exists('event')) { */ function event($name, $callback, bool $isAppend = true) { - Event::on($name, $callback, $isAppend); + $pro = di(EventProvider::class); + $pro->on($name, $callback,0); } }