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
@@ -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;
}