This commit is contained in:
2021-07-27 16:08:16 +08:00
parent ef423dc1b8
commit a36f876792
8 changed files with 744 additions and 748 deletions
+2 -1
View File
@@ -22,6 +22,7 @@ use Snowflake\Snowflake;
/** /**
* Inject constructor. * Inject constructor.
* @param string $value * @param string $value
* @param bool $withContext
* @param array $args * @param array $args
*/ */
public function __construct(private string $value, public bool $withContext = false, private array $args = []) public function __construct(private string $value, public bool $withContext = false, private array $args = [])
@@ -78,7 +79,7 @@ use Snowflake\Snowflake;
return $method; return $method;
} }
if (is_object($class)) $class = $class::class; if (is_object($class)) $class = $class::class;
$method = Snowflake::getDi()->getClassProperty($class, $method); $method = Snowflake::getDi()->getClassReflectionProperty($class, $method);
if (!$method || $method->isStatic()) { if (!$method || $method->isStatic()) {
return false; return false;
} }
+13 -16
View File
@@ -4,15 +4,10 @@
namespace Annotation; namespace Annotation;
use Annotation\Model\Get;
use Annotation\Model\Relation;
use Annotation\Model\Set;
use Attribute;
use DirectoryIterator; use DirectoryIterator;
use Exception; use Exception;
use ReflectionClass; use ReflectionClass;
use ReflectionException; use ReflectionException;
use ReflectionMethod;
use Snowflake\Abstracts\BaseObject; use Snowflake\Abstracts\BaseObject;
use Snowflake\Exception\NotFindClassException; use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake; use Snowflake\Snowflake;
@@ -56,6 +51,8 @@ class Loader extends BaseObject
*/ */
public function getProperty(string $class, string $property = ''): mixed public function getProperty(string $class, string $property = ''): mixed
{ {
return Snowflake::getDi()->getClassReflectionProperty($class, $property);
if (!isset(static::$_property[$class])) { if (!isset(static::$_property[$class])) {
return null; return null;
} }
@@ -68,20 +65,20 @@ class Loader extends BaseObject
/** /**
* @param string $class * @param string $class
* @param mixed $handler * @param object $handler
* @return Loader * @return $this
* @throws NotFindClassException
* @throws ReflectionException
* @throws Exception
*/ */
public function injectProperty(string $class, object $handler): static public function injectProperty(string $class, object $handler): static
{ {
$properties = $this->getProperty($class); $di = Snowflake::getDi();
if (empty($properties)) {
return $this; $reflect = $di->getReflect($class);
}
foreach ($properties as $property => $attributes) { $di->propertyInject($reflect, $handler);
foreach ($attributes as $attribute) {
$attribute->execute($handler, $property);
}
}
return $this; return $this;
} }
-48
View File
@@ -1,48 +0,0 @@
<?php
namespace Annotation\Route;
use Annotation\Attribute;
use Snowflake\Snowflake;
/**
* Class Interceptor
* @package Annotation\Route
*/
#[\Attribute(\Attribute::TARGET_METHOD)] class After extends Attribute
{
/**
* Interceptor constructor.
* @param \HttpServer\IInterface\After|\HttpServer\IInterface\After[] $after
* @throws
*/
public function __construct(public string|array $after)
{
if (is_string($this->after)) {
$this->after = [$this->after];
}
foreach ($this->after as $key => $value) {
$sn = Snowflake::createObject($value);
if (!($sn instanceof \HttpServer\IInterface\After)) {
continue;
}
$this->after[$key] = [$sn, 'onHandler'];
}
}
/**
* @param mixed $class
* @param mixed|null $method
* @return After
*/
public function execute(mixed $class, mixed $method = null): static
{
return $this;
}
}
+14 -5
View File
@@ -3,17 +3,12 @@ declare(strict_types=1);
namespace HttpServer\Http; namespace HttpServer\Http;
use Annotation\Route\Socket;
use Exception; use Exception;
use HttpServer\Abstracts\HttpService; use HttpServer\Abstracts\HttpService;
use HttpServer\Http\Response as HResponse;
use HttpServer\IInterface\AuthIdentity; use HttpServer\IInterface\AuthIdentity;
use JetBrains\PhpStorm\Pure; use JetBrains\PhpStorm\Pure;
use Snowflake\Abstracts\Config;
use Snowflake\Core\ArrayAccess;
use Snowflake\Core\Help; use Snowflake\Core\Help;
use Snowflake\Core\Json; use Snowflake\Core\Json;
use Snowflake\Exception\ConfigException;
use Snowflake\Snowflake; use Snowflake\Snowflake;
use function router; use function router;
@@ -456,6 +451,7 @@ class Request extends HttpService
{ {
/** @var Request $sRequest */ /** @var Request $sRequest */
$sRequest = Context::setContext('request', new Request()); $sRequest = Context::setContext('request', new Request());
$sRequest->fd = $request->fd; $sRequest->fd = $request->fd;
$sRequest->startTime = microtime(true); $sRequest->startTime = microtime(true);
@@ -476,4 +472,17 @@ class Request extends HttpService
} }
/**
* @param string $key
* @param mixed|null $defaultValue
* @return mixed
*/
public function post(string $key, mixed $defaultValue = null): mixed
{
/** @var HttpParams $params */
$params = Context::getContext(HttpParams::class);
return $params->post($key, $defaultValue);
}
} }
+22 -2
View File
@@ -4,6 +4,7 @@
namespace HttpServer\Route; namespace HttpServer\Route;
use Closure;
use HttpServer\IInterface\Middleware; use HttpServer\IInterface\Middleware;
use Snowflake\Abstracts\BaseObject; use Snowflake\Abstracts\BaseObject;
@@ -50,7 +51,7 @@ class MiddlewareManager extends BaseObject
* @param $method * @param $method
* @return bool * @return bool
*/ */
public function hasMiddleware($class, $method) public function hasMiddleware($class, $method): bool
{ {
if (is_object($class)) { if (is_object($class)) {
$class = $class::class; $class = $class::class;
@@ -63,8 +64,9 @@ class MiddlewareManager extends BaseObject
* @param $class * @param $class
* @param $method * @param $method
* @param $caller * @param $caller
* @return mixed
*/ */
public function callerMiddlewares($class, $method, $caller) public function callerMiddlewares($class, $method, $caller): mixed
{ {
if (is_object($class)) { if (is_object($class)) {
$class = $class::class; $class = $class::class;
@@ -83,4 +85,22 @@ class MiddlewareManager extends BaseObject
}, $caller); }, $caller);
} }
/**
* @param $middlewares
* @param Closure $caller
* @return Closure
*/
public function closureMiddlewares($middlewares, Closure $caller): Closure
{
return array_reduce(array_reverse($middlewares), function ($stack, $pipe) {
return function ($passable) use ($stack, $pipe) {
if ($pipe instanceof Middleware) {
return $pipe->onHandler($passable, $stack);
}
return call_user_func($pipe, $passable, $stack);
};
}, $caller);
}
} }
+20 -2
View File
@@ -94,8 +94,26 @@ class Node extends HttpService
} else { } else {
$this->handler = $handler; $this->handler = $handler;
} }
if (!empty($this->handler) && is_array($this->handler)) { return $this->injectMiddleware();
$this->callback = di(MiddlewareManager::class)->callerMiddlewares( }
/**
* @throws NotFindClassException
* @throws ReflectionException
*/
private function injectMiddleware(): static
{
$manager = di(MiddlewareManager::class);
if ($this->handler instanceof Closure) {
if (!empty($this->middleware)) {
$this->callback = $manager->closureMiddlewares($this->middleware, $this->createDispatch());
} else {
$this->callback = $this->createDispatch();
}
} else {
$manager->addMiddlewares($this->handler[0], $this->handler[1], $this->middleware);
$this->callback = $manager->callerMiddlewares(
$this->handler[0], $this->handler[1], $this->createDispatch() $this->handler[0], $this->handler[1], $this->createDispatch()
); );
} }
+12 -13
View File
@@ -214,7 +214,7 @@ class Container extends BaseObject
* @return mixed * @return mixed
* @throws Exception * @throws Exception
*/ */
private function propertyInject(ReflectionClass $reflect, $object): mixed public function propertyInject(ReflectionClass $reflect, $object): mixed
{ {
if (!isset(static::$_propertyAttributes[$reflect->getName()])) { if (!isset(static::$_propertyAttributes[$reflect->getName()])) {
return $object; return $object;
@@ -228,7 +228,7 @@ class Container extends BaseObject
/** /**
* @param \ReflectionClass $class * @param ReflectionClass $class
*/ */
private function resolveMethodAttribute(ReflectionClass $class) private function resolveMethodAttribute(ReflectionClass $class)
{ {
@@ -252,11 +252,10 @@ class Container extends BaseObject
/** /**
* @param \ReflectionAttribute $attribute * @param ReflectionClass $class
* @param \ReflectionClass $class * @param ReflectionMethod $method
* @param $object
*/ */
private function setReflectionMethod(ReflectionClass $class, \ReflectionMethod $method) private function setReflectionMethod(ReflectionClass $class, ReflectionMethod $method)
{ {
if (!isset(static::$_attributeMaping[$class->getName()])) { if (!isset(static::$_attributeMaping[$class->getName()])) {
static::$_attributeMaping[$class->getName()] = []; static::$_attributeMaping[$class->getName()] = [];
@@ -266,11 +265,11 @@ class Container extends BaseObject
/** /**
* @param \ReflectionAttribute $attribute * @param ReflectionClass $attribute
* @param \ReflectionClass $class * @param ReflectionMethod $method
* @param $object * @param $object
*/ */
private function setMethodsAttributes(\ReflectionClass $attribute, ReflectionMethod $method, $object) private function setMethodsAttributes(ReflectionClass $attribute, ReflectionMethod $method, $object)
{ {
if (!isset(static::$_methodsAttributes[$attribute->getName()])) { if (!isset(static::$_methodsAttributes[$attribute->getName()])) {
static::$_methodsAttributes[$attribute->getName()] = []; static::$_methodsAttributes[$attribute->getName()] = [];
@@ -280,7 +279,7 @@ class Container extends BaseObject
/** /**
* @param \ReflectionClass $class * @param ReflectionClass $class
*/ */
private function resolveTargetAttribute(ReflectionClass $class) private function resolveTargetAttribute(ReflectionClass $class)
{ {
@@ -299,7 +298,7 @@ class Container extends BaseObject
/** /**
* @param $className * @param $className
* @param $method * @param $method
* @return \ReflectionMethod|null * @return ReflectionMethod|null
*/ */
public function getReflectionMethod($className, $method): ?ReflectionMethod public function getReflectionMethod($className, $method): ?ReflectionMethod
{ {
@@ -325,9 +324,9 @@ class Container extends BaseObject
/** /**
* @param string $class * @param string $class
* @param string|null $property * @param string|null $property
* @return ReflectionProperty|array|null * @return ReflectionProperty|ReflectionProperty[]|null
*/ */
public function getClassProperty(string $class, string $property = null): ReflectionProperty|null|array public function getClassReflectionProperty(string $class, string $property = null): ReflectionProperty|null|array
{ {
if (!isset(static::$_reflectionProperty[$class])) { if (!isset(static::$_reflectionProperty[$class])) {
return null; return null;
+1 -1
View File
@@ -56,7 +56,7 @@ class Snowflake
*/ */
public static function injectProperty(object $class) public static function injectProperty(object $class)
{ {
$attributes = static::getDi()->getClassProperty($class::class); $attributes = static::getDi()->getClassReflectionProperty($class::class);
/** /**
* @var string $property * @var string $property
* @var ReflectionProperty $attribute * @var ReflectionProperty $attribute