From c06f25fcf543eae551a535d4ebd469f58333b31a Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Sun, 29 Aug 2021 05:53:47 +0800 Subject: [PATCH] 111 --- core/Di/Attributes.php | 36 +------ core/Di/Container.php | 2 +- function.php | 4 +- http-helper/Route/Node.php | 2 +- http-server/Worker/OnWorkerStart.php | 4 +- note/Aspect.php | 4 +- note/Asynchronous.php | 6 +- note/Attribute.php | 2 +- note/Event.php | 46 ++++---- note/IAnnotation.php | 2 +- note/Inject.php | 150 +++++++++++++-------------- note/LocalService.php | 40 +++---- note/Route/Document.php | 12 --- note/Route/Filter.php | 10 -- note/Route/Middleware.php | 31 +++--- note/Route/Route.php | 6 +- 16 files changed, 152 insertions(+), 205 deletions(-) diff --git a/core/Di/Attributes.php b/core/Di/Attributes.php index f914df9c..d008bb10 100644 --- a/core/Di/Attributes.php +++ b/core/Di/Attributes.php @@ -33,7 +33,7 @@ trait Attributes continue; } - $instance = $this->format_annotation($attribute); + $instance = $attribute->newInstance(); $this->_classTarget[$className][] = $instance; @@ -120,7 +120,7 @@ trait Attributes if (!class_exists($attribute->getName())) { continue; } - $instance = $this->format_annotation($attribute); + $instance = $attribute->newInstance(); $this->_classMethodNote[$className][$ReflectionMethod->getName()][] = $instance; @@ -166,7 +166,7 @@ trait Attributes continue; } - $instance = $this->format_annotation($attribute); + $instance = $attribute->newInstance(); $this->_classPropertyNote[$className][$ReflectionMethod->getName()] = $instance; @@ -176,36 +176,6 @@ trait Attributes } - /** - * @param \ReflectionAttribute $attribute - * @return array - * @throws \ReflectionException - */ - private function format_annotation(ReflectionAttribute $attribute) - { - $reflection_class = new ReflectionClass($attribute->getName()); - - $argument = $attribute->getArguments(); - - $array = ['class' => $attribute->getName(), 'params' => []]; - foreach ($reflection_class->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]; - } - } - } - - unset($reflection_class); - - return $array; - } - - /** * @param string $attribute * @param string|null $class diff --git a/core/Di/Container.php b/core/Di/Container.php index b3682305..02bef992 100644 --- a/core/Di/Container.php +++ b/core/Di/Container.php @@ -203,7 +203,7 @@ class Container extends BaseObject implements ContainerInterface { foreach ($this->getPropertyNote($reflect) as $property => $inject) { /** @var Inject $inject */ - $inject['class']::execute((object)$inject['params'] ,$object, $property); + $inject->execute($object, $property); } return $object; } diff --git a/function.php b/function.php index bbc17c09..d4eb4ca5 100644 --- a/function.php +++ b/function.php @@ -175,7 +175,7 @@ if (!function_exists('injectRuntime')) { $di = Kiri::getDi(); foreach ($fileLists as $class) { foreach ($di->getTargetNote($class) as $value) { - $value['class']::execute((object)$value['params'], $class); + $value->execute($class); } $methods = $di->getMethodAttribute($class); foreach ($methods as $method => $attribute) { @@ -183,7 +183,7 @@ if (!function_exists('injectRuntime')) { continue; } foreach ($attribute as $item) { - $value['class']::execute((object)$value['params'], $class, $method); + $item->execute($class, $method); } } } diff --git a/http-helper/Route/Node.php b/http-helper/Route/Node.php index 8619bd6b..51c410a0 100644 --- a/http-helper/Route/Node.php +++ b/http-helper/Route/Node.php @@ -231,7 +231,7 @@ class Node if (empty($aspect)) { return null; } - return di($aspect['params']['aspect']); + return di($aspect->aspect); } diff --git a/http-server/Worker/OnWorkerStart.php b/http-server/Worker/OnWorkerStart.php index 4e698f96..ed253265 100644 --- a/http-server/Worker/OnWorkerStart.php +++ b/http-server/Worker/OnWorkerStart.php @@ -81,7 +81,7 @@ class OnWorkerStart implements EventDispatcherInterface $di = Kiri::getDi(); foreach ($fileLists as $class) { foreach ($di->getTargetNote($class) as $value) { - $value['class']::execute((object)$value['params'], $class); + $value->execute($class); } $methods = $di->getMethodAttribute($class); foreach ($methods as $method => $attribute) { @@ -89,7 +89,7 @@ class OnWorkerStart implements EventDispatcherInterface continue; } foreach ($attribute as $item) { - $item['class']::execute((object)$item['params'], $class, $method); + $item->execute($class, $method); } } } diff --git a/note/Aspect.php b/note/Aspect.php index 919d2ae3..d63e9233 100644 --- a/note/Aspect.php +++ b/note/Aspect.php @@ -23,7 +23,7 @@ defined('ASPECT_ERROR') or define('ASPECT_ERROR', 'Aspect annotation must implem * Aspect constructor. * @param string $aspect */ - public function __construct(string $aspect) + public function __construct(public string $aspect) { } @@ -31,7 +31,7 @@ defined('ASPECT_ERROR') or define('ASPECT_ERROR', 'Aspect annotation must implem /** * @throws Exception */ - public static function execute(mixed $params, mixed $class, mixed $method = ''): bool + public function execute(mixed $class, mixed $method = ''): bool { return true; } diff --git a/note/Asynchronous.php b/note/Asynchronous.php index 10767df5..0bdca270 100644 --- a/note/Asynchronous.php +++ b/note/Asynchronous.php @@ -22,7 +22,7 @@ use Kiri\Kiri; * Asynchronous constructor. * @param string $name */ - public function __construct(string $name) + public function __construct(public string $name) { } @@ -34,10 +34,10 @@ use Kiri\Kiri; * @return bool * @throws Exception */ - public static function execute(mixed $params, mixed $class, mixed $method = null): bool + public function execute(mixed $class, mixed $method = null): bool { $async = Kiri::app()->getAsync(); - $async->addAsync($params->name, $class); + $async->addAsync($this->name, $class); return true; } diff --git a/note/Attribute.php b/note/Attribute.php index b8f86049..57753ba7 100644 --- a/note/Attribute.php +++ b/note/Attribute.php @@ -17,7 +17,7 @@ abstract class Attribute implements IAnnotation * @param mixed|string $method * @return mixed */ - public static function execute(mixed $params, mixed $class, mixed $method = ''): mixed + public function execute(mixed $class, mixed $method = ''): mixed { // TODO: Implement execute() method. return true; diff --git a/note/Event.php b/note/Event.php index 8f3ca5a6..92c50934 100644 --- a/note/Event.php +++ b/note/Event.php @@ -16,30 +16,30 @@ use Kiri\Events\EventProvider; { - /** - * Event constructor. - * @param string $name - * @param array $params - */ - public function __construct(string $name, array $params = []) - { - } + /** + * Event constructor. + * @param string $name + * @param array $params + */ + public function __construct(public string $name, public array $params = []) + { + } - /** - * @param mixed $class - * @param mixed|null $method - * @return bool - * @throws Exception - */ - public static function execute(mixed $params, mixed $class, mixed $method = null): bool - { - $pro = di(EventProvider::class); - if (is_string($class)) { - $class = di($class); - } - $pro->on($params->name, [$class, $method]); - return true; - } + /** + * @param mixed $class + * @param mixed|null $method + * @return bool + * @throws Exception + */ + public function execute(mixed $class, mixed $method = null): bool + { + $pro = di(EventProvider::class); + if (is_string($class)) { + $class = di($class); + } + $pro->on($this->name, [$class, $method]); + return true; + } } diff --git a/note/IAnnotation.php b/note/IAnnotation.php index 6030a330..c1caf314 100644 --- a/note/IAnnotation.php +++ b/note/IAnnotation.php @@ -15,7 +15,7 @@ interface IAnnotation * @param mixed $method * @return mixed */ - public static function execute(mixed $params, mixed $class, mixed $method = ''): mixed; + public function execute(mixed $class, mixed $method = ''): mixed; } diff --git a/note/Inject.php b/note/Inject.php index 002a9514..4a462cf7 100644 --- a/note/Inject.php +++ b/note/Inject.php @@ -18,88 +18,88 @@ use ReflectionProperty; { - /** - * Inject constructor. - * @param string $value - * @param array $construct - */ - public function __construct(string $value, array $construct = []) - { - } + /** + * Inject constructor. + * @param string $value + * @param array $construct + */ + public function __construct(public string $value, public array $construct = []) + { + } - /** - * @param mixed $class - * @param mixed|null $method - * @return bool - * @throws ReflectionException - * @throws Exception - */ - public static function execute(mixed $params, mixed $class, mixed $method = null): bool - { - if (!($method = static::getProperty($class, $method))) { - return false; - } - /** @var ReflectionProperty $class */ - $injectValue = static::parseInjectValue($params); - if ($method->isPrivate() || $method->isProtected()) { - static::setter($class, $method, $injectValue); - } else { - $class->{$method->getName()} = $injectValue; - } - return true; - } + /** + * @param mixed $class + * @param mixed|null $method + * @return bool + * @throws ReflectionException + * @throws Exception + */ + public function execute(mixed $class, mixed $method = null): bool + { + if (!($method = $this->getProperty($class, $method))) { + return false; + } + /** @var ReflectionProperty $class */ + $injectValue = static::parseInjectValue(); + if ($method->isPrivate() || $method->isProtected()) { + $this->setter($class, $method, $injectValue); + } else { + $class->{$method->getName()} = $injectValue; + } + return true; + } - /** - * @param $class - * @param $method - * @param $injectValue - */ - private static function setter($class, $method, $injectValue) - { - $method = 'set' . ucfirst(Str::convertUnderline($method->getName())); - if (!method_exists($class, $method)) { - return; - } - $class->$method($injectValue); - } + /** + * @param $class + * @param $method + * @param $injectValue + */ + private function setter($class, $method, $injectValue) + { + $method = 'set' . ucfirst(Str::convertUnderline($method->getName())); + if (!method_exists($class, $method)) { + return; + } + $class->$method($injectValue); + } - /** - * @param $class - * @param $method - * @return ReflectionProperty|bool - * @throws ReflectionException - */ - private static function getProperty($class, $method): ReflectionProperty|bool - { - if ($method instanceof ReflectionProperty && !$method->isStatic()) { - return $method; - } - if (is_object($class)) $class = $class::class; - $method = Kiri::getDi()->getClassReflectionProperty($class, $method); - if (!$method || $method->isStatic()) { - return false; - } - return $method; - } + /** + * @param $class + * @param $method + * @return ReflectionProperty|bool + * @throws ReflectionException + */ + private function getProperty($class, $method): ReflectionProperty|bool + { + if ($method instanceof ReflectionProperty && !$method->isStatic()) { + return $method; + } + if (is_object($class)) $class = $class::class; + $method = Kiri::getDi()->getClassReflectionProperty($class, $method); + if (!$method || $method->isStatic()) { + return false; + } + return $method; + } - /** - * @return mixed - * @throws Exception - */ - private static function parseInjectValue($params): mixed - { - if (!Kiri::app()->has($params->value)) { - if (!empty($params->construct)) { - return Kiri::getDi()->newObject($params->value, $params->construct); - } - return Kiri::getDi()->get($params->value); - } else { - return Kiri::app()->get($params->value); - } - } + /** + * @return mixed + * @throws Exception + */ + private function parseInjectValue(): mixed + { + if (!Kiri::app()->has($this->value)) { + if (!empty($this->construct)) { + return Kiri::getDi()->newObject($this->value, $this->construct); + } + return Kiri::getDi()->get($this->value); + } else { + return Kiri::app()->get($this->value); + } + } } diff --git a/note/LocalService.php b/note/LocalService.php index dc9e1d34..ef78aa4f 100644 --- a/note/LocalService.php +++ b/note/LocalService.php @@ -17,16 +17,22 @@ use Server\Events\OnWorkerExit; { - /** - * LocalService constructor. - * @param string $service - * @param array|null $args - * @param bool $async_reload - * @throws Exception - */ - public function __construct(string $service, ?array $args = [], bool $async_reload = true) - { - } + /** + * LocalService constructor. + * @param string $service + * @param array|null $args + * @param bool $async_reload + * @throws Exception + */ + public function __construct(public string $service, public ?array $args = [], public bool $async_reload = true) + { + if ($this->async_reload !== true) { + $pro = di(EventProvider::class); + $pro->on(OnWorkerExit::class, function () { + di(\Kiri\Di\LocalService::class)->remove($this->service); + }, 0); + } + } /** @@ -35,19 +41,13 @@ use Server\Events\OnWorkerExit; * @return bool * @throws Exception */ - public static function execute(mixed $params, mixed $class, mixed $method = null): bool + public function execute(mixed $class, mixed $method = null): bool { $class = ['class' => $class]; - if (!empty($params->args)) { - $class = array_merge($class, $params->args); + if (!empty($this->args)) { + $class = array_merge($class, $this->args); } - if ($params->async_reload !== true) { - $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); + Kiri::set($this->service, $class); return true; } diff --git a/note/Route/Document.php b/note/Route/Document.php index 0114cc65..460bf163 100644 --- a/note/Route/Document.php +++ b/note/Route/Document.php @@ -31,16 +31,4 @@ use Annotation\Attribute; } - /** - * @param static $params - * @param mixed $class - * @param mixed|null $method - * @return array - */ - public static function execute(mixed $params, mixed $class, mixed $method = null): array - { - // TODO: Implement execute() method. - return [$params->request, $params->response]; - } - } diff --git a/note/Route/Filter.php b/note/Route/Filter.php index ce4b4b7b..f2aee9fb 100644 --- a/note/Route/Filter.php +++ b/note/Route/Filter.php @@ -28,15 +28,5 @@ use Kiri\Kiri; } - /** - * @param mixed $class - * @param mixed|null $method - * @return bool - */ - public static function execute(mixed $params, mixed $class, mixed $method = null): bool - { - return true; - } - } diff --git a/note/Route/Middleware.php b/note/Route/Middleware.php index a2d71896..69af2bbc 100644 --- a/note/Route/Middleware.php +++ b/note/Route/Middleware.php @@ -22,8 +22,19 @@ use Http\IInterface\MiddlewareInterface; * @param string|array $middleware * @throws */ - public function __construct(string|array $middleware) + public function __construct(public 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']; + } } @@ -34,22 +45,10 @@ use Http\IInterface\MiddlewareInterface; * @return $this * @throws ReflectionException */ - public static function execute(mixed $params, mixed $class, mixed $method = null): mixed + public function execute(mixed $class, mixed $method = null): mixed { - if (is_string($params->middleware)) { - $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); + MiddlewareManager::addMiddlewares($class, $method, $this->middleware); + return parent::execute($class, $method); } diff --git a/note/Route/Route.php b/note/Route/Route.php index ebab2cb4..2ee316fb 100644 --- a/note/Route/Route.php +++ b/note/Route/Route.php @@ -18,7 +18,7 @@ use Kiri\Kiri; * @param string $method * @param string $version */ - public function __construct(string $uri, string $method, string $version = 'v.1.0') + public function __construct(public string $uri,public string $method,public string $version = 'v.1.0') { } @@ -31,14 +31,14 @@ use Kiri\Kiri; * @throws \Kiri\Exception\NotFindClassException * @throws \ReflectionException */ - public static function execute(mixed $params, mixed $class, mixed $method = null): Router + public function execute(mixed $class, mixed $method = null): Router { // TODO: Implement setHandler() method. $router = Kiri::app()->getRouter(); if (is_string($class)) { $class = di($class); } - $router->addRoute($params->uri, [$class, $method], strtoupper($params->method)); + $router->addRoute($this->uri, [$class, $method], strtoupper($this->method)); return $router; }