diff --git a/core/Di/Attributes.php b/core/Di/Attributes.php index b2501100..f46d5a38 100644 --- a/core/Di/Attributes.php +++ b/core/Di/Attributes.php @@ -11,271 +11,288 @@ trait Attributes { - private array $_classTarget = []; - private array $_classMethodNote = []; - private array $_classMethod = []; - private array $_classPropertyNote = []; - private array $_classProperty = []; + private array $_classTarget = []; + private array $_classMethodNote = []; + private array $_classMethod = []; + private array $_classPropertyNote = []; + private array $_classProperty = []; - private array $_mapping = [ - 'attributeClass' => [ - 'className' => [ - 'property' => [ - '' - ], - 'method' => [ + /** + * @param ReflectionClass $class + */ + protected function setTargetNote(ReflectionClass $class) + { + $className = $class->getName(); + if (!isset($this->_classTarget[$className])) { + $this->_classTarget[$className] = []; + } + foreach ($class->getAttributes() as $attribute) { + if (!class_exists($attribute->getName())) { + continue; + } - ] - ] - ] - ]; + $instance = $this->format_annotation($attribute); + + $this->_classTarget[$className][] = $instance; + + $this->setMappingClass($attribute, $className); + } + } - /** - * @param ReflectionClass $class - */ - protected function setTargetNote(ReflectionClass $class) - { - $className = $class->getName(); - if (!isset($this->_classTarget[$className])) { - $this->_classTarget[$className] = []; - } - foreach ($class->getAttributes() as $attribute) { - if (!class_exists($attribute->getName())) { - continue; - } - $this->_classTarget[$className][] = $attribute->newInstance(); - - $this->setMappingClass($attribute, $className); - } - } + /** + * @param ReflectionAttribute $attribute + * @param string $class + */ + private function setMappingClass(ReflectionAttribute $attribute, string $class) + { + if (!isset($this->_mapping[$attribute->getName()])) { + $this->_mapping[$attribute->getName()] = []; + } + if (!isset($this->_mapping[$attribute->getName()][$class])) { + $this->_mapping[$attribute->getName()][$class] = []; + } + } - /** - * @param ReflectionAttribute $attribute - * @param string $class - */ - private function setMappingClass(ReflectionAttribute $attribute, string $class) - { - if (!isset($this->_mapping[$attribute->getName()])) { - $this->_mapping[$attribute->getName()] = []; - } - if (!isset($this->_mapping[$attribute->getName()][$class])) { - $this->_mapping[$attribute->getName()][$class] = []; - } - } + /** + * @param ReflectionAttribute $attribute + * @param string $class + * @param string $method + * @param mixed $instance + */ + private function setMappingMethod(ReflectionAttribute $attribute, string $class, string $method, mixed $instance) + { + $this->setMappingClass($attribute, $class); + + if (!isset($this->_mapping[$attribute->getName()][$class]['method'])) { + $this->_mapping[$attribute->getName()][$class]['method'] = []; + } + $this->_mapping[$attribute->getName()][$class]['method'][] = [$method => $instance]; + } - /** - * @param ReflectionAttribute $attribute - * @param string $class - * @param string $method - * @param mixed $instance - */ - private function setMappingMethod(ReflectionAttribute $attribute, string $class, string $method, mixed $instance) - { - $this->setMappingClass($attribute, $class); + /** + * @param ReflectionAttribute $attribute + * @param string $class + * @param string $property + * @param $instance + */ + private function setMappingProperty(ReflectionAttribute $attribute, string $class, string $property, $instance) + { + $this->setMappingClass($attribute, $class); - if (!isset($this->_mapping[$attribute->getName()][$class]['method'])) { - $this->_mapping[$attribute->getName()][$class]['method'] = []; - } - $this->_mapping[$attribute->getName()][$class]['method'][] = [$method => $instance]; - } + $mapping = $this->_mapping[$attribute->getName()][$class]; + if (!isset($mapping['property'])) { + $mapping['property'] = []; + } + $mapping['property'][] = [$property => $instance]; + $this->_mapping[$attribute->getName()][$class] = $mapping; + } - /** - * @param ReflectionAttribute $attribute - * @param string $class - * @param string $property - * @param $instance - */ - private function setMappingProperty(ReflectionAttribute $attribute, string $class, string $property, $instance) - { - $this->setMappingClass($attribute, $class); - - $mapping = $this->_mapping[$attribute->getName()][$class]; - if (!isset($mapping['property'])) { - $mapping['property'] = []; - } - $mapping['property'][] = [$property => $instance]; - $this->_mapping[$attribute->getName()][$class] = $mapping; - } + /** + * @param mixed $class + * @return array + */ + public function getTargetNote(mixed $class): array + { + if (!is_string($class)) { + $class = $class::class; + } + return $this->_classTarget[$class] ?? []; + } - /** - * @param mixed $class - * @return array - */ - public function getTargetNote(mixed $class): array - { - if (!is_string($class)) { - $class = $class::class; - } - return $this->_classTarget[$class] ?? []; - } + /** + * @param ReflectionClass $class + */ + protected function setMethodNote(ReflectionClass $class) + { + $className = $class->getName(); + $this->_classMethodNote[$className] = $this->_classMethod[$className] = []; + foreach ($class->getMethods() as $ReflectionMethod) { + $this->_classMethod[$className][$ReflectionMethod->getName()] = $ReflectionMethod; + $this->_classMethodNote[$className][$ReflectionMethod->getName()] = []; + foreach ($ReflectionMethod->getAttributes() as $attribute) { + if (!class_exists($attribute->getName())) { + continue; + } + $instance = $this->format_annotation($attribute); + + $this->_classMethodNote[$className][$ReflectionMethod->getName()][] = $instance; + + $this->setMappingMethod($attribute, $className, $ReflectionMethod->getName(), $instance); + } + } + } - /** - * @param ReflectionClass $class - */ - protected function setMethodNote(ReflectionClass $class) - { - $className = $class->getName(); - $this->_classMethodNote[$className] = $this->_classMethod[$className] = []; - foreach ($class->getMethods() as $ReflectionMethod) { - $this->_classMethod[$className][$ReflectionMethod->getName()] = $ReflectionMethod; - $this->_classMethodNote[$className][$ReflectionMethod->getName()] = []; - foreach ($ReflectionMethod->getAttributes() as $attribute) { - if (!class_exists($attribute->getName())) { - continue; - } - $instance = $attribute->newInstance(); - - $this->_classMethodNote[$className][$ReflectionMethod->getName()][] = $instance; - - $this->setMappingMethod($attribute, $className, $ReflectionMethod->getName(), $instance); - } - } - } + /** + * @param string $class + * @param string $method + * @return bool + */ + public function hasMethod(string $class, string $method): bool + { + return isset($this->_classMethod[$class]) && isset($this->_classMethod[$class][$method]); + } - /** - * @param string $class - * @param string $method - * @return bool - */ - public function hasMethod(string $class, string $method): bool - { - return isset($this->_classMethod[$class]) && isset($this->_classMethod[$class][$method]); - } + /** + * @param ReflectionClass $class + * @return array + */ + #[Pure] public function getMethodNote(ReflectionClass $class): array + { + return $this->_classMethodNote[$class->getName()] ?? []; + } - /** - * @param ReflectionClass $class - * @return array - */ - #[Pure] public function getMethodNote(ReflectionClass $class): array - { - return $this->_classMethodNote[$class->getName()] ?? []; - } + /** + * @param ReflectionClass $class + */ + protected function setPropertyNote(ReflectionClass $class) + { + $className = $class->getName(); + $this->_classProperty[$className] = $this->_classPropertyNote[$className] = []; + foreach ($class->getProperties(ReflectionProperty::IS_PRIVATE | ReflectionProperty::IS_PUBLIC | + ReflectionProperty::IS_PROTECTED) as $ReflectionMethod) { + $this->_classProperty[$className][$ReflectionMethod->getName()] = $ReflectionMethod; + foreach ($ReflectionMethod->getAttributes() as $attribute) { + if (!class_exists($attribute->getName())) { + continue; + } + + $instance = $this->format_annotation($attribute); + + $this->_classPropertyNote[$className][$ReflectionMethod->getName()] = $instance; + + $this->setMappingProperty($attribute, $className, $ReflectionMethod->getName(), $instance); + } + } + } - /** - * @param ReflectionClass $class - */ - protected function setPropertyNote(ReflectionClass $class) - { - $className = $class->getName(); - $this->_classProperty[$className] = $this->_classPropertyNote[$className] = []; - foreach ($class->getProperties(ReflectionProperty::IS_PRIVATE | ReflectionProperty::IS_PUBLIC | - ReflectionProperty::IS_PROTECTED) as $ReflectionMethod) { - $this->_classProperty[$className][$ReflectionMethod->getName()] = $ReflectionMethod; - foreach ($ReflectionMethod->getAttributes() as $attribute) { - if (!class_exists($attribute->getName())) { - continue; - } - $instance = $attribute->newInstance(); + /** + * @param \ReflectionAttribute $attribute + * @return array + * @throws \ReflectionException + */ + private function format_annotation(ReflectionAttribute $attribute) + { + $attr = new ReflectionClass($attribute->getName()); - $this->_classPropertyNote[$className][$ReflectionMethod->getName()] = $instance; + $argument = $attribute->getArguments(); - $this->setMappingProperty($attribute, $className, $ReflectionMethod->getName(), $instance); - } - } - } + $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|null $class - * @return array[] - */ - public function getAttributeTrees(string $attribute, string $class = null): array - { - $mapping = $this->_mapping[$attribute] ?? []; - if (empty($mapping) || empty($class)) { - return $mapping; - } - return $mapping[$class] ?? []; - } + /** + * @param string $attribute + * @param string|null $class + * @return array[] + */ + public function getAttributeTrees(string $attribute, string $class = null): array + { + $mapping = $this->_mapping[$attribute] ?? []; + if (empty($mapping) || empty($class)) { + return $mapping; + } + return $mapping[$class] ?? []; + } - /** - * @param string $attribute - * @param string $class - * @param string|null $method - * @return array - */ - public function getMethodByAnnotation(string $attribute, string $class, string $method = null): mixed - { - $class = $this->getAttributeTrees($attribute, $class); - if (empty($class) || !isset($class['method']) || empty($method)) { - return $class['method'] ?? []; - } - foreach ($class['method'] as $value) { - $key = key($value); - if ($method == $key) { - return $value[$key]; - } - } - return null; - } + /** + * @param string $attribute + * @param string $class + * @param string|null $method + * @return array + */ + public function getMethodByAnnotation(string $attribute, string $class, string $method = null): mixed + { + $class = $this->getAttributeTrees($attribute, $class); + if (empty($class) || !isset($class['method']) || empty($method)) { + return $class['method'] ?? []; + } + foreach ($class['method'] as $value) { + $key = key($value); + if ($method == $key) { + return $value[$key]; + } + } + return null; + } - /** - * @param string $attribute - * @param string $class - * @param string $method - * @return mixed - */ - public function getPropertyByAnnotation(string $attribute, string $class, string $method): mixed - { - $class = $this->getAttributeTrees($attribute, $class); - if (empty($class) || !isset($class['property'])) { - return []; - } - foreach ($class['property'] as $value) { - $key = key($value); - if ($method == $key) { - return $value[$key]; - } - } - return null; - } + /** + * @param string $attribute + * @param string $class + * @param string $method + * @return mixed + */ + public function getPropertyByAnnotation(string $attribute, string $class, string $method): mixed + { + $class = $this->getAttributeTrees($attribute, $class); + if (empty($class) || !isset($class['property'])) { + return []; + } + foreach ($class['property'] as $value) { + $key = key($value); + if ($method == $key) { + return $value[$key]; + } + } + return null; + } - /** - * @param ReflectionClass|string $class - * @return array - * @throws \ReflectionException - */ - public function getMethods(ReflectionClass|string $class): array - { - if (is_string($class)) { - $class = $this->getReflect($class); - } - return $this->_classMethod[$class->getName()] ?? []; - } + /** + * @param ReflectionClass|string $class + * @return array + * @throws \ReflectionException + */ + public function getMethods(ReflectionClass|string $class): array + { + if (is_string($class)) { + $class = $this->getReflect($class); + } + return $this->_classMethod[$class->getName()] ?? []; + } - /** - * @param ReflectionClass $class - * @return ReflectionProperty[] - */ - #[Pure] public function getProperty(ReflectionClass $class): array - { - return $this->_classProperty[$class->getName()] ?? []; - } + /** + * @param ReflectionClass $class + * @return ReflectionProperty[] + */ + #[Pure] public function getProperty(ReflectionClass $class): array + { + return $this->_classProperty[$class->getName()] ?? []; + } - /** - * @param ReflectionClass $class - * @return array - */ - #[Pure] public function getPropertyNote(ReflectionClass $class): array - { - return $this->_classPropertyNote[$class->getName()] ?? []; - } + /** + * @param ReflectionClass $class + * @return array + */ + #[Pure] public function getPropertyNote(ReflectionClass $class): array + { + return $this->_classPropertyNote[$class->getName()] ?? []; + } } diff --git a/core/Di/Container.php b/core/Di/Container.php index f86097cd..b3682305 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->execute($object, $property); + $inject['class']::execute((object)$inject['params'] ,$object, $property); } return $object; } diff --git a/core/Kiri.php b/core/Kiri.php index c4c0bb57..26c58777 100644 --- a/core/Kiri.php +++ b/core/Kiri.php @@ -52,26 +52,6 @@ class Kiri 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 * diff --git a/function.php b/function.php index d4eb4ca5..bbc17c09 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->execute($class); + $value['class']::execute((object)$value['params'], $class); } $methods = $di->getMethodAttribute($class); foreach ($methods as $method => $attribute) { @@ -183,7 +183,7 @@ if (!function_exists('injectRuntime')) { continue; } foreach ($attribute as $item) { - $item->execute($class, $method); + $value['class']::execute((object)$value['params'], $class, $method); } } } diff --git a/http-helper/Route/Router.php b/http-helper/Route/Router.php index 7318c26e..85c67e9f 100644 --- a/http-helper/Route/Router.php +++ b/http-helper/Route/Router.php @@ -587,7 +587,7 @@ class Router extends HttpService implements RouterInterface continue; } foreach ($attribute as $item) { - $item->execute($class, $method); + $item['class']::execute((object)$item['params'], $class, $method); } } } diff --git a/http-server/Worker/OnServerWorker.php b/http-server/Worker/OnServerWorker.php index 1baf1794..3feac2f7 100644 --- a/http-server/Worker/OnServerWorker.php +++ b/http-server/Worker/OnServerWorker.php @@ -90,7 +90,7 @@ class OnServerWorker extends \Server\Abstracts\Server $di = Kiri::getDi(); foreach ($fileLists as $class) { foreach ($di->getTargetNote($class) as $value) { - $value->execute($class); + $value['class']::execute((object)$value['params'], $class); } $methods = $di->getMethodAttribute($class); foreach ($methods as $method => $attribute) { @@ -98,7 +98,7 @@ class OnServerWorker extends \Server\Abstracts\Server continue; } foreach ($attribute as $item) { - $item->execute($class, $method); + $item['class']::execute((object)$item['params'], $class, $method); } } } diff --git a/http-server/Worker/OnWorkerStart.php b/http-server/Worker/OnWorkerStart.php index d8965e58..ea5d25ef 100644 --- a/http-server/Worker/OnWorkerStart.php +++ b/http-server/Worker/OnWorkerStart.php @@ -113,7 +113,7 @@ class OnWorkerStart implements EventDispatcherInterface $di = Kiri::getDi(); foreach ($fileLists as $class) { foreach ($di->getTargetNote($class) as $value) { - $value->execute($class); + $value['class']::execute((object)$value['params'], $class); } $methods = $di->getMethodAttribute($class); foreach ($methods as $method => $attribute) { @@ -121,7 +121,7 @@ class OnWorkerStart implements EventDispatcherInterface continue; } foreach ($attribute as $item) { - $item->execute($class, $method); + $item['class']::execute((object)$item['params'], $class, $method); } } } diff --git a/note/Annotation.php b/note/Annotation.php index 4f53cd0e..cf6924c0 100644 --- a/note/Annotation.php +++ b/note/Annotation.php @@ -19,122 +19,9 @@ class Annotation extends Component 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 { $this->_loader = new Loader(); @@ -159,19 +46,7 @@ class Annotation extends Component return $this->_loader = $loader; } - - /** - * @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 * @throws ReflectionException diff --git a/note/Aspect.php b/note/Aspect.php index 8ec5d419..919d2ae3 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(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 */ - public function execute(mixed $class, mixed $method = ''): bool + public static function execute(mixed $params, mixed $class, mixed $method = ''): bool { return true; } diff --git a/note/Asynchronous.php b/note/Asynchronous.php index 0bdca270..10767df5 100644 --- a/note/Asynchronous.php +++ b/note/Asynchronous.php @@ -22,7 +22,7 @@ use Kiri\Kiri; * Asynchronous constructor. * @param string $name */ - public function __construct(public string $name) + public function __construct(string $name) { } @@ -34,10 +34,10 @@ use Kiri\Kiri; * @return bool * @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->addAsync($this->name, $class); + $async->addAsync($params->name, $class); return true; } diff --git a/note/Attribute.php b/note/Attribute.php index 3948f36c..b8f86049 100644 --- a/note/Attribute.php +++ b/note/Attribute.php @@ -4,7 +4,6 @@ namespace Annotation; - /** * Class Attribute * @package Annotation @@ -13,12 +12,12 @@ abstract class Attribute implements IAnnotation { - /** - * @param mixed $class - * @param mixed|string $method - * @return mixed - */ - public function execute(mixed $class, mixed $method = ''): mixed + /** + * @param static $class + * @param mixed|string $method + * @return mixed + */ + public static function execute(mixed $params, mixed $class, mixed $method = ''): mixed { // TODO: Implement execute() method. return true; diff --git a/note/Event.php b/note/Event.php index e14edae1..8f3ca5a6 100644 --- a/note/Event.php +++ b/note/Event.php @@ -21,7 +21,7 @@ use Kiri\Events\EventProvider; * @param string $name * @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 * @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); if (is_string($class)) { $class = di($class); } - $pro->on($this->name, [$class, $method]); + $pro->on($params->name, [$class, $method]); return true; } diff --git a/note/IAnnotation.php b/note/IAnnotation.php index 0887ce42..6030a330 100644 --- a/note/IAnnotation.php +++ b/note/IAnnotation.php @@ -9,12 +9,13 @@ use Closure; interface IAnnotation { - /** - * @param mixed $class - * @param mixed $method - * @return mixed - */ - public function execute(mixed $class, mixed $method = ''): mixed; + /** + * @param static $params + * @param mixed $class + * @param mixed $method + * @return mixed + */ + public static function execute(mixed $params, mixed $class, mixed $method = ''): mixed; } diff --git a/note/Inject.php b/note/Inject.php index 509fc4c3..002a9514 100644 --- a/note/Inject.php +++ b/note/Inject.php @@ -23,7 +23,7 @@ use ReflectionProperty; * @param string $value * @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 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; } /** @var ReflectionProperty $class */ - $injectValue = $this->parseInjectValue(); + $injectValue = static::parseInjectValue($params); if ($method->isPrivate() || $method->isProtected()) { - $this->setter($class, $method, $injectValue); + static::setter($class, $method, $injectValue); } else { $class->{$method->getName()} = $injectValue; } @@ -56,7 +56,7 @@ use ReflectionProperty; * @param $method * @param $injectValue */ - private function setter($class, $method, $injectValue) + private static function setter($class, $method, $injectValue) { $method = 'set' . ucfirst(Str::convertUnderline($method->getName())); if (!method_exists($class, $method)) { @@ -72,7 +72,7 @@ use ReflectionProperty; * @return ReflectionProperty|bool * @throws ReflectionException */ - private function getProperty($class, $method): ReflectionProperty|bool + private static function getProperty($class, $method): ReflectionProperty|bool { if ($method instanceof ReflectionProperty && !$method->isStatic()) { return $method; @@ -90,15 +90,15 @@ use ReflectionProperty; * @return mixed * @throws Exception */ - private function parseInjectValue(): mixed + private static function parseInjectValue($params): mixed { - if (!Kiri::app()->has($this->value)) { - if (!empty($this->construct)) { - return Kiri::getDi()->newObject($this->value, $this->construct); + if (!Kiri::app()->has($params->value)) { + if (!empty($params->construct)) { + return Kiri::getDi()->newObject($params->value, $params->construct); } - return Kiri::getDi()->get($this->value); + return Kiri::getDi()->get($params->value); } else { - return Kiri::app()->get($this->value); + return Kiri::app()->get($params->value); } } diff --git a/note/LocalService.php b/note/LocalService.php index c922fee7..dc9e1d34 100644 --- a/note/LocalService.php +++ b/note/LocalService.php @@ -24,15 +24,8 @@ use Server\Events\OnWorkerExit; * @param bool $async_reload * @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 * @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]; - if (!empty($this->args)) { - $class = array_merge($class, $this->args); + if (!empty($params->args)) { + $class = array_merge($class, $params->args); } - Kiri::set($this->service, $class); - return true; // TODO: Change the autogenerated stub + 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); + return true; } } diff --git a/note/Route/Document.php b/note/Route/Document.php index 3a92b96b..e8ee56db 100644 --- a/note/Route/Document.php +++ b/note/Route/Document.php @@ -26,10 +26,7 @@ use Annotation\Attribute; ]; - public function __construct( - public array $request, - public array $response - ) + public function __construct(array $request, array $response) { } @@ -39,10 +36,10 @@ use Annotation\Attribute; * @param mixed|null $method * @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. - return [$this->request, $this->response]; + return [$params->request, $params->response]; } } diff --git a/note/Route/Filter.php b/note/Route/Filter.php index 338a44f8..ce4b4b7b 100644 --- a/note/Route/Filter.php +++ b/note/Route/Filter.php @@ -23,7 +23,7 @@ use Kiri\Kiri; * Filter constructor. * @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 * @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; } diff --git a/note/Route/Middleware.php b/note/Route/Middleware.php index 45594a85..ad17b8dc 100644 --- a/note/Route/Middleware.php +++ b/note/Route/Middleware.php @@ -7,7 +7,7 @@ namespace Annotation\Route; use Annotation\Attribute; use Http\Route\MiddlewareManager; use ReflectionException; -use Http\IInterface\MiddlewareInterface ; +use Http\IInterface\MiddlewareInterface; /** * Class Middleware @@ -22,34 +22,33 @@ use Http\IInterface\MiddlewareInterface ; * @param string|array $middleware * @throws */ - public function __construct(public string|array $middleware) + public function __construct(string|array $middleware) { - if (is_string($this->middleware)) { - $this->middleware = [$this->middleware]; - } + } + + /** + * @param mixed $class + * @param mixed|null $method + * @return $this + * @throws ReflectionException + */ + public static function execute(mixed $params, mixed $class, mixed $method = null): mixed + { + if (is_string($params->middleware)) { + $params->middleware = [$params->middleware]; + } $array = []; - foreach ($this->middleware as $value) { + foreach ($params->middleware as $value) { $sn = di($value); if (!($sn instanceof MiddlewareInterface)) { continue; } $array[] = [$sn, 'onHandler']; } - $this->middleware = $array; - } + MiddlewareManager::addMiddlewares($class, $method, $array); - - /** - * @param mixed $class - * @param mixed|null $method - * @return $this - * @throws ReflectionException - */ - public function execute(mixed $class, mixed $method = null): static - { - MiddlewareManager::addMiddlewares($class, $method, $this->middleware); - return $this; + return parent::execute($params, $class, $method); } diff --git a/note/Route/Route.php b/note/Route/Route.php index 3e5bc99c..dc895d16 100644 --- a/note/Route/Route.php +++ b/note/Route/Route.php @@ -12,39 +12,33 @@ use Kiri\Kiri; #[\Attribute(\Attribute::TARGET_METHOD)] class Route extends Attribute { - /** - * Route constructor. - * @param string $uri - * @param string $method - * @param string $version - */ - public function __construct( - public string $uri, - public string $method, - public string $version = 'v.1.0' - ) - { - $this->method = strtoupper($this->method); - } + /** + * Route constructor. + * @param string $uri + * @param string $method + * @param string $version + */ + public function __construct(string $uri, string $method, string $version = 'v.1.0') + { + } - /** - * @param mixed $class - * @param mixed|null $method - * @return Router - * @throws Exception - */ - 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($this->uri, [$class, $method], $this->method); - return $router; - } + /** + * @param mixed $class + * @param mixed|null $method + * @return Router + * @throws Exception + */ + public static function execute(mixed $params, 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)); + return $router; + } } diff --git a/note/Route/Socket.php b/note/Route/Socket.php index 8ff77172..efdf7dfb 100644 --- a/note/Route/Socket.php +++ b/note/Route/Socket.php @@ -26,7 +26,7 @@ use Kiri\Kiri; * @param string|null $uri * @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 * @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. $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'); diff --git a/note/Target.php b/note/Target.php index 0042540c..51bcc401 100644 --- a/note/Target.php +++ b/note/Target.php @@ -21,7 +21,7 @@ namespace Annotation; /** * @param string $only */ - public function __construct(public string $only = Target::ALL) + public function __construct(string $only = Target::ALL) { }