This commit is contained in:
2021-08-13 14:58:58 +08:00
parent 15851ba97c
commit 2432b98be5
13 changed files with 90 additions and 151 deletions
+5 -7
View File
@@ -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;
}
+6 -3
View File
@@ -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);
}
+4 -2
View File
@@ -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;
}
+16
View File
@@ -0,0 +1,16 @@
<?php
namespace Server\Events;
class OnAfterCommandExecute
{
/**
* @param mixed $data
*/
public function __construct(public mixed $data)
{
}
}
+8
View File
@@ -0,0 +1,8 @@
<?php
namespace Server\Events;
class OnBeforeCommandExecute
{
}
+7 -9
View File
@@ -29,6 +29,7 @@ use Kiri\Di\LocalService;
use Kiri\Error\ErrorHandler;
use Kiri\Error\Logger;
use Kiri\Event;
use Kiri\Events\EventProvider;
use Kiri\Exception\InitException;
use Kiri\Exception\NotFindClassException;
use Kiri\Jwt\Jwt;
@@ -220,23 +221,20 @@ abstract class BaseApplication extends Component
*/
private function addEvent($key, $value): void
{
if ($value instanceof \Closure) {
Event::on($key, $value, true);
return;
}
if (is_object($value)) {
Event::on($key, $value, true);
$eventProvider = di(EventProvider::class);
if ($value instanceof \Closure || is_object($value)) {
$eventProvider->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);
}
}
+3 -16
View File
@@ -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);
}
}
+5 -2
View File
@@ -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]);
}
+6 -6
View File
@@ -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)) {
+16 -32
View File
@@ -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']);
+5 -4
View File
@@ -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) {
-63
View File
@@ -1,63 +0,0 @@
<?php
declare(strict_types=1);
namespace Kiri\Process;
use Exception;
use JetBrains\PhpStorm\Pure;
use Kiri\Event;
use Kiri\Kiri;
/**
* Class Process
* @package Kiri\Kiri\Service
*/
abstract class Process extends \Swoole\Process implements SProcess
{
/**
* Process constructor.
* @param $application
* @param $name
* @param bool $enable_coroutine
* @throws Exception
*/
public function __construct($application, $name, $enable_coroutine = true)
{
parent::__construct([$this, '_load'], false, 1, $enable_coroutine);
}
/**
* @param Process $process
* @throws Exception
*/
public function _load(Process $process)
{
Kiri::setProcessId($this->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;
}
}
+9 -7
View File
@@ -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);
}
}