改名
This commit is contained in:
@@ -5,9 +5,7 @@ namespace Annotation;
|
|||||||
|
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
use Kiri\Exception\ComponentException;
|
use Kiri\Events\EventProvider;
|
||||||
use Kiri\Kiri;
|
|
||||||
use Kiri\Event as SEvent;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -34,10 +32,10 @@ use Kiri\Event as SEvent;
|
|||||||
* @return bool
|
* @return bool
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function execute(mixed $class, mixed $method = null): bool
|
public function execute(mixed $class, mixed $method = null): bool
|
||||||
{
|
{
|
||||||
// TODO: Implement execute() method.
|
$pro = di(EventProvider::class);
|
||||||
SEvent::on($this->name, [$class, $method]);
|
$pro->on($this->name, [$class, $method]);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,9 @@ namespace Annotation;
|
|||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
use Kiri\Event;
|
use Kiri\Event;
|
||||||
|
use Kiri\Events\EventProvider;
|
||||||
use Kiri\Kiri;
|
use Kiri\Kiri;
|
||||||
|
use Server\Events\OnWorkerExit;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class LocalService
|
* Class LocalService
|
||||||
@@ -28,9 +30,10 @@ use Kiri\Kiri;
|
|||||||
if ($this->async_reload !== true) {
|
if ($this->async_reload !== true) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Event::on(Event::SERVER_WORKER_EXIT, function () {
|
$pro = di(EventProvider::class);
|
||||||
Kiri::app()->remove($this->service);
|
$pro->on(OnWorkerExit::class, function () {
|
||||||
});
|
di(\Kiri\Di\LocalService::class)->remove($this->service);
|
||||||
|
},0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ use Kiri\Abstracts\Component;
|
|||||||
use Kiri\Abstracts\Input;
|
use Kiri\Abstracts\Input;
|
||||||
use Kiri\Event;
|
use Kiri\Event;
|
||||||
use Kiri\Kiri;
|
use Kiri\Kiri;
|
||||||
|
use Server\Events\OnAfterCommandExecute;
|
||||||
|
use Server\Events\OnBeforeCommandExecute;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class AbstractConsole
|
* Class AbstractConsole
|
||||||
@@ -58,11 +60,11 @@ abstract class AbstractConsole extends Component
|
|||||||
*/
|
*/
|
||||||
public function execCommand(Command $command): mixed
|
public function execCommand(Command $command): mixed
|
||||||
{
|
{
|
||||||
fire(Event::BEFORE_COMMAND_EXECUTE);
|
fire(new OnBeforeCommandExecute());
|
||||||
|
|
||||||
$data = $command->onHandler($this->parameters);
|
$data = $command->onHandler($this->parameters);
|
||||||
|
|
||||||
fire(Event::AFTER_COMMAND_EXECUTE, [$data]);
|
fire(new OnAfterCommandExecute($data));
|
||||||
|
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Server\Events;
|
||||||
|
|
||||||
|
class OnAfterCommandExecute
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $data
|
||||||
|
*/
|
||||||
|
public function __construct(public mixed $data)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Server\Events;
|
||||||
|
|
||||||
|
class OnBeforeCommandExecute
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
@@ -29,6 +29,7 @@ use Kiri\Di\LocalService;
|
|||||||
use Kiri\Error\ErrorHandler;
|
use Kiri\Error\ErrorHandler;
|
||||||
use Kiri\Error\Logger;
|
use Kiri\Error\Logger;
|
||||||
use Kiri\Event;
|
use Kiri\Event;
|
||||||
|
use Kiri\Events\EventProvider;
|
||||||
use Kiri\Exception\InitException;
|
use Kiri\Exception\InitException;
|
||||||
use Kiri\Exception\NotFindClassException;
|
use Kiri\Exception\NotFindClassException;
|
||||||
use Kiri\Jwt\Jwt;
|
use Kiri\Jwt\Jwt;
|
||||||
@@ -220,23 +221,20 @@ abstract class BaseApplication extends Component
|
|||||||
*/
|
*/
|
||||||
private function addEvent($key, $value): void
|
private function addEvent($key, $value): void
|
||||||
{
|
{
|
||||||
if ($value instanceof \Closure) {
|
$eventProvider = di(EventProvider::class);
|
||||||
Event::on($key, $value, true);
|
if ($value instanceof \Closure || is_object($value)) {
|
||||||
return;
|
$eventProvider->on($key, $value,0);
|
||||||
}
|
|
||||||
if (is_object($value)) {
|
|
||||||
Event::on($key, $value, true);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (is_array($value)) {
|
if (is_array($value)) {
|
||||||
if (is_object($value[0]) && !($value[0] instanceof \Closure)) {
|
if (is_object($value[0]) && !($value[0] instanceof \Closure)) {
|
||||||
Event::on($key, $value, true);
|
$eventProvider->on($key, $value,0);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_string($value[0])) {
|
if (is_string($value[0])) {
|
||||||
$value[0] = Kiri::createObject($value[0]);
|
$value[0] = Kiri::createObject($value[0]);
|
||||||
Event::on($key, $value, true);
|
$eventProvider->on($key, $value,0);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -244,7 +242,7 @@ abstract class BaseApplication extends Component
|
|||||||
if (!is_callable($item, true)) {
|
if (!is_callable($item, true)) {
|
||||||
throw new InitException("Class does not hav callback.");
|
throw new InitException("Class does not hav callback.");
|
||||||
}
|
}
|
||||||
Event::on($key, $item, true);
|
$eventProvider->on($key, $item,0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+3
-16
@@ -8,6 +8,7 @@ use Exception;
|
|||||||
use HttpServer\IInterface\Task;
|
use HttpServer\IInterface\Task;
|
||||||
use ReflectionException;
|
use ReflectionException;
|
||||||
use Kiri\Abstracts\Component;
|
use Kiri\Abstracts\Component;
|
||||||
|
use Server\ServerManager;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Async
|
* Class Async
|
||||||
@@ -37,22 +38,8 @@ class Async extends Component
|
|||||||
*/
|
*/
|
||||||
public function dispatch(string $name, array $params = [])
|
public function dispatch(string $name, array $params = [])
|
||||||
{
|
{
|
||||||
$server = Kiri::app()->getSwoole();
|
$context = ServerManager::getContext();
|
||||||
if (!isset($server->setting['task_worker_num'])) {
|
$context->task(static::$_absences[$name], $params);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,7 +14,9 @@ use HttpServer\IInterface\IFormatter;
|
|||||||
use Kiri\Abstracts\Component;
|
use Kiri\Abstracts\Component;
|
||||||
use Kiri\Core\Json;
|
use Kiri\Core\Json;
|
||||||
use Kiri\Event;
|
use Kiri\Event;
|
||||||
|
use Kiri\Events\EventDispatch;
|
||||||
use Kiri\Kiri;
|
use Kiri\Kiri;
|
||||||
|
use Server\Events\OnAfterRequest;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class ErrorHandler
|
* Class ErrorHandler
|
||||||
@@ -74,7 +76,7 @@ class ErrorHandler extends Component implements ErrorInterface
|
|||||||
{
|
{
|
||||||
$this->category = 'exception';
|
$this->category = 'exception';
|
||||||
|
|
||||||
Event::trigger(Event::SYSTEM_RESOURCE_CLEAN);
|
di(EventDispatch::class)->dispatch(new OnAfterRequest());
|
||||||
|
|
||||||
$this->sendError($exception->getMessage(), $exception->getFile(), $exception->getLine());
|
$this->sendError($exception->getMessage(), $exception->getFile(), $exception->getLine());
|
||||||
}
|
}
|
||||||
@@ -99,7 +101,8 @@ class ErrorHandler extends Component implements ErrorInterface
|
|||||||
|
|
||||||
Kiri::app()->error($data, 'error');
|
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]);
|
throw new \ErrorException($error[1], $error[0], 1, $error[2], $error[3]);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,9 +9,9 @@ use JetBrains\PhpStorm\Pure;
|
|||||||
use Server\SInterface\CustomProcess;
|
use Server\SInterface\CustomProcess;
|
||||||
use Kiri\Core\Json;
|
use Kiri\Core\Json;
|
||||||
use Kiri\Exception\ComponentException;
|
use Kiri\Exception\ComponentException;
|
||||||
use Kiri\Process\Process;
|
|
||||||
use Kiri\Kiri;
|
use Kiri\Kiri;
|
||||||
use Swoole\Coroutine;
|
use Swoole\Coroutine;
|
||||||
|
use Swoole\Process;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -24,7 +24,7 @@ class LoggerProcess implements CustomProcess
|
|||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
#[Pure] public function getProcessName(\Swoole\Process $process): string
|
#[Pure] public function getProcessName(Process $process): string
|
||||||
{
|
{
|
||||||
// TODO: Implement getProcessName() method.
|
// TODO: Implement getProcessName() method.
|
||||||
return get_called_class();
|
return get_called_class();
|
||||||
@@ -32,10 +32,10 @@ class LoggerProcess implements CustomProcess
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param \Swoole\Process $process
|
* @param Process $process
|
||||||
* @throws ComponentException
|
* @throws ComponentException
|
||||||
*/
|
*/
|
||||||
public function onHandler(\Swoole\Process $process): void
|
public function onHandler(Process $process): void
|
||||||
{
|
{
|
||||||
// TODO: Implement onHandler() method.
|
// TODO: Implement onHandler() method.
|
||||||
$this->message($process);
|
$this->message($process);
|
||||||
@@ -43,11 +43,11 @@ class LoggerProcess implements CustomProcess
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param \Swoole\Process $process
|
* @param Process $process
|
||||||
* @throws ComponentException
|
* @throws ComponentException
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function message(\Swoole\Process $process)
|
public function message(Process $process)
|
||||||
{
|
{
|
||||||
$message = Json::decode($process->read());
|
$message = Json::decode($process->read());
|
||||||
if (!empty($message)) {
|
if (!empty($message)) {
|
||||||
|
|||||||
+16
-32
@@ -17,8 +17,9 @@ use Kiri\Abstracts\Config;
|
|||||||
use Kiri\Core\Json;
|
use Kiri\Core\Json;
|
||||||
use Kiri\Di\Container;
|
use Kiri\Di\Container;
|
||||||
use Kiri\Exception\NotFindClassException;
|
use Kiri\Exception\NotFindClassException;
|
||||||
use Kiri\Process\Process;
|
use Server\ServerManager;
|
||||||
use Swoole\Coroutine;
|
use Swoole\Coroutine;
|
||||||
|
use Swoole\Process;
|
||||||
use Swoole\WebSocket\Server;
|
use Swoole\WebSocket\Server;
|
||||||
|
|
||||||
|
|
||||||
@@ -119,26 +120,18 @@ class Kiri
|
|||||||
return static::$service;
|
return static::$service;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $name
|
* @param $name
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
* @throws NotFindClassException
|
||||||
#[Pure] public static function has($name): bool
|
* @throws ReflectionException
|
||||||
|
*/
|
||||||
|
public static function has($name): bool
|
||||||
{
|
{
|
||||||
return static::$service->has($name);
|
return static::$service->has($name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $className
|
|
||||||
* @param $id
|
|
||||||
*/
|
|
||||||
public static function setAlias($className, $id)
|
|
||||||
{
|
|
||||||
static::$service->setAlias($className, $id);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $port
|
* @param $port
|
||||||
* @return bool
|
* @return bool
|
||||||
@@ -448,30 +441,21 @@ class Kiri
|
|||||||
/**
|
/**
|
||||||
* @param string $class
|
* @param string $class
|
||||||
* @param array $params
|
* @param array $params
|
||||||
* @throws NotFindClassException
|
|
||||||
* @throws ReflectionException
|
* @throws ReflectionException
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public static function async(string $class, array $params = [])
|
public static function async(string $class, array $params = [])
|
||||||
{
|
{
|
||||||
$server = static::app()->getSwoole();
|
$manager = ServerManager::getContext();
|
||||||
if (!isset($server->setting['task_worker_num']) || !class_exists($class)) {
|
$manager->task(new $class(...$params));
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @var Task $class */
|
|
||||||
$class = static::createObject($class);
|
|
||||||
$class->setParams($params);
|
|
||||||
|
|
||||||
$server->task(swoole_serialize($class));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $v1
|
* @param array $v1
|
||||||
* @param $v2
|
* @param array $v2
|
||||||
* @return float
|
* @return float
|
||||||
*/
|
*/
|
||||||
#[Pure] public static function distance(array $v1, array $v2): float
|
#[Pure] public static function distance(array $v1, array $v2): float
|
||||||
{
|
{
|
||||||
$maxX = max($v1['x'], $v2['x']);
|
$maxX = max($v1['x'], $v2['x']);
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ use JetBrains\PhpStorm\Pure;
|
|||||||
use Server\SInterface\CustomProcess;
|
use Server\SInterface\CustomProcess;
|
||||||
use Kiri\Kiri;
|
use Kiri\Kiri;
|
||||||
use Swoole\Timer;
|
use Swoole\Timer;
|
||||||
|
use Swoole\Process;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Biomonitoring
|
* Class Biomonitoring
|
||||||
@@ -19,10 +20,10 @@ class Biomonitoring implements CustomProcess
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param \Swoole\Process $process
|
* @param Process $process
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
#[Pure] public function getProcessName(\Swoole\Process $process): string
|
#[Pure] public function getProcessName(Process $process): string
|
||||||
{
|
{
|
||||||
// TODO: Implement getProcessName() method.
|
// TODO: Implement getProcessName() method.
|
||||||
return get_called_class();
|
return get_called_class();
|
||||||
@@ -30,10 +31,10 @@ class Biomonitoring implements CustomProcess
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param \Swoole\Process $process
|
* @param Process $process
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function onHandler(\Swoole\Process $process): void
|
public function onHandler(Process $process): void
|
||||||
{
|
{
|
||||||
$server = Kiri::app()->getSwoole();
|
$server = Kiri::app()->getSwoole();
|
||||||
Timer::tick(1000, function () use ($server) {
|
Timer::tick(1000, function () use ($server) {
|
||||||
|
|||||||
@@ -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
@@ -15,6 +15,8 @@ use Kiri\Application;
|
|||||||
use Kiri\Core\ArrayAccess;
|
use Kiri\Core\ArrayAccess;
|
||||||
use Kiri\Error\Logger;
|
use Kiri\Error\Logger;
|
||||||
use Kiri\Event;
|
use Kiri\Event;
|
||||||
|
use Kiri\Events\EventDispatch;
|
||||||
|
use Kiri\Events\EventProvider;
|
||||||
use Kiri\Exception\ConfigException;
|
use Kiri\Exception\ConfigException;
|
||||||
use Kiri\Exception\NotFindClassException;
|
use Kiri\Exception\NotFindClassException;
|
||||||
use Kiri\Kiri;
|
use Kiri\Kiri;
|
||||||
@@ -335,14 +337,13 @@ if (!function_exists('fire')) {
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $event
|
* @param object $event
|
||||||
* @param array $params
|
* @throws NotFindClassException
|
||||||
* @throws Exception
|
* @throws ReflectionException
|
||||||
* @throws Exception
|
|
||||||
*/
|
*/
|
||||||
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)
|
function event($name, $callback, bool $isAppend = true)
|
||||||
{
|
{
|
||||||
Event::on($name, $callback, $isAppend);
|
$pro = di(EventProvider::class);
|
||||||
|
$pro->on($name, $callback,0);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user