改名
This commit is contained in:
@@ -19,11 +19,12 @@ use Snowflake\Snowflake;
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* Inject constructor.
|
||||
* @param string $value
|
||||
* @param array $args
|
||||
*/
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
+169
-172
@@ -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;
|
||||
@@ -27,204 +22,206 @@ class Loader extends BaseObject
|
||||
{
|
||||
|
||||
|
||||
private static array $_classes = [];
|
||||
private static array $_classes = [];
|
||||
|
||||
|
||||
private static array $_directory = [];
|
||||
private static array $_directory = [];
|
||||
|
||||
|
||||
private static array $_property = [];
|
||||
private static array $_property = [];
|
||||
|
||||
|
||||
private static array $_methods = [];
|
||||
private static array $_methods = [];
|
||||
|
||||
|
||||
/**
|
||||
* @param $path
|
||||
* @param $namespace
|
||||
* @throws Exception
|
||||
*/
|
||||
public function loader($path, $namespace)
|
||||
{
|
||||
$this->_scanDir(new DirectoryIterator($path), $namespace);
|
||||
}
|
||||
/**
|
||||
* @param $path
|
||||
* @param $namespace
|
||||
* @throws Exception
|
||||
*/
|
||||
public function loader($path, $namespace)
|
||||
{
|
||||
$this->_scanDir(new DirectoryIterator($path), $namespace);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $class
|
||||
* @param string $property
|
||||
* @return mixed
|
||||
*/
|
||||
public function getProperty(string $class, string $property = ''): mixed
|
||||
{
|
||||
if (!isset(static::$_property[$class])) {
|
||||
return null;
|
||||
}
|
||||
if (!empty($property)) {
|
||||
return static::$_property[$class][$property] ?? [];
|
||||
}
|
||||
return static::$_property[$class];
|
||||
}
|
||||
/**
|
||||
* @param string $class
|
||||
* @param string $property
|
||||
* @return mixed
|
||||
*/
|
||||
public function getProperty(string $class, string $property = ''): mixed
|
||||
{
|
||||
return Snowflake::getDi()->getClassReflectionProperty($class, $property);
|
||||
|
||||
if (!isset(static::$_property[$class])) {
|
||||
return null;
|
||||
}
|
||||
if (!empty($property)) {
|
||||
return static::$_property[$class][$property] ?? [];
|
||||
}
|
||||
return static::$_property[$class];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @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 object $handler
|
||||
* @return $this
|
||||
* @throws NotFindClassException
|
||||
* @throws ReflectionException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function injectProperty(string $class, object $handler): static
|
||||
{
|
||||
$di = Snowflake::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(static::$_methods[$class])) {
|
||||
return [];
|
||||
}
|
||||
$properties = static::$_methods[$class];
|
||||
if (!empty($method) && isset($properties[$method])) {
|
||||
return $properties[$method];
|
||||
}
|
||||
return $properties;
|
||||
}
|
||||
/**
|
||||
* @param string $class
|
||||
* @param string $method
|
||||
* @return mixed
|
||||
*/
|
||||
public function getMethod(string $class, string $method = ''): array
|
||||
{
|
||||
if (!isset(static::$_methods[$class])) {
|
||||
return [];
|
||||
}
|
||||
$properties = static::$_methods[$class];
|
||||
if (!empty($method) && isset($properties[$method])) {
|
||||
return $properties[$method];
|
||||
}
|
||||
return $properties;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param DirectoryIterator $paths
|
||||
* @param $namespace
|
||||
* @throws Exception
|
||||
*/
|
||||
public function _scanDir(DirectoryIterator $paths, $namespace)
|
||||
{
|
||||
foreach ($paths as $path) {
|
||||
if ($path->isDot() || str_starts_with($path->getFilename(), '.')) {
|
||||
continue;
|
||||
}
|
||||
if ($path->isDir()) {
|
||||
$iterator = new DirectoryIterator($path->getRealPath());
|
||||
$directory = rtrim($path->getRealPath(), '/');
|
||||
if (!isset(static::$_directory[$directory])) {
|
||||
static::$_directory[$directory] = [];
|
||||
}
|
||||
$this->_scanDir($iterator, $namespace);
|
||||
} else {
|
||||
$this->readFile($path, $namespace);
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @param DirectoryIterator $paths
|
||||
* @param $namespace
|
||||
* @throws Exception
|
||||
*/
|
||||
public function _scanDir(DirectoryIterator $paths, $namespace)
|
||||
{
|
||||
foreach ($paths as $path) {
|
||||
if ($path->isDot() || str_starts_with($path->getFilename(), '.')) {
|
||||
continue;
|
||||
}
|
||||
if ($path->isDir()) {
|
||||
$iterator = new DirectoryIterator($path->getRealPath());
|
||||
$directory = rtrim($path->getRealPath(), '/');
|
||||
if (!isset(static::$_directory[$directory])) {
|
||||
static::$_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 (empty($replace) || count($replace->getAttributes(Target::class)) < 1) {
|
||||
return;
|
||||
}
|
||||
$this->appendFileToDirectory($path->getRealPath(), $replace->getName());
|
||||
/**
|
||||
* @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 (empty($replace) || count($replace->getAttributes(Target::class)) < 1) {
|
||||
return;
|
||||
}
|
||||
$this->appendFileToDirectory($path->getRealPath(), $replace->getName());
|
||||
|
||||
static::$_classes[] = $replace->getName();
|
||||
} catch (Throwable $throwable) {
|
||||
$this->addError($throwable, 'throwable');
|
||||
}
|
||||
}
|
||||
static::$_classes[] = $replace->getName();
|
||||
} catch (Throwable $throwable) {
|
||||
$this->addError($throwable, 'throwable');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param DirectoryIterator $path
|
||||
* @param string $namespace
|
||||
* @return ReflectionClass|null
|
||||
* @throws ReflectionException
|
||||
* @throws NotFindClassException
|
||||
*/
|
||||
private function getReflect(DirectoryIterator $path, string $namespace): ?ReflectionClass
|
||||
{
|
||||
return Snowflake::getDi()->getReflect($this->explodeFileName($path, $namespace));
|
||||
}
|
||||
/**
|
||||
* @param DirectoryIterator $path
|
||||
* @param string $namespace
|
||||
* @return ReflectionClass|null
|
||||
* @throws ReflectionException
|
||||
* @throws NotFindClassException
|
||||
*/
|
||||
private function getReflect(DirectoryIterator $path, string $namespace): ?ReflectionClass
|
||||
{
|
||||
return Snowflake::getDi()->getReflect($this->explodeFileName($path, $namespace));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @param string|array $outPath
|
||||
* @throws Exception
|
||||
*/
|
||||
public function loadByDirectory(string $path)
|
||||
{
|
||||
try {
|
||||
$path = '/' . trim($path, '/');
|
||||
/**
|
||||
* @param string $path
|
||||
* @param string|array $outPath
|
||||
* @throws Exception
|
||||
*/
|
||||
public function loadByDirectory(string $path)
|
||||
{
|
||||
try {
|
||||
$path = '/' . trim($path, '/');
|
||||
|
||||
$paths = [];
|
||||
foreach (static::$_directory as $key => $_path) {
|
||||
$key = '/' . trim($key, '/');
|
||||
if (!str_starts_with($key, $path)) {
|
||||
continue;
|
||||
}
|
||||
foreach ($_path as $item) {
|
||||
$paths[] = $item;
|
||||
}
|
||||
}
|
||||
return $paths;
|
||||
} catch (Throwable $exception) {
|
||||
$this->addError($exception, 'throwable');
|
||||
return [];
|
||||
}
|
||||
}
|
||||
$paths = [];
|
||||
foreach (static::$_directory as $key => $_path) {
|
||||
$key = '/' . trim($key, '/');
|
||||
if (!str_starts_with($key, $path)) {
|
||||
continue;
|
||||
}
|
||||
foreach ($_path as $item) {
|
||||
$paths[] = $item;
|
||||
}
|
||||
}
|
||||
return $paths;
|
||||
} catch (Throwable $exception) {
|
||||
$this->addError($exception, 'throwable');
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param DirectoryIterator $path
|
||||
* @param string $namespace
|
||||
* @return string
|
||||
*/
|
||||
private function explodeFileName(DirectoryIterator $path, string $namespace): string
|
||||
{
|
||||
$replace = str_replace(APP_PATH . 'app', '', $path->getRealPath());
|
||||
/**
|
||||
* @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);
|
||||
$replace = str_replace('.php', '', $replace);
|
||||
$replace = str_replace(DIRECTORY_SEPARATOR, '\\', $replace);
|
||||
$explode = explode('\\', $replace);
|
||||
array_shift($explode);
|
||||
|
||||
return $namespace . '\\' . implode('\\', $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]);
|
||||
/**
|
||||
* @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), '/');
|
||||
$array = '/' . trim(implode('/', $array), '/');
|
||||
|
||||
static::$_directory[$array][] = $className;
|
||||
}
|
||||
static::$_directory[$array][] = $className;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user