Files
kiri-core/core/AspectManager.php
T

125 lines
2.4 KiB
PHP
Raw Normal View History

2021-03-29 03:09:55 +08:00
<?php
2021-08-11 01:04:57 +08:00
namespace Kiri;
2021-03-29 03:09:55 +08:00
2021-03-29 10:05:51 +08:00
use Exception;
2021-08-11 01:04:57 +08:00
use Kiri\Abstracts\Component;
2021-08-23 19:10:44 +08:00
use ReflectionException;
2021-03-29 03:09:55 +08:00
defined('ASPECT_ERROR') or define('ASPECT_ERROR', 'Aspect annotation must implement ');
/**
* Class Aop
2021-08-11 01:04:57 +08:00
* @package Kiri
2021-03-29 03:09:55 +08:00
*/
2021-08-23 18:58:22 +08:00
class AspectManager extends Component
2021-03-29 03:09:55 +08:00
{
2021-07-12 18:51:41 +08:00
private static array $_aop = [];
2021-04-21 01:45:59 +08:00
2021-07-12 18:51:41 +08:00
/**
* @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;
}
2021-08-23 19:10:44 +08:00
/**
* @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;
}
2021-07-12 18:51:41 +08:00
/**
* @param $handler
* @return bool
*/
public function hasAop($handler): bool
{
return isset(static::$_aop[$handler[0]::class . '::' . $handler[1]]);
}
2021-04-21 01:45:59 +08:00
2021-04-22 14:22:11 +08:00
/**
* @param $handler
* @param $params
* @return mixed
* @throws ReflectionException
* @throws Exception
*/
2021-07-12 18:51:41 +08:00
final public function dispatch($handler, $params): mixed
{
$aopName = $handler[0]::class . '::' . $handler[1];
2021-08-11 01:04:57 +08:00
$reflect = Kiri::getDi()->getReflect(current(static::$_aop[$aopName]));
2021-07-12 18:51:41 +08:00
if (!$reflect->isInstantiable() || !$reflect->hasMethod('invoke')) {
throw new Exception(ASPECT_ERROR . IAspect::class);
}
$method = $reflect->getMethod('invoke');
return $method->invokeArgs($reflect->newInstance($handler), $params);
}
2021-04-21 01:45:59 +08:00
2021-04-22 14:22:11 +08:00
/**
2021-07-12 18:51:41 +08:00
* @param array $handler
2021-08-02 18:12:32 +08:00
* @return IAspect
2021-04-22 14:22:11 +08:00
* @throws Exception
2021-08-02 18:12:32 +08:00
* @throws ReflectionException
2021-04-22 14:22:11 +08:00
*/
2021-08-02 18:12:32 +08:00
public function getAop(array $handler): IAspect
2021-07-12 18:51:41 +08:00
{
$aopName = $handler[0]::class . '::' . $handler[1];
2021-04-21 01:45:59 +08:00
2021-08-11 01:04:57 +08:00
$reflect = Kiri::getDi()->get(current(static::$_aop[$aopName]));
2021-08-02 18:12:32 +08:00
if (!method_exists($reflect, 'invoke')) {
2021-07-12 18:51:41 +08:00
throw new Exception(ASPECT_ERROR . IAspect::class);
}
return $reflect;
}
2021-04-21 01:45:59 +08:00
2021-04-22 14:22:11 +08:00
/**
* @param $handler
* @param $params
* @return mixed
* @throws Exception
*/
2021-07-12 18:51:41 +08:00
private function notFound($handler, $params): mixed
{
if (!method_exists($handler[0], $handler[1])) {
return response()->close(404);
}
return call_user_func($handler, ...$params);
}
2021-03-29 03:09:55 +08:00
}