This commit is contained in:
as2252258@163.com
2021-07-27 03:30:55 +08:00
parent 22fa0d292c
commit 4e3b41331d
13 changed files with 888 additions and 1199 deletions
+2 -7
View File
@@ -192,14 +192,9 @@ class Annotation extends Component
* @param string|array $outPath
* @throws Exception
*/
public function runtime(string $dir, string|array $outPath = '')
public function runtime(string $dir)
{
if (empty($outPath)) {
$outPath = [];
} else if (is_string($outPath)) {
$outPath = [$outPath];
}
$this->_loader->loadByDirectory($dir, $outPath);
return $this->_loader->loadByDirectory($dir);
}
+9 -141
View File
@@ -38,13 +38,6 @@ class Loader extends BaseObject
private static array $_methods = [];
/**
* @return array
*/
public function getDirectory(): array
{
return static::$_directory;
}
/**
* @param $path
@@ -56,16 +49,6 @@ class Loader extends BaseObject
$this->_scanDir(new DirectoryIterator($path), $namespace);
}
/**
* @return array
*/
public function getClasses(): array
{
return static::$_classes;
}
/**
* @param string $class
* @param string $property
@@ -121,16 +104,6 @@ class Loader extends BaseObject
}
/**
* @param string $class
* @return array
*/
public function getTarget(string $class): array
{
return static::$_classes[$class] ?? [];
}
/**
* @param DirectoryIterator $paths
* @param $namespace
@@ -173,16 +146,7 @@ class Loader extends BaseObject
}
$this->appendFileToDirectory($path->getRealPath(), $replace->getName());
$_array['handler'] = $replace->newInstance();
$_array['target'] = [];
$_array['methods'] = [];
$_array['property'] = [];
$_array = $this->_targets($replace, $_array);
$_array = $this->_methods($replace, $_array);
$_array = $this->_properties($replace, $_array);
static::$_classes[$replace->getName()] = $_array;
static::$_classes[] = $replace->getName();
} catch (Throwable $throwable) {
$this->addError($throwable, 'throwable');
}
@@ -202,75 +166,6 @@ class Loader extends BaseObject
}
/**
* @param ReflectionClass $replace
* @param array $_array
* @return array
*/
private function _targets(ReflectionClass $replace, array $_array): array
{
foreach ($replace->getAttributes() as $attribute) {
if ($attribute->getName() == Attribute::class) {
continue;
}
if ($attribute->getName() == Target::class) {
continue;
}
$_array['target'][] = $attribute->newInstance();
}
return $_array;
}
/**
* @param ReflectionClass $replace
* @param array $_array
* @return array
*/
private function _methods(ReflectionClass $replace, array $_array): array
{
$methods = $replace->getMethods(ReflectionMethod::IS_PUBLIC);
foreach ($methods as $method) {
$_method = [];
foreach ($method->getAttributes() as $attribute) {
if (!class_exists($attribute->getName())) {
continue;
}
$_method[] = $attribute->newInstance();
}
if (!empty($_method)) {
static::$_methods[$replace->getName()][$method->getName()] = $_method;
}
}
return $_array;
}
/**
* @param ReflectionClass $replace
* @param array $_array
* @return array
*/
private function _properties(ReflectionClass $replace, array $_array): array
{
$methods = $replace->getProperties();
foreach ($methods as $method) {
$_property = [];
if ($method->isStatic()) continue;
foreach ($method->getAttributes() as $attribute) {
if (!class_exists($attribute->getName())) {
continue;
}
$_property[] = $attribute->newInstance();
}
if (!empty($_property)) {
static::$_property[$replace->getName()][$method->getName()] = $_property;
}
}
return $_array;
}
/**
* @param string $path
* @param string|array $outPath
@@ -280,15 +175,22 @@ class Loader extends BaseObject
{
try {
$path = '/' . trim($path, '/');
$paths = [];
foreach (static::$_directory as $key => $_path) {
$key = '/' . trim($key, '/');
if (!str_starts_with($key, $path) || in_array($key, $outPath)) {
continue;
}
$this->execute($_path);
foreach ($_path as $item) {
$paths[] = $item;
}
}
return $paths;
} catch (Throwable $exception) {
$this->addError($exception, 'throwable');
return [];
}
}
@@ -326,38 +228,4 @@ class Loader extends BaseObject
}
/**
* @param array $classes
* @throws Exception
*/
private function execute(array $classes)
{
if (empty($classes)) {
return;
}
foreach ($classes as $className) {
if (!isset(static::$_methods[$className])) {
continue;
}
foreach (static::$_methods[$className] as $name => $attribute) {
$this->methods($attribute, $className, $name);
}
}
}
/**
* @param $attribute
* @param $annotation
* @param $className
* @param $name
*/
private function methods($attribute, $className, $name)
{
$handler = static::$_classes[$className]['handler'];
foreach ($attribute as $value) {
$value->execute($handler, $name);
}
}
}
-2
View File
@@ -4,9 +4,7 @@
namespace Annotation\Model;
use Annotation\Annotation;
use Attribute;
use Database\ActiveRecord;
use Exception;
use Snowflake\Snowflake;
-52
View File
@@ -1,52 +0,0 @@
<?php
namespace Annotation\Route;
use Annotation\Attribute;
use JetBrains\PhpStorm\Pure;
use Snowflake\Snowflake;
/**
* Class Interceptor
* @package Annotation\Route
*/
#[\Attribute(\Attribute::TARGET_METHOD)] class Interceptor extends Attribute
{
/**
* Interceptor constructor.
* @param string|array $interceptor
* @throws
*/
public function __construct(public string|array $interceptor)
{
if (is_string($this->interceptor)) {
$this->interceptor = [$this->interceptor];
}
foreach ($this->interceptor as $key => $value) {
$sn = Snowflake::createObject($value);
if (!($sn instanceof \HttpServer\IInterface\Interceptor)) {
continue;
}
$this->interceptor[$key] = [$sn, 'Interceptor'];
}
}
/**
* @param mixed $class
* @param mixed|null $method
* @return Interceptor
*/
public function execute(mixed $class, mixed $method = null): static
{
return $this;
}
}
-52
View File
@@ -1,52 +0,0 @@
<?php
namespace Annotation\Route;
use Annotation\Attribute;
use Snowflake\Snowflake;
/**
* Class Limits
* @package Annotation\Route
*/
#[\Attribute(\Attribute::TARGET_METHOD)] class Limits extends Attribute
{
/**
* Limits constructor.
* @param string|array $limits
* @throws
*/
public function __construct(public string|array $limits)
{
if (is_string($this->limits)) {
$this->limits = [$this->limits];
}
foreach ($this->limits as $key => $value) {
$sn = Snowflake::createObject($value);
if (!($sn instanceof \HttpServer\IInterface\Limits)) {
continue;
}
$this->limits[$key] = [$sn, 'next'];
}
}
/**
* @param mixed $class
* @param mixed|null $method
* @return Limits
*/
public function execute(mixed $class, mixed $method = null): static
{
return $this;
}
}
+31 -28
View File
@@ -5,6 +5,7 @@ namespace Annotation\Route;
use Annotation\Attribute;
use HttpServer\Route\Middlewares;
use Snowflake\Snowflake;
use HttpServer\IInterface\Middleware as IMiddleware;
@@ -16,38 +17,40 @@ use HttpServer\IInterface\Middleware as IMiddleware;
{
/**
* Interceptor constructor.
* @param string|array $middleware
* @throws
*/
public function __construct(public string|array $middleware)
{
if (is_string($this->middleware)) {
$this->middleware = [$this->middleware];
}
/**
* Interceptor constructor.
* @param string|array $middleware
* @throws
*/
public function __construct(public string|array $middleware)
{
if (is_string($this->middleware)) {
$this->middleware = [$this->middleware];
}
$array = [];
foreach ($this->middleware as $key => $value) {
$sn = Snowflake::createObject($value);
if (!($sn instanceof IMiddleware)) {
continue;
}
$array[] = [$sn, 'onHandler'];
}
$this->middleware = $array;
}
$array = [];
foreach ($this->middleware as $value) {
$sn = Snowflake::createObject($value);
if (!($sn instanceof IMiddleware)) {
continue;
}
$array[] = [$sn, 'onHandler'];
}
$this->middleware = $array;
}
/**
* @param mixed $class
* @param mixed|null $method
* @return Middleware
*/
/**
* @param mixed $class
* @param mixed|null $method
* @return Middleware
*/
public function execute(mixed $class, mixed $method = null): static
{
return $this;
}
{
$middleware = Snowflake::getDi()->get(Middlewares::class);
$middleware->addMiddlewares($class, $method, $this->middleware);
return $this;
}
}
+34 -27
View File
@@ -13,36 +13,43 @@ use Snowflake\Snowflake;
#[\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'
)
{
}
/**
* 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'
)
{
}
/**
* @param mixed $class
* @param mixed|null $method
* @return Router
* @throws Exception
*/
/**
* @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 = Snowflake::app()->getRouter();
$router->addRoute($this->uri, [$class, $method], $this->method);
return $router;
}
{
// TODO: Implement setHandler() method.
$router = Snowflake::app()->getRouter();
$node = $router->addRoute($this->uri, [$class, $method], $this->method);
if ($node !== null) {
$attribute = Snowflake::getDi()->getMethodAttribute($class::class, $method);
foreach ($attribute as $item) {
if ($item instanceof Route) {
continue;
}
$item->execute($class, $method);
}
}
return $router;
}
}