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.
* @param string $value
* @param bool $withContext
* @param array $args
*/
public function __construct(private string $value, public bool $withContext = false, private array $args = [])
@@ -78,7 +79,7 @@ use Snowflake\Snowflake;
return $method;
}
if (is_object($class)) $class = $class::class;
$method = Snowflake::getDi()->getClassProperty($class, $method);
$method = Snowflake::getDi()->getClassReflectionProperty($class, $method);
if (!$method || $method->isStatic()) {
return false;
}
+13 -16
View File
@@ -4,15 +4,10 @@
namespace Annotation;
use Annotation\Model\Get;
use Annotation\Model\Relation;
use Annotation\Model\Set;
use Attribute;
use DirectoryIterator;
use Exception;
use ReflectionClass;
use ReflectionException;
use ReflectionMethod;
use Snowflake\Abstracts\BaseObject;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake;
@@ -56,6 +51,8 @@ class Loader extends BaseObject
*/
public function getProperty(string $class, string $property = ''): mixed
{
return Snowflake::getDi()->getClassReflectionProperty($class, $property);
if (!isset(static::$_property[$class])) {
return null;
}
@@ -68,20 +65,20 @@ class Loader extends BaseObject
/**
* @param string $class
* @param mixed $handler
* @return Loader
* @param object $handler
* @return $this
* @throws NotFindClassException
* @throws ReflectionException
* @throws Exception
*/
public function injectProperty(string $class, object $handler): static
{
$properties = $this->getProperty($class);
if (empty($properties)) {
return $this;
}
foreach ($properties as $property => $attributes) {
foreach ($attributes as $attribute) {
$attribute->execute($handler, $property);
}
}
$di = Snowflake::getDi();
$reflect = $di->getReflect($class);
$di->propertyInject($reflect, $handler);
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;
use Annotation\Route\Socket;
use Exception;
use HttpServer\Abstracts\HttpService;
use HttpServer\Http\Response as HResponse;
use HttpServer\IInterface\AuthIdentity;
use JetBrains\PhpStorm\Pure;
use Snowflake\Abstracts\Config;
use Snowflake\Core\ArrayAccess;
use Snowflake\Core\Help;
use Snowflake\Core\Json;
use Snowflake\Exception\ConfigException;
use Snowflake\Snowflake;
use function router;
@@ -456,6 +451,7 @@ class Request extends HttpService
{
/** @var Request $sRequest */
$sRequest = Context::setContext('request', new Request());
$sRequest->fd = $request->fd;
$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;
use Closure;
use HttpServer\IInterface\Middleware;
use Snowflake\Abstracts\BaseObject;
@@ -50,7 +51,7 @@ class MiddlewareManager extends BaseObject
* @param $method
* @return bool
*/
public function hasMiddleware($class, $method)
public function hasMiddleware($class, $method): bool
{
if (is_object($class)) {
$class = $class::class;
@@ -63,8 +64,9 @@ class MiddlewareManager extends BaseObject
* @param $class
* @param $method
* @param $caller
* @return mixed
*/
public function callerMiddlewares($class, $method, $caller)
public function callerMiddlewares($class, $method, $caller): mixed
{
if (is_object($class)) {
$class = $class::class;
@@ -83,4 +85,22 @@ class MiddlewareManager extends BaseObject
}, $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 {
$this->handler = $handler;
}
if (!empty($this->handler) && is_array($this->handler)) {
$this->callback = di(MiddlewareManager::class)->callerMiddlewares(
return $this->injectMiddleware();
}
/**
* @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()
);
}
+12 -13
View File
@@ -214,7 +214,7 @@ class Container extends BaseObject
* @return mixed
* @throws Exception
*/
private function propertyInject(ReflectionClass $reflect, $object): mixed
public function propertyInject(ReflectionClass $reflect, $object): mixed
{
if (!isset(static::$_propertyAttributes[$reflect->getName()])) {
return $object;
@@ -228,7 +228,7 @@ class Container extends BaseObject
/**
* @param \ReflectionClass $class
* @param ReflectionClass $class
*/
private function resolveMethodAttribute(ReflectionClass $class)
{
@@ -252,11 +252,10 @@ class Container extends BaseObject
/**
* @param \ReflectionAttribute $attribute
* @param \ReflectionClass $class
* @param $object
* @param ReflectionClass $class
* @param ReflectionMethod $method
*/
private function setReflectionMethod(ReflectionClass $class, \ReflectionMethod $method)
private function setReflectionMethod(ReflectionClass $class, ReflectionMethod $method)
{
if (!isset(static::$_attributeMaping[$class->getName()])) {
static::$_attributeMaping[$class->getName()] = [];
@@ -266,11 +265,11 @@ class Container extends BaseObject
/**
* @param \ReflectionAttribute $attribute
* @param \ReflectionClass $class
* @param ReflectionClass $attribute
* @param ReflectionMethod $method
* @param $object
*/
private function setMethodsAttributes(\ReflectionClass $attribute, ReflectionMethod $method, $object)
private function setMethodsAttributes(ReflectionClass $attribute, ReflectionMethod $method, $object)
{
if (!isset(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)
{
@@ -299,7 +298,7 @@ class Container extends BaseObject
/**
* @param $className
* @param $method
* @return \ReflectionMethod|null
* @return ReflectionMethod|null
*/
public function getReflectionMethod($className, $method): ?ReflectionMethod
{
@@ -325,9 +324,9 @@ class Container extends BaseObject
/**
* @param string $class
* @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])) {
return null;
+1 -1
View File
@@ -56,7 +56,7 @@ class Snowflake
*/
public static function injectProperty(object $class)
{
$attributes = static::getDi()->getClassProperty($class::class);
$attributes = static::getDi()->getClassReflectionProperty($class::class);
/**
* @var string $property
* @var ReflectionProperty $attribute