改名
This commit is contained in:
+18
-301
@@ -4,16 +4,8 @@
|
||||
namespace Annotation;
|
||||
|
||||
|
||||
use Exception;
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
use ReflectionAttribute;
|
||||
use ReflectionClass;
|
||||
use ReflectionException;
|
||||
use ReflectionMethod;
|
||||
use DirectoryIterator;
|
||||
use Snowflake\Abstracts\Component;
|
||||
use Snowflake\Exception\NotFindClassException;
|
||||
use Snowflake\Exception\NotFindPropertyException;
|
||||
use Snowflake\Snowflake;
|
||||
|
||||
/**
|
||||
* Class Annotation
|
||||
@@ -22,28 +14,13 @@ use Snowflake\Snowflake;
|
||||
class Annotation extends Component
|
||||
{
|
||||
|
||||
private array $_annotations = [];
|
||||
|
||||
private Loader $_loader;
|
||||
|
||||
|
||||
private array $_classes = [];
|
||||
|
||||
|
||||
private array $_targets = [];
|
||||
|
||||
|
||||
private array $_methods = [];
|
||||
|
||||
|
||||
private array $_properties = [];
|
||||
|
||||
|
||||
/**
|
||||
* @param array $handler
|
||||
* @param $name
|
||||
*/
|
||||
public function addMethodAttribute(array $handler, $name)
|
||||
public function init(): void
|
||||
{
|
||||
$this->_methods[get_class($handler[0])][$name] = $handler;
|
||||
$this->_loader = new Loader();
|
||||
}
|
||||
|
||||
|
||||
@@ -54,7 +31,16 @@ class Annotation extends Component
|
||||
*/
|
||||
public function getMethods(string $className): array
|
||||
{
|
||||
return $this->_methods[$className] ?? [];
|
||||
return $this->_loader->getMethod($className);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param object $class
|
||||
*/
|
||||
public function injectProperty(object $class)
|
||||
{
|
||||
$this->_loader->injectProperty(get_class($class), $class);
|
||||
}
|
||||
|
||||
|
||||
@@ -62,281 +48,12 @@ class Annotation extends Component
|
||||
* @param string $path
|
||||
* @param string $namespace
|
||||
* @param string $alias
|
||||
* @return $this
|
||||
* @throws ReflectionException|NotFindPropertyException|NotFindClassException
|
||||
*/
|
||||
public function read(string $path, string $namespace, string $alias = 'root'): static
|
||||
{
|
||||
return $this->scanDir(glob($path . '*'), $namespace, $alias);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $paths
|
||||
* @param string $namespace
|
||||
* @param string $alias
|
||||
* @return $this
|
||||
* @throws ReflectionException|NotFindPropertyException
|
||||
* @throws NotFindClassException
|
||||
* @throws Exception
|
||||
*/
|
||||
private function scanDir(array $paths, string $namespace, string $alias): static
|
||||
{
|
||||
foreach ($paths as $path) {
|
||||
$explode = explode('/', $path);
|
||||
|
||||
$explode_pop = array_pop($explode);
|
||||
if (is_file($path)) {
|
||||
if (!str_contains($path, '.php')) {
|
||||
continue;
|
||||
}
|
||||
$explode_pop = str_replace('.php', '', $explode_pop);
|
||||
$this->getReflect($namespace . '\\' . $explode_pop, $alias);
|
||||
} else {
|
||||
$this->scanDir(glob($path . '/*'), $namespace . '\\' . $explode_pop, $alias);
|
||||
}
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $class
|
||||
* @param string $alias
|
||||
* @return array
|
||||
* @throws Exception
|
||||
*/
|
||||
private function getReflect(string $class, string $alias): array
|
||||
{
|
||||
try {
|
||||
$reflect = $this->reflectClass($class);
|
||||
if (empty($reflect) || !$reflect->isInstantiable()) {
|
||||
return [];
|
||||
}
|
||||
$object = $reflect->newInstance();
|
||||
$this->resolveMethod($reflect, $class, $alias, $object);
|
||||
return $this->targets($reflect);
|
||||
} catch (\Throwable $throwable) {
|
||||
$this->addError($throwable);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ReflectionClass $reflect
|
||||
* @param $class
|
||||
* @param $alias
|
||||
* @param $object
|
||||
* @throws NotFindPropertyException
|
||||
*/
|
||||
private function resolveMethod(ReflectionClass $reflect, $class, $alias, $object)
|
||||
{
|
||||
$targets = $reflect->getAttributes(Target::class);
|
||||
if (empty($targets)) {
|
||||
return;
|
||||
}
|
||||
foreach ($reflect->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
|
||||
if ($method->class != $class) {
|
||||
continue;
|
||||
}
|
||||
$this->resolveAnnotations($method, $alias, $object);
|
||||
}
|
||||
$this->resolveProperty($reflect, $object);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ReflectionClass $reflectionClass
|
||||
* @param $object
|
||||
* @throws NotFindPropertyException
|
||||
*/
|
||||
private function resolveProperty(ReflectionClass $reflectionClass, $object)
|
||||
{
|
||||
$property = $reflectionClass->getProperties();
|
||||
if (!isset($this->_properties[get_class($object)])) {
|
||||
$this->_properties[get_class($object)] = [];
|
||||
}
|
||||
foreach ($property as $value) {
|
||||
if ($value->isStatic()) continue;
|
||||
$attributes = $value->getAttributes();
|
||||
if (count($attributes) < 1) {
|
||||
continue;
|
||||
}
|
||||
foreach ($attributes as $attribute) {
|
||||
/** @var IAnnotation $annotation */
|
||||
$annotation = $this->instance($attribute);
|
||||
if (empty($annotation)) {
|
||||
continue;
|
||||
}
|
||||
$annotation = $annotation->execute([$object, $value->getName()]);
|
||||
$this->_properties[get_class($object)][$value->getName()] = $annotation;
|
||||
|
||||
if ($value->isPublic()) {
|
||||
continue;
|
||||
}
|
||||
if (!method_exists($object, ($name = 'set' . ucfirst($value->getName())))) {
|
||||
throw new NotFindPropertyException('set property need method ' . $name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $class
|
||||
* @param string $property
|
||||
* @return mixed|null
|
||||
*/
|
||||
public function getProperty($class, $property = ''): ?array
|
||||
{
|
||||
if (is_object($class)) {
|
||||
$class = get_class($class);
|
||||
}
|
||||
if (!isset($this->_properties[$class])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!empty($property)) {
|
||||
return $this->_properties[$class][$property] ?? null;
|
||||
}
|
||||
|
||||
return $this->_properties[$class];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $class
|
||||
* @return void
|
||||
*/
|
||||
public function setProperty($class): void
|
||||
public function read(string $path, string $namespace, string $alias = 'root'): void
|
||||
{
|
||||
$lists = $this->getProperty($class);
|
||||
if (empty($lists)) {
|
||||
return;
|
||||
}
|
||||
Snowflake::configure($class, $lists);
|
||||
|
||||
$this->_loader->_scanDir(new DirectoryIterator($path), $namespace);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $class
|
||||
* @return ReflectionClass|null
|
||||
* @throws ReflectionException|NotFindClassException
|
||||
*/
|
||||
private function reflectClass(string $class): ?ReflectionClass
|
||||
{
|
||||
return Snowflake::getDi()->getReflect($class);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ReflectionMethod $method
|
||||
* @param $alias
|
||||
* @param $object
|
||||
* @return array
|
||||
*/
|
||||
private function resolveAnnotations(ReflectionMethod $method, $alias, $object): array
|
||||
{
|
||||
$attributes = $method->getAttributes();
|
||||
if (count($attributes) < 1) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$name = get_class($object) . '_' . $method->getName();
|
||||
if (!isset($this->_annotations[$name])) {
|
||||
$this->_annotations[$name] = [];
|
||||
}
|
||||
|
||||
foreach ($attributes as $attribute) {
|
||||
/** @var IAnnotation $class */
|
||||
$class = $this->instance($attribute);
|
||||
if ($class === null) {
|
||||
continue;
|
||||
}
|
||||
$this->_annotations[$name][] = $class->execute([$object, $method->getName()]);
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $class
|
||||
* @param $method
|
||||
* @return mixed
|
||||
*/
|
||||
#[Pure] public function getAnnotationByMethod($class, $method): array
|
||||
{
|
||||
if (is_object($class)) {
|
||||
$class = get_class($class);
|
||||
}
|
||||
if (!isset($this->_annotations[$class . '_' . $method])) {
|
||||
return [];
|
||||
}
|
||||
return $this->_annotations[$class . '_' . $method];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $className
|
||||
* @param string $method
|
||||
* @return array
|
||||
*/
|
||||
public function getByClass($className, $method = ''): array
|
||||
{
|
||||
if (!isset($this->_classes[$className])) {
|
||||
return [];
|
||||
}
|
||||
if (empty($method)) {
|
||||
return $this->_classes[$className];
|
||||
}
|
||||
foreach ($this->_classes[$className] as $_method => $class) {
|
||||
if ($method == $_method) {
|
||||
return [$class];
|
||||
}
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ReflectionClass $reflect
|
||||
* @return array
|
||||
*/
|
||||
private function targets(ReflectionClass $reflect): array
|
||||
{
|
||||
$name = $reflect->getName();
|
||||
if (!isset($this->_classes[$name])) {
|
||||
$this->_classes[$name] = [];
|
||||
}
|
||||
$attributes = $reflect->getAttributes();
|
||||
if (count($attributes) > 0) {
|
||||
if (!isset($this->_targets[$name])) {
|
||||
$this->_targets[$name] = [];
|
||||
}
|
||||
foreach ($attributes as $attribute) {
|
||||
$class = $this->instance($attribute);
|
||||
if ($class === null) {
|
||||
continue;
|
||||
}
|
||||
$this->_targets[$name][] = $class;
|
||||
}
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ReflectionAttribute $attribute
|
||||
* @return array|object|null
|
||||
*/
|
||||
private function instance(ReflectionAttribute $attribute): array|object|null
|
||||
{
|
||||
if (!class_exists($attribute->getName())) {
|
||||
return null;
|
||||
}
|
||||
return $attribute->newInstance();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Annotation;
|
||||
|
||||
|
||||
use Snowflake\Snowflake;
|
||||
|
||||
/**
|
||||
* Class Attribute
|
||||
* @package Annotation
|
||||
*/
|
||||
abstract class Attribute implements IAnnotation
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param array $handler
|
||||
* @return mixed
|
||||
*/
|
||||
public function execute(array $handler): mixed
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -13,7 +13,7 @@ use Snowflake\Snowflake;
|
||||
* Class Event
|
||||
* @package Annotation
|
||||
*/
|
||||
#[\Attribute(\Attribute::TARGET_METHOD)] class Event implements IAnnotation
|
||||
#[\Attribute(\Attribute::TARGET_METHOD)] class Event extends Attribute
|
||||
{
|
||||
|
||||
|
||||
|
||||
+15
-4
@@ -13,7 +13,7 @@ use Snowflake\Snowflake;
|
||||
* Class Inject
|
||||
* @package Annotation
|
||||
*/
|
||||
#[\Attribute(\Attribute::TARGET_PROPERTY)] class Inject implements IAnnotation
|
||||
#[\Attribute(\Attribute::TARGET_PROPERTY)] class Inject extends Attribute
|
||||
{
|
||||
|
||||
|
||||
@@ -35,9 +35,20 @@ use Snowflake\Snowflake;
|
||||
*/
|
||||
public function execute(array $handler): mixed
|
||||
{
|
||||
if (Snowflake::app()->has($this->className)) {
|
||||
return Snowflake::app()->get($this->className);
|
||||
[$object, $property] = $handler;
|
||||
if (class_exists($this->className)) {
|
||||
return $object->$property = Snowflake::createObject($this->className, $this->args);
|
||||
}
|
||||
return Snowflake::createObject($this->className, $this->args);
|
||||
|
||||
$application = Snowflake::app();
|
||||
if (!$application->has($this->className)) {
|
||||
return $object;
|
||||
}
|
||||
|
||||
$object->$property = $application->get($this->className);
|
||||
if (!empty($this->args) && is_object($object->$property)) {
|
||||
Snowflake::configure($object->$property, $this->args);
|
||||
}
|
||||
return $object;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,184 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Annotation;
|
||||
|
||||
|
||||
use Attribute;
|
||||
use DirectoryIterator;
|
||||
use ReflectionClass;
|
||||
use ReflectionMethod;
|
||||
use ReflectionProperty;
|
||||
use Snowflake\Abstracts\BaseObject;
|
||||
use Throwable;
|
||||
|
||||
|
||||
/**
|
||||
* Class Loader
|
||||
* @package Annotation
|
||||
*/
|
||||
class Loader extends BaseObject
|
||||
{
|
||||
|
||||
|
||||
private array $_classes = [];
|
||||
|
||||
|
||||
/**
|
||||
* @param $path
|
||||
* @param $namespace
|
||||
*/
|
||||
public function loader($path, $namespace)
|
||||
{
|
||||
$this->_scanDir(new DirectoryIterator($path), $namespace);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getClasses(): array
|
||||
{
|
||||
return $this->_classes;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $class
|
||||
* @param string $property
|
||||
* @return mixed
|
||||
*/
|
||||
public function getProperty(string $class, string $property = ''): mixed
|
||||
{
|
||||
if (!isset($this->_classes[$class])) {
|
||||
return null;
|
||||
}
|
||||
$properties = $this->_classes[$class]['property'];
|
||||
if (!empty($property) && isset($properties[$property])) {
|
||||
return $properties[$property];
|
||||
}
|
||||
return $properties;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $class
|
||||
* @param mixed $handler
|
||||
* @return Loader
|
||||
*/
|
||||
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]);
|
||||
}
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $class
|
||||
* @param string $method
|
||||
* @return mixed
|
||||
*/
|
||||
public function getMethod(string $class, string $method = ''): mixed
|
||||
{
|
||||
if (!isset($this->_classes[$class])) {
|
||||
return null;
|
||||
}
|
||||
$properties = $this->_classes[$class]['methods'];
|
||||
if (!empty($property) && isset($properties[$method])) {
|
||||
return $properties[$method];
|
||||
}
|
||||
return $properties;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $class
|
||||
* @return array
|
||||
*/
|
||||
public function getTarget(string $class): array
|
||||
{
|
||||
return $this->_classes[$class] ?? [];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param DirectoryIterator $paths
|
||||
* @param $namespace
|
||||
*/
|
||||
public function _scanDir(DirectoryIterator $paths, $namespace)
|
||||
{
|
||||
foreach ($paths as $path) {
|
||||
if ($path->getFilename() === '.' || $path->getFilename() === '..') {
|
||||
continue;
|
||||
}
|
||||
if (str_starts_with($path->getFilename(), '.')) {
|
||||
continue;
|
||||
}
|
||||
if ($path->isDir()) {
|
||||
$this->_scanDir(new DirectoryIterator($path->getRealPath()), $namespace);
|
||||
continue;
|
||||
}
|
||||
if ($path->getExtension() !== 'php') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$replace = str_replace(__DIR__ . '/', '', $path->getPathname());
|
||||
|
||||
$replace = str_replace('.php', '', $replace);
|
||||
$replace = str_replace(DIRECTORY_SEPARATOR, '\\', $replace);
|
||||
$explode = explode('\\', $replace);
|
||||
array_shift($explode);
|
||||
|
||||
try {
|
||||
$replace = new ReflectionClass($namespace . implode('\\', $explode));
|
||||
if (!$replace->isInstantiable()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$_array = ['target' => [], 'methods' => [], 'property' => []];
|
||||
foreach ($replace->getAttributes() as $attribute) {
|
||||
if ($attribute->getName() == Attribute::class) {
|
||||
continue;
|
||||
}
|
||||
$_array['target'][] = $attribute->newInstance();
|
||||
}
|
||||
|
||||
$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();
|
||||
}
|
||||
$_array['methods'][$method->getName()] = $_method;
|
||||
}
|
||||
|
||||
$methods = $replace->getProperties(ReflectionMethod::IS_PUBLIC ^ ReflectionProperty::IS_STATIC);
|
||||
foreach ($methods as $method) {
|
||||
$_property = [];
|
||||
foreach ($method->getAttributes() as $attribute) {
|
||||
if (!class_exists($attribute->getName())) {
|
||||
continue;
|
||||
}
|
||||
$_property[] = $attribute->newInstance();
|
||||
}
|
||||
$_array['property'][$method->getName()] = $_property;
|
||||
}
|
||||
|
||||
$this->_classes[$replace->getName()] = $_array;
|
||||
} catch (Throwable $throwable) {
|
||||
echo $throwable->getMessage() . PHP_EOL;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,18 +5,15 @@ namespace Annotation\Model;
|
||||
|
||||
|
||||
use Annotation\Annotation;
|
||||
use Annotation\IAnnotation;
|
||||
use Attribute;
|
||||
use Database\ActiveRecord;
|
||||
use Snowflake\Exception\ComponentException;
|
||||
use Snowflake\Snowflake;
|
||||
|
||||
|
||||
/**
|
||||
* Class Get
|
||||
* @package Annotation\Model
|
||||
*/
|
||||
#[Attribute(Attribute::TARGET_METHOD)] class Get implements IAnnotation
|
||||
#[Attribute(Attribute::TARGET_METHOD)] class Get extends \Annotation\Attribute
|
||||
{
|
||||
|
||||
|
||||
@@ -33,16 +30,14 @@ use Snowflake\Snowflake;
|
||||
|
||||
/**
|
||||
* @param array $handler
|
||||
* @return Annotation
|
||||
* @throws ComponentException
|
||||
* @return ActiveRecord
|
||||
*/
|
||||
public function execute(array $handler): Annotation
|
||||
public function execute(array $handler): ActiveRecord
|
||||
{
|
||||
// TODO: Implement execute() method.
|
||||
$annotation = Snowflake::app()->getAttributes();
|
||||
$annotation->addMethodAttribute($handler, $this->name);
|
||||
/** @var ActiveRecord $activeRecord */
|
||||
[$activeRecord, $method] = $handler;
|
||||
|
||||
return $annotation;
|
||||
return $activeRecord->addGets($this->name, $method);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Annotation\Model;
|
||||
|
||||
|
||||
use Annotation\Attribute;
|
||||
use Database\ActiveRecord;
|
||||
|
||||
#[\Attribute(\Attribute::TARGET_METHOD)] class Set extends Attribute
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* Set constructor.
|
||||
* @param string $name
|
||||
*/
|
||||
public function __construct(public string $name)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @param array $handler
|
||||
* @return ActiveRecord
|
||||
*/
|
||||
public function execute(array $handler): ActiveRecord
|
||||
{
|
||||
/** @var ActiveRecord $activeRecord */
|
||||
[$activeRecord, $method] = $handler;
|
||||
|
||||
return $activeRecord->addSets($this->name, $method);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -11,7 +11,7 @@ use validator\Validator;
|
||||
* Class RequestValidator
|
||||
* @package Annotation
|
||||
*/
|
||||
#[\Attribute(\Attribute::TARGET_METHOD)] class RequestValidator implements IAnnotation
|
||||
#[\Attribute(\Attribute::TARGET_METHOD)] class RequestValidator extends Attribute
|
||||
{
|
||||
|
||||
/**
|
||||
|
||||
@@ -4,31 +4,33 @@
|
||||
namespace Annotation\Route;
|
||||
|
||||
|
||||
use Annotation\IAnnotation;
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
use ReflectionException;
|
||||
use Snowflake\Exception\NotFindClassException;
|
||||
use Annotation\Attribute;
|
||||
use Snowflake\Snowflake;
|
||||
|
||||
/**
|
||||
* Class Interceptor
|
||||
* @package Annotation\Route
|
||||
*/
|
||||
#[\Attribute(\Attribute::TARGET_METHOD)] class After implements IAnnotation
|
||||
#[\Attribute(\Attribute::TARGET_METHOD)] class After extends Attribute
|
||||
{
|
||||
|
||||
use Node;
|
||||
|
||||
/**
|
||||
* Interceptor constructor.
|
||||
* @param \HttpServer\IInterface\After|\HttpServer\IInterface\After[] $after
|
||||
* @throws
|
||||
*/
|
||||
#[Pure] public function __construct(public string|array $after)
|
||||
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'];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -4,13 +4,13 @@
|
||||
namespace Annotation\Route;
|
||||
|
||||
|
||||
use Annotation\IAnnotation;
|
||||
use Annotation\Attribute;
|
||||
|
||||
/**
|
||||
* Class Document
|
||||
* @package Annotation\Route
|
||||
*/
|
||||
#[\Attribute(\Attribute::TARGET_METHOD)] class Document implements IAnnotation
|
||||
#[\Attribute(\Attribute::TARGET_METHOD)] class Document extends Attribute
|
||||
{
|
||||
|
||||
const INTEGER = 'int';
|
||||
|
||||
@@ -4,11 +4,13 @@
|
||||
namespace Annotation\Route;
|
||||
|
||||
|
||||
use Annotation\Attribute;
|
||||
|
||||
/**
|
||||
* Class Filter
|
||||
* @package Annotation\Route
|
||||
*/
|
||||
#[\Attribute(\Attribute::TARGET_METHOD)] class Filter
|
||||
#[\Attribute(\Attribute::TARGET_METHOD)] class Filter extends Attribute
|
||||
{
|
||||
|
||||
/**
|
||||
@@ -20,4 +22,15 @@ namespace Annotation\Route;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $handler
|
||||
* @return array
|
||||
*/
|
||||
public function execute(array $handler): array
|
||||
{
|
||||
// TODO: Implement execute() method.
|
||||
return $handler;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -4,20 +4,17 @@
|
||||
namespace Annotation\Route;
|
||||
|
||||
|
||||
use Annotation\IAnnotation;
|
||||
use Annotation\Attribute;
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
use ReflectionException;
|
||||
use Snowflake\Exception\NotFindClassException;
|
||||
use Snowflake\Snowflake;
|
||||
|
||||
/**
|
||||
* Class Interceptor
|
||||
* @package Annotation\Route
|
||||
*/
|
||||
#[\Attribute(\Attribute::TARGET_METHOD)] class Interceptor implements IAnnotation
|
||||
#[\Attribute(\Attribute::TARGET_METHOD)] class Interceptor extends Attribute
|
||||
{
|
||||
|
||||
use Node;
|
||||
|
||||
/**
|
||||
* Interceptor constructor.
|
||||
@@ -29,6 +26,16 @@ use Snowflake\Snowflake;
|
||||
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'];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -4,31 +4,37 @@
|
||||
namespace Annotation\Route;
|
||||
|
||||
|
||||
use Annotation\IAnnotation;
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
use ReflectionException;
|
||||
use Snowflake\Exception\NotFindClassException;
|
||||
use Annotation\Attribute;
|
||||
use Snowflake\Snowflake;
|
||||
|
||||
/**
|
||||
* Class Limits
|
||||
* @package Annotation\Route
|
||||
*/
|
||||
#[\Attribute(\Attribute::TARGET_METHOD)] class Limits implements IAnnotation
|
||||
#[\Attribute(\Attribute::TARGET_METHOD)] class Limits extends Attribute
|
||||
{
|
||||
|
||||
use Node;
|
||||
|
||||
/**
|
||||
* Limits constructor.
|
||||
* @param string|array $limits
|
||||
* @throws
|
||||
*/
|
||||
#[Pure] public function __construct(public string|array $limits)
|
||||
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'];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -4,32 +4,35 @@
|
||||
namespace Annotation\Route;
|
||||
|
||||
|
||||
use Annotation\IAnnotation;
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
use ReflectionException;
|
||||
use Snowflake\Exception\NotFindClassException;
|
||||
use Annotation\Attribute;
|
||||
use Snowflake\Snowflake;
|
||||
|
||||
/**
|
||||
* Class Middleware
|
||||
* @package Annotation\Route
|
||||
*/
|
||||
#[\Attribute(\Attribute::TARGET_METHOD)] class Middleware implements IAnnotation
|
||||
#[\Attribute(\Attribute::TARGET_METHOD)] class Middleware extends Attribute
|
||||
{
|
||||
|
||||
use Node;
|
||||
|
||||
|
||||
/**
|
||||
* Interceptor constructor.
|
||||
* @param string|array $middleware
|
||||
* @throws
|
||||
*/
|
||||
#[Pure] public function __construct(public string|array $middleware)
|
||||
public function __construct(public string|array $middleware)
|
||||
{
|
||||
if (is_string($this->middleware)) {
|
||||
$this->middleware = [$this->middleware];
|
||||
}
|
||||
foreach ($this->middleware as $key => $value) {
|
||||
$sn = Snowflake::createObject($value);
|
||||
|
||||
if (!($sn instanceof \HttpServer\IInterface\Middleware)) {
|
||||
continue;
|
||||
}
|
||||
$this->middleware[$key] = [$sn, 'onHandler'];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,45 +0,0 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Annotation\Route;
|
||||
|
||||
use HttpServer\IInterface\After;
|
||||
use HttpServer\IInterface\Interceptor;
|
||||
use HttpServer\IInterface\Limits;
|
||||
use HttpServer\IInterface\Middleware;
|
||||
use ReflectionException;
|
||||
use Snowflake\Exception\NotFindClassException;
|
||||
use Snowflake\Snowflake;
|
||||
|
||||
trait Node
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param array $classes
|
||||
* @return array
|
||||
* @throws ReflectionException
|
||||
* @throws NotFindClassException
|
||||
*/
|
||||
public function reflectClass(array $classes): array
|
||||
{
|
||||
$array = [];
|
||||
foreach ($classes as $class) {
|
||||
$object = Snowflake::getDi()->get($class);
|
||||
if ($object instanceof Interceptor) {
|
||||
$array[] = [$object, 'Interceptor'];
|
||||
}
|
||||
if ($object instanceof Limits) {
|
||||
$array[] = [$object, 'next'];
|
||||
}
|
||||
if ($object instanceof After) {
|
||||
$array[] = [$object, 'onHandler'];
|
||||
}
|
||||
if ($object instanceof Middleware) {
|
||||
$array[] = [$object, 'onHandler'];
|
||||
}
|
||||
}
|
||||
return $array;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -4,26 +4,27 @@
|
||||
namespace Annotation\Route;
|
||||
|
||||
|
||||
use Closure;
|
||||
use Exception;
|
||||
use HttpServer\Route\Node;
|
||||
use Annotation\Attribute;
|
||||
use HttpServer\Route\Router;
|
||||
use ReflectionException;
|
||||
use Snowflake\Exception\ComponentException;
|
||||
use Snowflake\Exception\ConfigException;
|
||||
use Snowflake\Exception\NotFindClassException;
|
||||
use Snowflake\Snowflake;
|
||||
use Annotation\IAnnotation;
|
||||
|
||||
#[\Attribute(\Attribute::TARGET_METHOD)] class Route implements IAnnotation
|
||||
#[\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 $method,
|
||||
public string $version = 'v.1.0'
|
||||
)
|
||||
{
|
||||
}
|
||||
@@ -34,13 +35,15 @@ use Annotation\IAnnotation;
|
||||
* @return Router
|
||||
* @throws ComponentException
|
||||
* @throws ConfigException
|
||||
* @throws ReflectionException
|
||||
* @throws NotFindClassException
|
||||
*/
|
||||
public function execute(array $handler): Router
|
||||
{
|
||||
// TODO: Implement setHandler() method.
|
||||
$router = Snowflake::app()->getRouter();
|
||||
|
||||
$router->addRoute($this->uri, $handler, $this->method);
|
||||
$router->addRoute($this->uri . $this->version, $handler, $this->method);
|
||||
|
||||
return $router;
|
||||
}
|
||||
|
||||
@@ -4,10 +4,7 @@
|
||||
namespace Annotation\Route;
|
||||
|
||||
|
||||
use Annotation\IAnnotation;
|
||||
use Closure;
|
||||
use Exception;
|
||||
use HttpServer\Route\Node;
|
||||
use Annotation\Attribute;
|
||||
use HttpServer\Route\Router;
|
||||
use ReflectionException;
|
||||
use Snowflake\Exception\ComponentException;
|
||||
@@ -19,7 +16,7 @@ use Snowflake\Snowflake;
|
||||
* Class Socket
|
||||
* @package Annotation
|
||||
*/
|
||||
#[\Attribute(\Attribute::TARGET_METHOD)] class Socket implements IAnnotation
|
||||
#[\Attribute(\Attribute::TARGET_METHOD)] class Socket extends Attribute
|
||||
{
|
||||
|
||||
const CLOSE = 'CLOSE';
|
||||
@@ -30,10 +27,12 @@ use Snowflake\Snowflake;
|
||||
* Socket constructor.
|
||||
* @param string $event
|
||||
* @param string|null $uri
|
||||
* @param string $version
|
||||
*/
|
||||
public function __construct(
|
||||
public string $event,
|
||||
public ?string $uri = null
|
||||
public ?string $uri = null,
|
||||
public string $version = 'v.1.0'
|
||||
)
|
||||
{
|
||||
}
|
||||
@@ -44,6 +43,8 @@ use Snowflake\Snowflake;
|
||||
* @return Router
|
||||
* @throws ComponentException
|
||||
* @throws ConfigException
|
||||
* @throws ReflectionException
|
||||
* @throws NotFindClassException
|
||||
*/
|
||||
public function execute(array $handler): Router
|
||||
{
|
||||
@@ -52,7 +53,7 @@ use Snowflake\Snowflake;
|
||||
|
||||
$method = $this->event . '::' . (is_null($this->uri) ? 'event' : $this->uri);
|
||||
|
||||
$router->addRoute($method, $handler, 'sw::socket');
|
||||
$router->addRoute($method . $this->version, $handler, 'sw::socket');
|
||||
|
||||
return $router;
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace Annotation;
|
||||
* Class Target
|
||||
* @package Annotation
|
||||
*/
|
||||
#[\Attribute(\Attribute::TARGET_CLASS)] class Target
|
||||
#[\Attribute(\Attribute::TARGET_CLASS)] class Target extends Attribute
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user