This commit is contained in:
as2252258@163.com
2021-08-29 04:06:49 +08:00
parent 9da50baed0
commit 133ca2c273
21 changed files with 356 additions and 495 deletions
+34 -17
View File
@@ -18,20 +18,6 @@ trait Attributes
private array $_classProperty = []; private array $_classProperty = [];
private array $_mapping = [
'attributeClass' => [
'className' => [
'property' => [
''
],
'method' => [
]
]
]
];
/** /**
* @param ReflectionClass $class * @param ReflectionClass $class
*/ */
@@ -45,7 +31,10 @@ trait Attributes
if (!class_exists($attribute->getName())) { if (!class_exists($attribute->getName())) {
continue; continue;
} }
$this->_classTarget[$className][] = $attribute->newInstance();
$instance = $this->format_annotation($attribute);
$this->_classTarget[$className][] = $instance;
$this->setMappingClass($attribute, $className); $this->setMappingClass($attribute, $className);
} }
@@ -130,7 +119,7 @@ trait Attributes
if (!class_exists($attribute->getName())) { if (!class_exists($attribute->getName())) {
continue; continue;
} }
$instance = $attribute->newInstance(); $instance = $this->format_annotation($attribute);
$this->_classMethodNote[$className][$ReflectionMethod->getName()][] = $instance; $this->_classMethodNote[$className][$ReflectionMethod->getName()][] = $instance;
@@ -175,7 +164,8 @@ trait Attributes
if (!class_exists($attribute->getName())) { if (!class_exists($attribute->getName())) {
continue; continue;
} }
$instance = $attribute->newInstance();
$instance = $this->format_annotation($attribute);
$this->_classPropertyNote[$className][$ReflectionMethod->getName()] = $instance; $this->_classPropertyNote[$className][$ReflectionMethod->getName()] = $instance;
@@ -185,6 +175,33 @@ trait Attributes
} }
/**
* @param \ReflectionAttribute $attribute
* @return array
* @throws \ReflectionException
*/
private function format_annotation(ReflectionAttribute $attribute)
{
$attr = new ReflectionClass($attribute->getName());
$argument = $attribute->getArguments();
$array = ['class' => $attribute->getName(), 'params' => []];
foreach ($attr->getConstructor()->getParameters() as $key => $parameter) {
if (isset($argument[$parameter->getName()])) {
$array['params'][$parameter->getName()] = $argument[$parameter->getName()];
} else {
if (!isset($argument[$key])) {
$array['params'][$parameter->getName()] = $parameter->getDefaultValue();
} else {
$array['params'][$parameter->getName()] = $argument[$key];
}
}
}
return $array;
}
/** /**
* @param string $attribute * @param string $attribute
* @param string|null $class * @param string|null $class
+1 -1
View File
@@ -203,7 +203,7 @@ class Container extends BaseObject implements ContainerInterface
{ {
foreach ($this->getPropertyNote($reflect) as $property => $inject) { foreach ($this->getPropertyNote($reflect) as $property => $inject) {
/** @var Inject $inject */ /** @var Inject $inject */
$inject->execute($object, $property); $inject['class']::execute((object)$inject['params'] ,$object, $property);
} }
return $object; return $object;
} }
-20
View File
@@ -52,26 +52,6 @@ class Kiri
private static ?Application $service = null; private static ?Application $service = null;
/**
* @param object $class
* @throws ReflectionException
*/
public static function injectProperty(object $class)
{
$attributes = static::getDi()->getClassReflectionProperty($class::class);
/**
* @var string $property
* @var ReflectionProperty $attribute
*/
foreach ($attributes as $property => $attribute) {
foreach ($attribute->getAttributes() as $item) {
$item->newInstance()->execute($class, $property);
}
}
}
/** /**
* @param $service * @param $service
* *
+2 -2
View File
@@ -175,7 +175,7 @@ if (!function_exists('injectRuntime')) {
$di = Kiri::getDi(); $di = Kiri::getDi();
foreach ($fileLists as $class) { foreach ($fileLists as $class) {
foreach ($di->getTargetNote($class) as $value) { foreach ($di->getTargetNote($class) as $value) {
$value->execute($class); $value['class']::execute((object)$value['params'], $class);
} }
$methods = $di->getMethodAttribute($class); $methods = $di->getMethodAttribute($class);
foreach ($methods as $method => $attribute) { foreach ($methods as $method => $attribute) {
@@ -183,7 +183,7 @@ if (!function_exists('injectRuntime')) {
continue; continue;
} }
foreach ($attribute as $item) { foreach ($attribute as $item) {
$item->execute($class, $method); $value['class']::execute((object)$value['params'], $class, $method);
} }
} }
} }
+1 -1
View File
@@ -587,7 +587,7 @@ class Router extends HttpService implements RouterInterface
continue; continue;
} }
foreach ($attribute as $item) { foreach ($attribute as $item) {
$item->execute($class, $method); $item['class']::execute((object)$item['params'], $class, $method);
} }
} }
} }
+2 -2
View File
@@ -90,7 +90,7 @@ class OnServerWorker extends \Server\Abstracts\Server
$di = Kiri::getDi(); $di = Kiri::getDi();
foreach ($fileLists as $class) { foreach ($fileLists as $class) {
foreach ($di->getTargetNote($class) as $value) { foreach ($di->getTargetNote($class) as $value) {
$value->execute($class); $value['class']::execute((object)$value['params'], $class);
} }
$methods = $di->getMethodAttribute($class); $methods = $di->getMethodAttribute($class);
foreach ($methods as $method => $attribute) { foreach ($methods as $method => $attribute) {
@@ -98,7 +98,7 @@ class OnServerWorker extends \Server\Abstracts\Server
continue; continue;
} }
foreach ($attribute as $item) { foreach ($attribute as $item) {
$item->execute($class, $method); $item['class']::execute((object)$item['params'], $class, $method);
} }
} }
} }
+2 -2
View File
@@ -113,7 +113,7 @@ class OnWorkerStart implements EventDispatcherInterface
$di = Kiri::getDi(); $di = Kiri::getDi();
foreach ($fileLists as $class) { foreach ($fileLists as $class) {
foreach ($di->getTargetNote($class) as $value) { foreach ($di->getTargetNote($class) as $value) {
$value->execute($class); $value['class']::execute((object)$value['params'], $class);
} }
$methods = $di->getMethodAttribute($class); $methods = $di->getMethodAttribute($class);
foreach ($methods as $method => $attribute) { foreach ($methods as $method => $attribute) {
@@ -121,7 +121,7 @@ class OnWorkerStart implements EventDispatcherInterface
continue; continue;
} }
foreach ($attribute as $item) { foreach ($attribute as $item) {
$item->execute($class, $method); $item['class']::execute((object)$item['params'], $class, $method);
} }
} }
} }
+1 -126
View File
@@ -19,122 +19,9 @@ class Annotation extends Component
private Loader $_loader; private Loader $_loader;
private array $_model_sets = [];
private array $_model_gets = [];
private array $_model_relate = [];
/** /**
* @param string $class *
* @param string $setName
* @param string $method
*/ */
public function addSets(string $class, string $setName, string $method)
{
$this->_model_sets[$class][$setName] = $method;
}
/**
* @param string $class
* @param string $setName
* @param string $method
*/
public function addGets(string $class, string $setName, string $method)
{
$this->_model_gets[$class][$setName] = $method;
}
/**
* @param string $class
* @param string $setName
* @param string $method
*/
public function addRelate(string $class, string $setName, string $method)
{
$this->_model_relate[$class][$setName] = $method;
}
/**
* @param $class
* @return array
*/
public function getGets($class): array
{
return $this->_model_gets[$class] ?? [];
}
/**
* @param $class
* @return array
*/
public function getSets($class): array
{
return $this->_model_gets[$class] ?? [];
}
/**
* @param string $class
* @param string|null $setName
* @return array|string|null
*/
public function getGetMethodName(string $class, string $setName = null): array|null|string
{
$gets = $this->_model_gets[$class] ?? null;
if ($gets == null) {
return null;
}
if (empty($setName)) return $gets;
return $gets[$setName] ?? null;
}
public function runGet($name, $value)
{
}
/**
* @param string $class
* @param string|null $method
* @return array|string|null
*/
public function getRelateMethods(string $class, string $method = null): array|null|string
{
$gets = $this->_model_relate[$class] ?? null;
if ($gets == null) {
return null;
}
if (empty($method)) return $gets;
return $gets[$method] ?? null;
}
/**
* @param string $class
* @param string $setName
* @return mixed|null
*/
public function getSetMethodName(string $class, string $setName): ?string
{
if (!isset($this->_model_sets[$class])) {
return null;
}
$lists = $this->_model_sets[$class];
if (isset($lists[$setName])) {
return $lists[$setName];
}
return null;
}
public function init(): void public function init(): void
{ {
$this->_loader = new Loader(); $this->_loader = new Loader();
@@ -160,18 +47,6 @@ class Annotation extends Component
} }
/**
* @param string $className
* @param string $method
* @return array 根据类名获取注解
* 根据类名获取注解
*/
public function getMethods(string $className, string $method = ''): mixed
{
return $this->_loader->getMethod($className, $method);
}
/** /**
* @param object $class * @param object $class
* @throws ReflectionException * @throws ReflectionException
+2 -2
View File
@@ -23,7 +23,7 @@ defined('ASPECT_ERROR') or define('ASPECT_ERROR', 'Aspect annotation must implem
* Aspect constructor. * Aspect constructor.
* @param string $aspect * @param string $aspect
*/ */
public function __construct(public string $aspect) public function __construct(string $aspect)
{ {
} }
@@ -31,7 +31,7 @@ defined('ASPECT_ERROR') or define('ASPECT_ERROR', 'Aspect annotation must implem
/** /**
* @throws Exception * @throws Exception
*/ */
public function execute(mixed $class, mixed $method = ''): bool public static function execute(mixed $params, mixed $class, mixed $method = ''): bool
{ {
return true; return true;
} }
+3 -3
View File
@@ -22,7 +22,7 @@ use Kiri\Kiri;
* Asynchronous constructor. * Asynchronous constructor.
* @param string $name * @param string $name
*/ */
public function __construct(public string $name) public function __construct(string $name)
{ {
} }
@@ -34,10 +34,10 @@ use Kiri\Kiri;
* @return bool * @return bool
* @throws Exception * @throws Exception
*/ */
public function execute(mixed $class, mixed $method = null): bool public static function execute(mixed $params, mixed $class, mixed $method = null): bool
{ {
$async = Kiri::app()->getAsync(); $async = Kiri::app()->getAsync();
$async->addAsync($this->name, $class); $async->addAsync($params->name, $class);
return true; return true;
} }
+2 -3
View File
@@ -4,7 +4,6 @@
namespace Annotation; namespace Annotation;
/** /**
* Class Attribute * Class Attribute
* @package Annotation * @package Annotation
@@ -14,11 +13,11 @@ abstract class Attribute implements IAnnotation
/** /**
* @param mixed $class * @param static $class
* @param mixed|string $method * @param mixed|string $method
* @return mixed * @return mixed
*/ */
public function execute(mixed $class, mixed $method = ''): mixed public static function execute(mixed $params, mixed $class, mixed $method = ''): mixed
{ {
// TODO: Implement execute() method. // TODO: Implement execute() method.
return true; return true;
+3 -3
View File
@@ -21,7 +21,7 @@ use Kiri\Events\EventProvider;
* @param string $name * @param string $name
* @param array $params * @param array $params
*/ */
public function __construct(public string $name, public array $params = []) public function __construct(string $name, array $params = [])
{ {
} }
@@ -32,13 +32,13 @@ use Kiri\Events\EventProvider;
* @return bool * @return bool
* @throws Exception * @throws Exception
*/ */
public function execute(mixed $class, mixed $method = null): bool public static function execute(mixed $params, mixed $class, mixed $method = null): bool
{ {
$pro = di(EventProvider::class); $pro = di(EventProvider::class);
if (is_string($class)) { if (is_string($class)) {
$class = di($class); $class = di($class);
} }
$pro->on($this->name, [$class, $method]); $pro->on($params->name, [$class, $method]);
return true; return true;
} }
+2 -1
View File
@@ -10,11 +10,12 @@ interface IAnnotation
{ {
/** /**
* @param static $params
* @param mixed $class * @param mixed $class
* @param mixed $method * @param mixed $method
* @return mixed * @return mixed
*/ */
public function execute(mixed $class, mixed $method = ''): mixed; public static function execute(mixed $params, mixed $class, mixed $method = ''): mixed;
} }
+13 -13
View File
@@ -23,7 +23,7 @@ use ReflectionProperty;
* @param string $value * @param string $value
* @param array $construct * @param array $construct
*/ */
public function __construct(private string $value, private array $construct = []) public function __construct(string $value, array $construct = [])
{ {
} }
@@ -35,15 +35,15 @@ use ReflectionProperty;
* @throws ReflectionException * @throws ReflectionException
* @throws Exception * @throws Exception
*/ */
public function execute(mixed $class, mixed $method = null): bool public static function execute(mixed $params, mixed $class, mixed $method = null): bool
{ {
if (!($method = $this->getProperty($class, $method))) { if (!($method = static::getProperty($class, $method))) {
return false; return false;
} }
/** @var ReflectionProperty $class */ /** @var ReflectionProperty $class */
$injectValue = $this->parseInjectValue(); $injectValue = static::parseInjectValue($params);
if ($method->isPrivate() || $method->isProtected()) { if ($method->isPrivate() || $method->isProtected()) {
$this->setter($class, $method, $injectValue); static::setter($class, $method, $injectValue);
} else { } else {
$class->{$method->getName()} = $injectValue; $class->{$method->getName()} = $injectValue;
} }
@@ -56,7 +56,7 @@ use ReflectionProperty;
* @param $method * @param $method
* @param $injectValue * @param $injectValue
*/ */
private function setter($class, $method, $injectValue) private static function setter($class, $method, $injectValue)
{ {
$method = 'set' . ucfirst(Str::convertUnderline($method->getName())); $method = 'set' . ucfirst(Str::convertUnderline($method->getName()));
if (!method_exists($class, $method)) { if (!method_exists($class, $method)) {
@@ -72,7 +72,7 @@ use ReflectionProperty;
* @return ReflectionProperty|bool * @return ReflectionProperty|bool
* @throws ReflectionException * @throws ReflectionException
*/ */
private function getProperty($class, $method): ReflectionProperty|bool private static function getProperty($class, $method): ReflectionProperty|bool
{ {
if ($method instanceof ReflectionProperty && !$method->isStatic()) { if ($method instanceof ReflectionProperty && !$method->isStatic()) {
return $method; return $method;
@@ -90,15 +90,15 @@ use ReflectionProperty;
* @return mixed * @return mixed
* @throws Exception * @throws Exception
*/ */
private function parseInjectValue(): mixed private static function parseInjectValue($params): mixed
{ {
if (!Kiri::app()->has($this->value)) { if (!Kiri::app()->has($params->value)) {
if (!empty($this->construct)) { if (!empty($params->construct)) {
return Kiri::getDi()->newObject($this->value, $this->construct); return Kiri::getDi()->newObject($params->value, $params->construct);
} }
return Kiri::getDi()->get($this->value); return Kiri::getDi()->get($params->value);
} else { } else {
return Kiri::app()->get($this->value); return Kiri::app()->get($params->value);
} }
} }
+12 -13
View File
@@ -24,15 +24,8 @@ use Server\Events\OnWorkerExit;
* @param bool $async_reload * @param bool $async_reload
* @throws Exception * @throws Exception
*/ */
public function __construct(public string $service, public ?array $args = [], public bool $async_reload = true) public function __construct(string $service, ?array $args = [], bool $async_reload = true)
{ {
if ($this->async_reload !== true) {
return;
}
$pro = di(EventProvider::class);
$pro->on(OnWorkerExit::class, function () {
di(\Kiri\Di\LocalService::class)->remove($this->service);
},0);
} }
@@ -42,14 +35,20 @@ use Server\Events\OnWorkerExit;
* @return bool * @return bool
* @throws Exception * @throws Exception
*/ */
public function execute(mixed $class, mixed $method = null): bool public static function execute(mixed $params, mixed $class, mixed $method = null): bool
{ {
$class = ['class' => $class]; $class = ['class' => $class];
if (!empty($this->args)) { if (!empty($params->args)) {
$class = array_merge($class, $this->args); $class = array_merge($class, $params->args);
} }
Kiri::set($this->service, $class); if ($params->async_reload !== true) {
return true; // TODO: Change the autogenerated stub $pro = di(EventProvider::class);
$pro->on(OnWorkerExit::class, function () use ($params) {
di(\Kiri\Di\LocalService::class)->remove($params->service);
},0);
}
Kiri::set($params->service, $class);
return true;
} }
} }
+3 -6
View File
@@ -26,10 +26,7 @@ use Annotation\Attribute;
]; ];
public function __construct( public function __construct(array $request, array $response)
public array $request,
public array $response
)
{ {
} }
@@ -39,10 +36,10 @@ use Annotation\Attribute;
* @param mixed|null $method * @param mixed|null $method
* @return array * @return array
*/ */
public function execute(mixed $class, mixed $method = null): array public static function execute(mixed $params, mixed $class, mixed $method = null): array
{ {
// TODO: Implement execute() method. // TODO: Implement execute() method.
return [$this->request, $this->response]; return [$params->request, $params->response];
} }
} }
+2 -2
View File
@@ -23,7 +23,7 @@ use Kiri\Kiri;
* Filter constructor. * Filter constructor.
* @param array $rules * @param array $rules
*/ */
public function __construct(public array $rules) public function __construct(array $rules)
{ {
} }
@@ -33,7 +33,7 @@ use Kiri\Kiri;
* @param mixed|null $method * @param mixed|null $method
* @return bool * @return bool
*/ */
public function execute(mixed $class, mixed $method = null): bool public static function execute(mixed $params, mixed $class, mixed $method = null): bool
{ {
return true; return true;
} }
+17 -18
View File
@@ -7,7 +7,7 @@ namespace Annotation\Route;
use Annotation\Attribute; use Annotation\Attribute;
use Http\Route\MiddlewareManager; use Http\Route\MiddlewareManager;
use ReflectionException; use ReflectionException;
use Http\IInterface\MiddlewareInterface ; use Http\IInterface\MiddlewareInterface;
/** /**
* Class Middleware * Class Middleware
@@ -22,21 +22,8 @@ use Http\IInterface\MiddlewareInterface ;
* @param string|array $middleware * @param string|array $middleware
* @throws * @throws
*/ */
public function __construct(public string|array $middleware) public function __construct(string|array $middleware)
{ {
if (is_string($this->middleware)) {
$this->middleware = [$this->middleware];
}
$array = [];
foreach ($this->middleware as $value) {
$sn = di($value);
if (!($sn instanceof MiddlewareInterface)) {
continue;
}
$array[] = [$sn, 'onHandler'];
}
$this->middleware = $array;
} }
@@ -46,10 +33,22 @@ use Http\IInterface\MiddlewareInterface ;
* @return $this * @return $this
* @throws ReflectionException * @throws ReflectionException
*/ */
public function execute(mixed $class, mixed $method = null): static public static function execute(mixed $params, mixed $class, mixed $method = null): mixed
{ {
MiddlewareManager::addMiddlewares($class, $method, $this->middleware); if (is_string($params->middleware)) {
return $this; $params->middleware = [$params->middleware];
}
$array = [];
foreach ($params->middleware as $value) {
$sn = di($value);
if (!($sn instanceof MiddlewareInterface)) {
continue;
}
$array[] = [$sn, 'onHandler'];
}
MiddlewareManager::addMiddlewares($class, $method, $array);
return parent::execute($params, $class, $method);
} }
+3 -9
View File
@@ -18,13 +18,8 @@ use Kiri\Kiri;
* @param string $method * @param string $method
* @param string $version * @param string $version
*/ */
public function __construct( public function __construct(string $uri, string $method, string $version = 'v.1.0')
public string $uri,
public string $method,
public string $version = 'v.1.0'
)
{ {
$this->method = strtoupper($this->method);
} }
@@ -34,15 +29,14 @@ use Kiri\Kiri;
* @return Router * @return Router
* @throws Exception * @throws Exception
*/ */
public function execute(mixed $class, mixed $method = null): Router public static function execute(mixed $params, mixed $class, mixed $method = null): Router
{ {
// TODO: Implement setHandler() method. // TODO: Implement setHandler() method.
$router = Kiri::app()->getRouter(); $router = Kiri::app()->getRouter();
if (is_string($class)) { if (is_string($class)) {
$class = di($class); $class = di($class);
} }
$router->addRoute($this->uri, [$class, $method], $this->method); $router->addRoute($params->uri, [$class, $method], strtoupper($params->method));
return $router; return $router;
} }
+3 -3
View File
@@ -26,7 +26,7 @@ use Kiri\Kiri;
* @param string|null $uri * @param string|null $uri
* @param string $version * @param string $version
*/ */
public function __construct(public string $event, public ?string $uri = null, public string $version = 'v.1.0') public function __construct(string $event, ?string $uri = null, string $version = 'v.1.0')
{ {
} }
@@ -37,12 +37,12 @@ use Kiri\Kiri;
* @return Router * @return Router
* @throws Exception * @throws Exception
*/ */
public function execute(mixed $class, mixed $method = null): Router public static function execute(mixed $params, mixed $class, mixed $method = null): Router
{ {
// TODO: Implement setHandler() method. // TODO: Implement setHandler() method.
$router = Kiri::app()->getRouter(); $router = Kiri::app()->getRouter();
$path = $this->event . '::' . (is_null($this->uri) ? 'event' : $this->uri); $path = $params->event . '::' . (is_null($params->uri) ? 'event' : $params->uri);
$router->addRoute($path, [di($class), $method], 'sw::socket'); $router->addRoute($path, [di($class), $method], 'sw::socket');
+1 -1
View File
@@ -21,7 +21,7 @@ namespace Annotation;
/** /**
* @param string $only * @param string $only
*/ */
public function __construct(public string $only = Target::ALL) public function __construct(string $only = Target::ALL)
{ {
} }