This commit is contained in:
2021-08-24 18:24:46 +08:00
parent 575f4b8520
commit 24c93edf2b
110 changed files with 5 additions and 6 deletions
+210
View File
@@ -0,0 +1,210 @@
<?php
namespace Annotation;
use DirectoryIterator;
use Exception;
use ReflectionException;
use Kiri\Abstracts\Component;
/**
* Class Annotation
* @package Annotation
*/
class Annotation extends Component
{
private Loader $_loader;
private array $_model_sets = [];
private array $_model_gets = [];
private array $_model_relate = [];
/**
* @param string $class
* @param string $setName
* @param string $method
*/
public function addSets(string $class, string $setName, string $method)
{
$this->_model_sets[$class][$setName] = $method;
}
/**
* @param string $class
* @param string $setName
* @param string $method
*/
public function addGets(string $class, string $setName, string $method)
{
$this->_model_gets[$class][$setName] = $method;
}
/**
* @param string $class
* @param string $setName
* @param string $method
*/
public function addRelate(string $class, string $setName, string $method)
{
$this->_model_relate[$class][$setName] = $method;
}
/**
* @param $class
* @return array
*/
public function getGets($class): array
{
return $this->_model_gets[$class] ?? [];
}
/**
* @param $class
* @return array
*/
public function getSets($class): array
{
return $this->_model_gets[$class] ?? [];
}
/**
* @param string $class
* @param string|null $setName
* @return array|string|null
*/
public function getGetMethodName(string $class, string $setName = null): array|null|string
{
$gets = $this->_model_gets[$class] ?? null;
if ($gets == null) {
return null;
}
if (empty($setName)) return $gets;
return $gets[$setName] ?? null;
}
public function runGet($name, $value)
{
}
/**
* @param string $class
* @param string|null $method
* @return array|string|null
*/
public function getRelateMethods(string $class, string $method = null): array|null|string
{
$gets = $this->_model_relate[$class] ?? null;
if ($gets == null) {
return null;
}
if (empty($method)) return $gets;
return $gets[$method] ?? null;
}
/**
* @param string $class
* @param string $setName
* @return mixed|null
*/
public function getSetMethodName(string $class, string $setName): ?string
{
if (!isset($this->_model_sets[$class])) {
return null;
}
$lists = $this->_model_sets[$class];
if (isset($lists[$setName])) {
return $lists[$setName];
}
return null;
}
public function init(): void
{
$this->_loader = new Loader();
}
/**
* @return Loader
*/
public function getLoader(): Loader
{
return $this->_loader;
}
/**
* @param Loader $loader
* @return Loader
*/
public function setLoader(Loader $loader): Loader
{
return $this->_loader = $loader;
}
/**
* @param string $className
* @param string $method
* @return array 根据类名获取注解
* 根据类名获取注解
*/
public function getMethods(string $className, string $method = ''): mixed
{
return $this->_loader->getMethod($className, $method);
}
/**
* @param object $class
* @throws ReflectionException
*/
public function injectProperty(object $class)
{
$this->_loader->injectProperty($class::class, $class);
}
/**
* @param string $path
* @param string $namespace
* @param array $exclude
* @return void
* @throws Exception
*/
public function read(string $path, string $namespace = 'App', array $exclude = []): void
{
$this->_loader->_scanDir(new DirectoryIterator($path), $namespace, $exclude);
}
/**
* @param string $dir
* @param array $exclude
* @return array
* @throws Exception
*/
public function runtime(string $dir, array $exclude = []): array
{
return $this->_loader->loadByDirectory($dir, $exclude);
}
}
+40
View File
@@ -0,0 +1,40 @@
<?php
namespace Annotation;
use Exception;
use Kiri\AspectManager;
use Kiri\IAspect;
use Kiri\Kiri;
defined('ASPECT_ERROR') or define('ASPECT_ERROR', 'Aspect annotation must implement ');
/**
* Class Aspect
* @package Annotation
*/
#[\Attribute(\Attribute::TARGET_METHOD)] class Aspect extends Attribute
{
/**
* Aspect constructor.
* @param string $aspect
*/
public function __construct(public string $aspect)
{
}
/**
* @throws Exception
*/
public function execute(mixed $class, mixed $method = ''): bool
{
return true;
}
}
+44
View File
@@ -0,0 +1,44 @@
<?php
namespace Annotation;
use Exception;
use Http\IInterface\Task;
use Kiri\Kiri;
/**
* Class Asynchronous
* @package Annotation
* Task任务
*/
#[\Attribute(\Attribute::TARGET_CLASS)] class Asynchronous extends Attribute
{
/**
* Asynchronous constructor.
* @param string $name
*/
public function __construct(public string $name)
{
}
/**
* @param mixed $class
* @param mixed|null $method
* @return bool
* @throws Exception
*/
public function execute(mixed $class, mixed $method = null): bool
{
$async = Kiri::app()->getAsync();
$async->addAsync($this->name, $class);
return true;
}
}
+27
View File
@@ -0,0 +1,27 @@
<?php
namespace Annotation;
/**
* Class Attribute
* @package Annotation
*/
abstract class Attribute implements IAnnotation
{
/**
* @param mixed $class
* @param mixed|string $method
* @return mixed
*/
public function execute(mixed $class, mixed $method = ''): mixed
{
// TODO: Implement execute() method.
return true;
}
}
+45
View File
@@ -0,0 +1,45 @@
<?php
namespace Annotation;
use Exception;
use Kiri\Events\EventProvider;
/**
* Class Event
* @package Annotation
*/
#[\Attribute(\Attribute::TARGET_METHOD)] class Event extends Attribute
{
/**
* 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 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;
}
}
+20
View File
@@ -0,0 +1,20 @@
<?php
namespace Annotation;
use Closure;
interface IAnnotation
{
/**
* @param mixed $class
* @param mixed $method
* @return mixed
*/
public function execute(mixed $class, mixed $method = ''): mixed;
}
+105
View File
@@ -0,0 +1,105 @@
<?php
namespace Annotation;
use Exception;
use Kiri\Core\Str;
use Kiri\Kiri;
use ReflectionException;
use ReflectionProperty;
/**
* Class Inject
* @package Annotation
*/
#[\Attribute(\Attribute::TARGET_PROPERTY)] class Inject extends Attribute
{
/**
* Inject constructor.
* @param string $value
* @param array $construct
*/
public function __construct(private string $value, private array $construct = [])
{
}
/**
* @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 = $this->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 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 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 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);
}
}
}
+239
View File
@@ -0,0 +1,239 @@
<?php
namespace Annotation;
use DirectoryIterator;
use Exception;
use ReflectionClass;
use ReflectionException;
use Kiri\Abstracts\BaseObject;
use Kiri\Kiri;
use Throwable;
/**
* Class Loader
* @package Annotation
*/
class Loader extends BaseObject
{
private array $_classes = [];
private array $_directory = [];
private array $_property = [];
private array $_methods = [];
/**
* @param $path
* @param $namespace
* @throws Exception
*/
public function loader($path, $namespace)
{
$this->_scanDir(new DirectoryIterator($path), $namespace);
}
/**
* @param string $class
* @param string $property
* @return \ReflectionProperty|array|null
* @throws ReflectionException
*/
public function getProperty(string $class, string $property = ''): \ReflectionProperty|array|null
{
return Kiri::getDi()->getClassReflectionProperty($class, $property);
}
/**
* @param string $class
* @param object $handler
* @return $this
* @throws ReflectionException
* @throws Exception
*/
public function injectProperty(string $class, object $handler): static
{
$di = Kiri::getDi();
$reflect = $di->getReflect($class);
$di->propertyInject($reflect, $handler);
return $this;
}
/**
* @param string $class
* @param string $method
* @return mixed
*/
public function getMethod(string $class, string $method = ''): array
{
if (!isset($this->_methods[$class])) {
return [];
}
$properties = $this->_methods[$class];
if (!empty($method) && isset($properties[$method])) {
return $properties[$method];
}
return $properties;
}
/**
* @param DirectoryIterator $paths
* @param $namespace
* @param array $exclude
* @throws Exception
*/
public function _scanDir(DirectoryIterator $paths, $namespace, array $exclude = [])
{
foreach ($paths as $path) {
if ($path->isDot() || str_starts_with($path->getFilename(), '.')) {
continue;
}
if ($this->inExclude($exclude, $path->getRealPath())) {
continue;
}
if ($path->isDir()) {
$iterator = new DirectoryIterator($path->getRealPath());
$directory = rtrim($path->getRealPath(), '/');
if (!isset($this->_directory[$directory])) {
$this->_directory[$directory] = [];
}
$this->_scanDir($iterator, $namespace);
} else {
$this->readFile($path, $namespace);
}
}
}
/**
* @param DirectoryIterator $path
* @param $namespace
* @throws Exception
*/
private function readFile(DirectoryIterator $path, $namespace)
{
try {
if ($path->getExtension() !== 'php') {
return;
}
$replace = $this->getReflect($path, $namespace);
if (!$replace->getAttributes(Target::class)) {
return;
}
$this->appendFileToDirectory($path->getRealPath(), $replace->getName());
} catch (Throwable $throwable) {
$this->error(jTraceEx($throwable), 'throwable');
}
}
/**
* @param DirectoryIterator $path
* @param string $namespace
* @return ReflectionClass|null
* @throws ReflectionException
*/
private function getReflect(DirectoryIterator $path, string $namespace): ?ReflectionClass
{
return Kiri::getDi()->getReflect($this->explodeFileName($path, $namespace));
}
/**
* @param string $path
* @param array $exclude
* @return array
* @throws Exception
*/
public function loadByDirectory(string $path, array $exclude = []): array
{
try {
$path = '/' . trim($path, '/');
$paths = [];
foreach ($this->_directory as $key => $_path) {
$key = '/' . trim($key, '/');
if (!str_starts_with($key, $path) || $this->inExclude($exclude, $path)) {
continue;
}
unset($this->_directory[$key]);
foreach ($_path as $item) {
$paths[] = $item;
}
}
return $paths;
} catch (Throwable $exception) {
$this->addError($exception, 'throwable');
return [];
}
}
/**
* @param array $exclude
* @param $path
* @return bool
*/
private function inExclude(array $exclude, $path): bool
{
if (empty($exclude)) {
return false;
}
foreach ($exclude as $value) {
if (str_starts_with($path, $value)) {
return true;
}
}
return false;
}
/**
* @param DirectoryIterator $path
* @param string $namespace
* @return string
*/
private function explodeFileName(DirectoryIterator $path, string $namespace): string
{
$replace = str_replace(APP_PATH . 'app', '', $path->getRealPath());
$replace = str_replace('.php', '', $replace);
$replace = str_replace(DIRECTORY_SEPARATOR, '\\', $replace);
$explode = explode('\\', $replace);
array_shift($explode);
return $namespace . '\\' . implode('\\', $explode);
}
/**
* @param string $filePath
* @param string $className
*/
public function appendFileToDirectory(string $filePath, string $className)
{
$array = explode('/', $filePath);
unset($array[count($array) - 1]);
$array = '/' . trim(implode('/', $array), '/');
$this->_directory[$array][] = $className;
}
}
+55
View File
@@ -0,0 +1,55 @@
<?php
namespace Annotation;
use Exception;
use Kiri\Events\EventProvider;
use Kiri\Kiri;
use Server\Events\OnWorkerExit;
/**
* Class LocalService
* @package Annotation
*/
#[\Attribute(\Attribute::TARGET_CLASS)] class LocalService extends Attribute
{
/**
* 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) {
return;
}
$pro = di(EventProvider::class);
$pro->on(OnWorkerExit::class, function () {
di(\Kiri\Di\LocalService::class)->remove($this->service);
},0);
}
/**
* @param object $class
* @param string $method
* @return bool
* @throws Exception
*/
public function execute(mixed $class, mixed $method = null): bool
{
$class = ['class' => $class];
if (!empty($this->args)) {
$class = array_merge($class, $this->args);
}
Kiri::set($this->service, $class);
return true; // TODO: Change the autogenerated stub
}
}
+17
View File
@@ -0,0 +1,17 @@
<?php
namespace Annotation;
interface Porters
{
/**
* @return mixed
*/
public function process(): mixed;
}
+48
View File
@@ -0,0 +1,48 @@
<?php
namespace Annotation\Route;
use Annotation\Attribute;
/**
* Class Document
* @package Annotation\Route
*/
#[\Attribute(\Attribute::TARGET_METHOD)] class Document extends Attribute
{
const INTEGER = 'int';
const STRING = 'string';
const BOOLEAN = 'bool';
const FLOAT = 'float';
const ALIAS = [
self::INTEGER => '整数',
self::STRING => '字符串',
self::BOOLEAN => '布尔值',
self::FLOAT => '浮点',
];
public function __construct(
public array $request,
public array $response
)
{
}
/**
* @param mixed $class
* @param mixed|null $method
* @return array
*/
public function execute(mixed $class, mixed $method = null): array
{
// TODO: Implement execute() method.
return [$this->request, $this->response];
}
}
+42
View File
@@ -0,0 +1,42 @@
<?php
namespace Annotation\Route;
use Annotation\Attribute;
use Exception;
use Http\HttpFilter;
use ReflectionException;
use Kiri\Exception\ComponentException;
use Kiri\Exception\NotFindClassException;
use Kiri\Kiri;
/**
* Class Filter
* @package Annotation\Route
*/
#[\Attribute(\Attribute::TARGET_METHOD)] class Filter extends Attribute
{
/**
* Filter constructor.
* @param array $rules
*/
public function __construct(public array $rules)
{
}
/**
* @param mixed $class
* @param mixed|null $method
* @return bool
*/
public function execute(mixed $class, mixed $method = null): bool
{
return true;
}
}
+58
View File
@@ -0,0 +1,58 @@
<?php
namespace Annotation\Route;
use Annotation\Attribute;
use Http\Route\MiddlewareManager;
use ReflectionException;
use Kiri\Kiri;
use Http\IInterface\MiddlewareInterface ;
/**
* Class Middleware
* @package Annotation\Route
*/
#[\Attribute(\Attribute::TARGET_METHOD)] class Middleware extends Attribute
{
/**
* 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 $value) {
$sn = di($value);
if (!($sn instanceof MiddlewareInterface)) {
continue;
}
$array[] = [$sn, 'onHandler'];
}
$this->middleware = $array;
}
/**
* @param mixed $class
* @param mixed|null $method
* @return $this
* @throws ReflectionException
*/
public function execute(mixed $class, mixed $method = null): static
{
$middleware = Kiri::getDi()->get(MiddlewareManager::class);
$middleware->addMiddlewares($class, $method, $this->middleware);
return $this;
}
}
+49
View File
@@ -0,0 +1,49 @@
<?php
namespace Annotation\Route;
use Annotation\Attribute;
use Exception;
use Http\Route\Router;
use Kiri\Kiri;
#[\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'
)
{
}
/**
* @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 = Kiri::app()->getRouter();
if (is_string($class)) {
$class = di($class);
}
$router->addRoute($this->uri, [$class, $method], $this->method);
return $router;
}
}
+52
View File
@@ -0,0 +1,52 @@
<?php
namespace Annotation\Route;
use Annotation\Attribute;
use Exception;
use Http\Route\Router;
use Kiri\Kiri;
/**
* Class Socket
* @package Annotation
*/
#[\Attribute(\Attribute::TARGET_METHOD)] class Socket extends Attribute
{
const CLOSE = 'CLOSE';
const MESSAGE = 'MESSAGE';
const HANDSHAKE = 'HANDSHAKE';
/**
* Socket constructor.
* @param string $event
* @param string|null $uri
* @param string $version
*/
public function __construct(public string $event, public ?string $uri = null, public string $version = 'v.1.0')
{
}
/**
* @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 = Kiri::app()->getRouter();
$path = $this->event . '::' . (is_null($this->uri) ? 'event' : $this->uri);
$router->addRoute($path, [di($class), $method], 'sw::socket');
return $router;
}
}
+28
View File
@@ -0,0 +1,28 @@
<?php
namespace Annotation;
/**
* Class Target
* @package Annotation
*/
#[\Attribute(\Attribute::TARGET_CLASS)] class Target extends Attribute
{
const WORKER = 'worker';
const ALL = 'any';
const PROCESS = 'process';
const TASK = 'task';
/**
* @param string $only
*/
public function __construct(public string $only = Target::ALL)
{
}
}