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
+246 -229
View File
@@ -11,271 +11,288 @@ trait Attributes
{ {
private array $_classTarget = []; private array $_classTarget = [];
private array $_classMethodNote = []; private array $_classMethodNote = [];
private array $_classMethod = []; private array $_classMethod = [];
private array $_classPropertyNote = []; private array $_classPropertyNote = [];
private array $_classProperty = []; private array $_classProperty = [];
private array $_mapping = [ /**
'attributeClass' => [ * @param ReflectionClass $class
'className' => [ */
'property' => [ protected function setTargetNote(ReflectionClass $class)
'' {
], $className = $class->getName();
'method' => [ 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 * @param ReflectionAttribute $attribute
*/ * @param string $class
protected function setTargetNote(ReflectionClass $class) */
{ private function setMappingClass(ReflectionAttribute $attribute, string $class)
$className = $class->getName(); {
if (!isset($this->_classTarget[$className])) { if (!isset($this->_mapping[$attribute->getName()])) {
$this->_classTarget[$className] = []; $this->_mapping[$attribute->getName()] = [];
} }
foreach ($class->getAttributes() as $attribute) { if (!isset($this->_mapping[$attribute->getName()][$class])) {
if (!class_exists($attribute->getName())) { $this->_mapping[$attribute->getName()][$class] = [];
continue; }
} }
$this->_classTarget[$className][] = $attribute->newInstance();
$this->setMappingClass($attribute, $className);
}
}
/** /**
* @param ReflectionAttribute $attribute * @param ReflectionAttribute $attribute
* @param string $class * @param string $class
*/ * @param string $method
private function setMappingClass(ReflectionAttribute $attribute, string $class) * @param mixed $instance
{ */
if (!isset($this->_mapping[$attribute->getName()])) { private function setMappingMethod(ReflectionAttribute $attribute, string $class, string $method, mixed $instance)
$this->_mapping[$attribute->getName()] = []; {
} $this->setMappingClass($attribute, $class);
if (!isset($this->_mapping[$attribute->getName()][$class])) {
$this->_mapping[$attribute->getName()][$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 ReflectionAttribute $attribute
* @param string $class * @param string $class
* @param string $method * @param string $property
* @param mixed $instance * @param $instance
*/ */
private function setMappingMethod(ReflectionAttribute $attribute, string $class, string $method, mixed $instance) private function setMappingProperty(ReflectionAttribute $attribute, string $class, string $property, $instance)
{ {
$this->setMappingClass($attribute, $class); $this->setMappingClass($attribute, $class);
if (!isset($this->_mapping[$attribute->getName()][$class]['method'])) { $mapping = $this->_mapping[$attribute->getName()][$class];
$this->_mapping[$attribute->getName()][$class]['method'] = []; if (!isset($mapping['property'])) {
} $mapping['property'] = [];
$this->_mapping[$attribute->getName()][$class]['method'][] = [$method => $instance]; }
} $mapping['property'][] = [$property => $instance];
$this->_mapping[$attribute->getName()][$class] = $mapping;
}
/** /**
* @param ReflectionAttribute $attribute * @param mixed $class
* @param string $class * @return array
* @param string $property */
* @param $instance public function getTargetNote(mixed $class): array
*/ {
private function setMappingProperty(ReflectionAttribute $attribute, string $class, string $property, $instance) if (!is_string($class)) {
{ $class = $class::class;
$this->setMappingClass($attribute, $class); }
return $this->_classTarget[$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 * @param ReflectionClass $class
* @return array */
*/ protected function setMethodNote(ReflectionClass $class)
public function getTargetNote(mixed $class): array {
{ $className = $class->getName();
if (!is_string($class)) { $this->_classMethodNote[$className] = $this->_classMethod[$className] = [];
$class = $class::class; foreach ($class->getMethods() as $ReflectionMethod) {
} $this->_classMethod[$className][$ReflectionMethod->getName()] = $ReflectionMethod;
return $this->_classTarget[$class] ?? []; $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 * @param string $class
*/ * @param string $method
protected function setMethodNote(ReflectionClass $class) * @return bool
{ */
$className = $class->getName(); public function hasMethod(string $class, string $method): bool
$this->_classMethodNote[$className] = $this->_classMethod[$className] = []; {
foreach ($class->getMethods() as $ReflectionMethod) { return isset($this->_classMethod[$class]) && isset($this->_classMethod[$class][$method]);
$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 ReflectionClass $class
* @param string $method * @return array
* @return bool */
*/ #[Pure] public function getMethodNote(ReflectionClass $class): array
public function hasMethod(string $class, string $method): bool {
{ return $this->_classMethodNote[$class->getName()] ?? [];
return isset($this->_classMethod[$class]) && isset($this->_classMethod[$class][$method]); }
}
/** /**
* @param ReflectionClass $class * @param ReflectionClass $class
* @return array */
*/ protected function setPropertyNote(ReflectionClass $class)
#[Pure] public function getMethodNote(ReflectionClass $class): array {
{ $className = $class->getName();
return $this->_classMethodNote[$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 * @param \ReflectionAttribute $attribute
*/ * @return array
protected function setPropertyNote(ReflectionClass $class) * @throws \ReflectionException
{ */
$className = $class->getName(); private function format_annotation(ReflectionAttribute $attribute)
$this->_classProperty[$className] = $this->_classPropertyNote[$className] = []; {
foreach ($class->getProperties(ReflectionProperty::IS_PRIVATE | ReflectionProperty::IS_PUBLIC | $attr = new ReflectionClass($attribute->getName());
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();
$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 $attribute
* @param string|null $class * @param string|null $class
* @return array[] * @return array[]
*/ */
public function getAttributeTrees(string $attribute, string $class = null): array public function getAttributeTrees(string $attribute, string $class = null): array
{ {
$mapping = $this->_mapping[$attribute] ?? []; $mapping = $this->_mapping[$attribute] ?? [];
if (empty($mapping) || empty($class)) { if (empty($mapping) || empty($class)) {
return $mapping; return $mapping;
} }
return $mapping[$class] ?? []; return $mapping[$class] ?? [];
} }
/** /**
* @param string $attribute * @param string $attribute
* @param string $class * @param string $class
* @param string|null $method * @param string|null $method
* @return array * @return array
*/ */
public function getMethodByAnnotation(string $attribute, string $class, string $method = null): mixed public function getMethodByAnnotation(string $attribute, string $class, string $method = null): mixed
{ {
$class = $this->getAttributeTrees($attribute, $class); $class = $this->getAttributeTrees($attribute, $class);
if (empty($class) || !isset($class['method']) || empty($method)) { if (empty($class) || !isset($class['method']) || empty($method)) {
return $class['method'] ?? []; return $class['method'] ?? [];
} }
foreach ($class['method'] as $value) { foreach ($class['method'] as $value) {
$key = key($value); $key = key($value);
if ($method == $key) { if ($method == $key) {
return $value[$key]; return $value[$key];
} }
} }
return null; return null;
} }
/** /**
* @param string $attribute * @param string $attribute
* @param string $class * @param string $class
* @param string $method * @param string $method
* @return mixed * @return mixed
*/ */
public function getPropertyByAnnotation(string $attribute, string $class, string $method): mixed public function getPropertyByAnnotation(string $attribute, string $class, string $method): mixed
{ {
$class = $this->getAttributeTrees($attribute, $class); $class = $this->getAttributeTrees($attribute, $class);
if (empty($class) || !isset($class['property'])) { if (empty($class) || !isset($class['property'])) {
return []; return [];
} }
foreach ($class['property'] as $value) { foreach ($class['property'] as $value) {
$key = key($value); $key = key($value);
if ($method == $key) { if ($method == $key) {
return $value[$key]; return $value[$key];
} }
} }
return null; return null;
} }
/** /**
* @param ReflectionClass|string $class * @param ReflectionClass|string $class
* @return array * @return array
* @throws \ReflectionException * @throws \ReflectionException
*/ */
public function getMethods(ReflectionClass|string $class): array public function getMethods(ReflectionClass|string $class): array
{ {
if (is_string($class)) { if (is_string($class)) {
$class = $this->getReflect($class); $class = $this->getReflect($class);
} }
return $this->_classMethod[$class->getName()] ?? []; return $this->_classMethod[$class->getName()] ?? [];
} }
/** /**
* @param ReflectionClass $class * @param ReflectionClass $class
* @return ReflectionProperty[] * @return ReflectionProperty[]
*/ */
#[Pure] public function getProperty(ReflectionClass $class): array #[Pure] public function getProperty(ReflectionClass $class): array
{ {
return $this->_classProperty[$class->getName()] ?? []; return $this->_classProperty[$class->getName()] ?? [];
} }
/** /**
* @param ReflectionClass $class * @param ReflectionClass $class
* @return array * @return array
*/ */
#[Pure] public function getPropertyNote(ReflectionClass $class): array #[Pure] public function getPropertyNote(ReflectionClass $class): array
{ {
return $this->_classPropertyNote[$class->getName()] ?? []; return $this->_classPropertyNote[$class->getName()] ?? [];
} }
} }
+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);
} }
} }
} }
+3 -128
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;
} }
+6 -7
View File
@@ -4,7 +4,6 @@
namespace Annotation; namespace Annotation;
/** /**
* Class Attribute * Class Attribute
* @package Annotation * @package Annotation
@@ -13,12 +12,12 @@ 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;
} }
+7 -6
View File
@@ -9,12 +9,13 @@ use Closure;
interface IAnnotation interface IAnnotation
{ {
/** /**
* @param mixed $class * @param static $params
* @param mixed $method * @param mixed $class
* @return mixed * @param mixed $method
*/ * @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;
} }
+18 -19
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,34 +22,33 @@ 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];
}
/**
* @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 = []; $array = [];
foreach ($this->middleware as $value) { foreach ($params->middleware as $value) {
$sn = di($value); $sn = di($value);
if (!($sn instanceof MiddlewareInterface)) { if (!($sn instanceof MiddlewareInterface)) {
continue; continue;
} }
$array[] = [$sn, 'onHandler']; $array[] = [$sn, 'onHandler'];
} }
$this->middleware = $array; MiddlewareManager::addMiddlewares($class, $method, $array);
}
return parent::execute($params, $class, $method);
/**
* @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;
} }
+25 -31
View File
@@ -12,39 +12,33 @@ use Kiri\Kiri;
#[\Attribute(\Attribute::TARGET_METHOD)] class Route extends Attribute #[\Attribute(\Attribute::TARGET_METHOD)] class Route extends Attribute
{ {
/** /**
* Route constructor. * Route constructor.
* @param string $uri * @param string $uri
* @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);
}
/** /**
* @param mixed $class * @param mixed $class
* @param mixed|null $method * @param mixed|null $method
* @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($params->uri, [$class, $method], strtoupper($params->method));
$router->addRoute($this->uri, [$class, $method], $this->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)
{ {
} }