This commit is contained in:
as2252258@163.com
2021-09-19 16:42:29 +08:00
parent d77f5b16c3
commit ff58d0faef
16 changed files with 256 additions and 262 deletions
-2
View File
@@ -35,11 +35,9 @@
"autoload": {
"psr-4": {
"Kiri\\": "core/",
"Http\\": "http-helper/",
"Http\\Message\\": "http-message/",
"Http\\Handler\\": "http-handler/",
"Server\\": "http-server/",
"Console\\": "kiri-console/",
"Gii\\": "kiri-gii/",
"Annotation\\": "note/"
},
-2
View File
@@ -16,7 +16,6 @@ use Exception;
use Http\Handler\Router;
use Server\Server;
use Kafka\KafkaProvider;
use Kiri\AspectManager;
use Kiri\Async;
use Kiri\Cache\Redis;
use Kiri\Di\LocalService;
@@ -460,7 +459,6 @@ abstract class BaseApplication extends Component
'logger' => ['class' => Logger::class],
'annotation' => ['class' => SAnnotation::class],
'databases' => ['class' => Connection::class],
'aop' => ['class' => AspectManager::class],
'jwt' => ['class' => Jwt::class],
'async' => ['class' => Async::class],
'kafka-container' => ['class' => KafkaProvider::class],
-124
View File
@@ -1,124 +0,0 @@
<?php
namespace Kiri;
use Exception;
use Kiri\Abstracts\Component;
use ReflectionException;
defined('ASPECT_ERROR') or define('ASPECT_ERROR', 'Aspect annotation must implement ');
/**
* Class Aop
* @package Kiri
*/
class AspectManager extends Component
{
private static array $_aop = [];
/**
* @param array $handler
* @param string $aspect
*/
public function aop_add(array $handler, string $aspect)
{
[$class, $method] = $handler;
$alias = $class::class . '::' . $method;
if (!isset(static::$_aop[$alias])) {
static::$_aop[$alias] = [];
}
if (in_array($aspect, static::$_aop[$alias])) {
return;
}
static::$_aop[$alias][] = $aspect;
}
/**
* @param string $class
* @param string $method
* @param string $aspect
*/
public function addAspect(string $class, string $method, string $aspect)
{
$alias = $class . '::' . $method;
if (!isset(static::$_aop[$alias])) {
static::$_aop[$alias] = [];
}
if (in_array($aspect, static::$_aop[$alias])) {
return;
}
static::$_aop[$alias][] = $aspect;
}
/**
* @param $handler
* @return bool
*/
public function hasAop($handler): bool
{
return isset(static::$_aop[$handler[0]::class . '::' . $handler[1]]);
}
/**
* @param $handler
* @param $params
* @return mixed
* @throws ReflectionException
* @throws Exception
*/
final public function dispatch($handler, $params): mixed
{
$aopName = $handler[0]::class . '::' . $handler[1];
$reflect = Kiri::getDi()->getReflect(current(static::$_aop[$aopName]));
if (!$reflect->isInstantiable() || !$reflect->hasMethod('invoke')) {
throw new Exception(ASPECT_ERROR . IAspect::class);
}
$method = $reflect->getMethod('invoke');
return $method->invokeArgs($reflect->newInstance($handler), $params);
}
/**
* @param array $handler
* @return IAspect
* @throws Exception
* @throws ReflectionException
*/
public function getAop(array $handler): IAspect
{
$aopName = $handler[0]::class . '::' . $handler[1];
$reflect = Kiri::getDi()->get(current(static::$_aop[$aopName]));
if (!method_exists($reflect, 'invoke')) {
throw new Exception(ASPECT_ERROR . IAspect::class);
}
return $reflect;
}
/**
* @param $handler
* @param $params
* @return mixed
* @throws Exception
*/
private function notFound($handler, $params): mixed
{
if (!method_exists($handler[0], $handler[1])) {
return response()->close(404);
}
return call_user_func($handler, ...$params);
}
}
+8
View File
@@ -0,0 +1,8 @@
<?php
namespace Kiri\Proxy;
abstract class AProxy
{
}
+53
View File
@@ -0,0 +1,53 @@
<?php
namespace Kiri\Proxy;
use Annotation\Aspect;
use Http\Handler\Handler;
use Kiri\Di\NoteManager;
use Kiri\IAspect;
use Kiri\Kiri;
/**
*
*/
class AspectProxy extends AProxy implements ProxyInterface
{
/**
* @param \Http\Handler\Handler $executor
* @return mixed
*/
public function proxy(Handler $executor): mixed
{
if ($executor->callback instanceof \Closure) {
return call_user_func($executor->callback, ...$executor->params);
}
$controller = Kiri::getDi()->get($executor->callback[0]);
$aspect = $this->getAspect($executor->callback);
if (!is_null($aspect)) {
$aspect->before();
$result = $aspect->invoke([$controller, $executor->callback[1]], $executor->params);
$aspect->after($result);
} else {
$result = call_user_func([$controller, $executor->callback[1]]);
}
return $result;
}
/**
* @param array $executor
* @return ?IAspect
*/
protected function getAspect(array $executor): ?Aspect
{
$aspect = NoteManager::getSpecify_annotation(Aspect::class, $executor[0], $executor[1]);
if (!is_null($aspect)) {
$aspect = Kiri::getDi()->get($aspect->aspect);
}
return $aspect;
}
}
+17
View File
@@ -0,0 +1,17 @@
<?php
namespace Kiri\Proxy;
use Http\Handler\Handler;
interface ProxyInterface
{
/**
* @param \Http\Handler\Handler $executor
* @return mixed
*/
public function proxy(Handler $executor): mixed;
}
-16
View File
@@ -9,7 +9,6 @@ use Http\Handler\Router;
use JetBrains\PhpStorm\Pure;
use Kiri\Abstracts\Config;
use Kiri\Application;
use Kiri\AspectManager;
use Kiri\Core\ArrayAccess;
use Kiri\Di\NoteManager;
use Kiri\Error\Logger;
@@ -412,21 +411,6 @@ if (!function_exists('fire')) {
}
}
if (!function_exists('aop')) {
/**
* @param mixed $handler
* @param array $params
* @return mixed
* @throws Exception
*/
function aop(mixed $handler, array $params = []): mixed
{
return Kiri::getDi()->get(AspectManager::class)->dispatch($handler, $params);
}
}
if (!function_exists('app')) {
+3 -10
View File
@@ -6,6 +6,7 @@ use Http\Handler\Handler as CHl;
use Http\Message\ServerRequest;
use Kiri\Core\Help;
use Kiri\Kiri;
use Kiri\Proxy\AspectProxy;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
@@ -74,16 +75,8 @@ abstract class Handler implements RequestHandlerInterface
*/
protected function dispatcher(ServerRequestInterface $request): mixed
{
if ($this->handler->callback instanceof \Closure) {
$response = call_user_func($this->handler->callback, ...$this->handler->params);
} else {
[$controller, $action] = $this->handler->callback;
$controller = Kiri::getDi()->get($controller);
$response = call_user_func([$controller, $action], ...$this->handler->params);
}
if (!($response instanceof ResponseInterface)) {
$aspect = Kiri::getDi()->get(AspectProxy::class);
if (!(($response = $aspect->proxy($this->handler)) instanceof ResponseInterface)) {
$response = $this->transferToResponse($response);
}
$response->withHeader('Run-Time', $this->_runTime($request));
+1 -2
View File
@@ -106,8 +106,7 @@ class Pipeline
* @param $destination
* @param $parameters
* @return Closure|array
* @throws ReflectionException
*/
*/
private function aspect_caller($destination, $parameters): Closure|array
{
[$controller, $action] = $destination;
+1 -1
View File
@@ -212,7 +212,7 @@ class Router
/**
* @throws Exception
*/
public function _loader()
public function read_files()
{
$this->loadRouteDir(APP_PATH . 'routes');
}
+23
View File
@@ -0,0 +1,23 @@
<?php
namespace Server\Events;
use Swoole\Server;
/**
*
*/
class OnTaskerStart
{
/**
* @param Server $server
* @param int $workerId
*/
public function __construct(public Server $server, public int $workerId)
{
}
}
+1 -1
View File
@@ -61,7 +61,7 @@ class Http extends \Server\Abstracts\Http implements OnClose, OnConnect
$PsrResponse = $this->handler($handler, $PsrRequest);
}
} catch (\Throwable $throwable) {
$PsrResponse = \response()->withStatus($throwable->getCode())
$PsrResponse = $this->response->withStatus($throwable->getCode())
->withContentType(\Http\Message\Response::CONTENT_TYPE_HTML)
->withBody(new Stream(jTraceEx($throwable, null, true)));
} finally {
+9 -3
View File
@@ -7,6 +7,7 @@ use Exception;
use Kiri\Abstracts\Config;
use Kiri\Core\Help;
use Kiri\Events\EventDispatch;
use Kiri\Kiri;
use Kiri\Runtime;
use Server\Events\OnAfterWorkerStart;
use Server\Events\OnBeforeWorkerStart;
@@ -17,6 +18,7 @@ use Server\Events\OnWorkerStop;
use Server\ServerManager;
use Swoole\Server;
use Swoole\Timer;
use Server\Events\OnTaskerStart as OnTaskStart;
/**
@@ -42,9 +44,13 @@ class OnServerWorker extends \Server\Abstracts\Server
public function onWorkerStart(Server $server, int $workerId)
{
$this->eventDispatch->dispatch(new OnBeforeWorkerStart($workerId));
$this->eventDispatch->dispatch(new OnWorkerStart($server, $workerId));
if ($workerId < $server->setting['worker_num']) {
$this->eventDispatch->dispatch(new OnWorkerStart($server, $workerId));
$this->setProcessName(sprintf('Worker[%d].%d', $server->worker_pid, $workerId));
} else {
$this->eventDispatch->dispatch(new OnTaskStart($server, $workerId));
$this->setProcessName(sprintf('Tasker[%d].%d', $server->worker_pid, $workerId));
}
$this->eventDispatch->dispatch(new OnAfterWorkerStart());
}
+33
View File
@@ -0,0 +1,33 @@
<?php
namespace Server\Worker;
use Kiri\Abstracts\Config;
use Kiri\Kiri;
use Psr\EventDispatcher\EventDispatcherInterface;
use Server\ServerManager;
class OnTaskerStart extends WorkerStart implements EventDispatcherInterface
{
/**
* @throws \Kiri\Exception\ConfigException
* @throws \ReflectionException
*/
public function dispatch(object $event)
{
$isWorker = $event->workerId < $event->server->setting['worker_num'];
$time = microtime(true);
ServerManager::setEnv('environmental', Kiri::TASK);
if (!is_enable_file_modification_listening()) {
$this->interpretDirectory();
}
$this->mixed($event, $isWorker, $time);
}
}
+19 -101
View File
@@ -14,111 +14,29 @@ use Psr\EventDispatcher\EventDispatcherInterface;
use ReflectionException;
use Server\ServerManager;
class OnWorkerStart implements EventDispatcherInterface
class OnWorkerStart extends WorkerStart implements EventDispatcherInterface
{
/**
* @var Annotation
*/
#[Inject(Annotation::class)]
public Annotation $annotation;
/**
* @param object $event
* @return void
* @throws ConfigException
* @throws ReflectionException
* @throws Exception
*/
public function dispatch(object $event)
{
$isWorker = $event->workerId < $event->server->setting['worker_num'];
$time = microtime(true);
/**
* @var Router
*/
#[Inject(Router::class)]
public Router $router;
/**
* @param object $event
* @return void
* @throws ConfigException
* @throws ReflectionException
* @throws Exception
*/
public function dispatch(object $event)
{
$isWorker = $event->workerId < $event->server->setting['worker_num'];
$time = microtime(true);
$this->interpretDirectory($isWorker);
if ($isWorker) {
ServerManager::setEnv('environmental', Kiri::WORKER);
Kiri::getDi()->get(Router::class)->_loader();
$this->setProcessName(sprintf('Worker[%d].%d', $event->server->worker_pid, $event->workerId));
} else {
ServerManager::setEnv('environmental', Kiri::TASK);
$this->setProcessName(sprintf('Tasker[%d].%d', $event->server->worker_pid, $event->workerId));
}
$this->mixed($event, Config::get('id', 'system-service'), $isWorker, $time);
}
/**
* @param $event
* @param $name
* @param $isWorker
* @param $time
*/
private function mixed($event, $name, $isWorker, $time)
{
echo sprintf("\033[36m[" . date('Y-m-d H:i:s') . "]\033[0m [%s]Builder %s[%d].%d use time %s.", $name, $isWorker ? 'Worker' : 'Taker',
$event->server->worker_pid, $event->workerId, round(microtime(true) - $time, 6) . 's') . PHP_EOL;
}
/**
* @param $prefix
* @throws ConfigException
*/
protected function setProcessName($prefix)
{
if (Kiri::getPlatform()->isMac()) {
return;
}
$name = Config::get('id', 'system-service');
if (!empty($prefix)) {
$name .= '.' . $prefix;
}
swoole_set_process_name($name);
}
/**
* @throws ReflectionException
* @throws Exception
*/
private function interpretDirectory($isWorker)
{
$di = Kiri::getDi();
// $namespace = array_filter(explode('\\', Router::getNamespace()));
//
// $namespace = APP_PATH . implode('/', $namespace);
$this->annotation->read(APP_PATH . 'app', 'App');
$fileLists = $this->annotation->read(APP_PATH . 'app');
foreach ($fileLists->runtime(APP_PATH . 'app') as $class) {
foreach (NoteManager::getTargetNote($class) as $value) {
$value->execute($class);
}
$methods = $di->getMethodAttribute($class);
foreach ($methods as $method => $attribute) {
if (empty($attribute)) {
continue;
}
foreach ($attribute as $item) {
$item->execute($class, $method);
}
}
}
}
ServerManager::setEnv('environmental', Kiri::WORKER);
if (!is_enable_file_modification_listening()) {
$this->router->read_files();
$this->interpretDirectory();
}
$this->mixed($event, $isWorker, $time);
}
}
+88
View File
@@ -0,0 +1,88 @@
<?php
namespace Server\Worker;
use Annotation\Annotation;
use Annotation\Inject;
use Http\Handler\Router;
use Kiri\Abstracts\Config;
use Kiri\Di\NoteManager;
use Kiri\Exception\ConfigException;
use Kiri\Kiri;
class WorkerStart
{
/**
* @var Annotation
*/
#[Inject(Annotation::class)]
public Annotation $annotation;
/**
* @var Router
*/
#[Inject(Router::class)]
public Router $router;
/**
* @throws \ReflectionException
* @throws \Exception
*/
protected function interpretDirectory()
{
$di = Kiri::getDi();
$this->annotation->read(APP_PATH . 'app', 'App');
$fileLists = $this->annotation->read(APP_PATH . 'app');
foreach ($fileLists->runtime(APP_PATH . 'app') as $class) {
foreach (NoteManager::getTargetNote($class) as $value) {
$value->execute($class);
}
$methods = $di->getMethodAttribute($class);
foreach ($methods as $method => $attribute) {
if (empty($attribute)) {
continue;
}
foreach ($attribute as $item) {
$item->execute($class, $method);
}
}
}
}
/**
* @param $event
* @param $isWorker
* @param $time
* @throws \Kiri\Exception\ConfigException
*/
protected function mixed($event, $isWorker, $time)
{
$name = Config::get('id', 'system-service');
echo sprintf("\033[36m[" . date('Y-m-d H:i:s') . "]\033[0m [%s]Builder %s[%d].%d use time %s.", $name, $isWorker ? 'Worker' : 'Taker',
$event->server->worker_pid, $event->workerId, round(microtime(true) - $time, 6) . 's') . PHP_EOL;
}
/**
* @param $prefix
* @throws ConfigException
*/
protected function setProcessName($prefix)
{
if (Kiri::getPlatform()->isMac()) {
return;
}
$name = Config::get('id', 'system-service');
if (!empty($prefix)) {
$name .= '.' . $prefix;
}
swoole_set_process_name($name);
}
}