This commit is contained in:
as2252258@163.com
2021-08-29 05:53:47 +08:00
parent 1cb1b0b9cb
commit c06f25fcf5
16 changed files with 152 additions and 205 deletions
+3 -33
View File
@@ -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
+1 -1
View File
@@ -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;
}
+2 -2
View File
@@ -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);
}
}
}
+1 -1
View File
@@ -231,7 +231,7 @@ class Node
if (empty($aspect)) {
return null;
}
return di($aspect['params']['aspect']);
return di($aspect->aspect);
}
+2 -2
View File
@@ -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);
}
}
}
+2 -2
View File
@@ -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;
}
+3 -3
View File
@@ -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;
}
+1 -1
View File
@@ -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;
+23 -23
View File
@@ -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;
}
}
+1 -1
View File
@@ -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;
}
+75 -75
View File
@@ -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);
}
}
}
+20 -20
View File
@@ -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;
}
-12
View File
@@ -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];
}
}
-10
View File
@@ -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;
}
}
+15 -16
View File
@@ -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);
}
+3 -3
View File
@@ -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;
}