This commit is contained in:
2021-07-21 11:25:31 +08:00
parent 3b05c856ef
commit 8f6079b040
9 changed files with 201 additions and 112 deletions
+44 -44
View File
@@ -17,14 +17,14 @@ use Snowflake\Snowflake;
{
/**
* Inject constructor.
* @param string $className
* @param array $args
*/
public function __construct(private string $className, private array $args = [])
{
}
/**
* Inject constructor.
* @param string $className
* @param array $args
*/
public function __construct(private string $className, private array $args = [])
{
}
/**
@@ -34,44 +34,44 @@ use Snowflake\Snowflake;
* @throws ReflectionException
* @throws Exception
*/
public function execute(mixed $class, mixed $method = null): bool
{
$injectValue = $this->parseInjectValue();
if (!($method instanceof ReflectionProperty)) {
$method = new ReflectionProperty($class, $method);
}
public function execute(mixed $class, mixed $method = null): bool
{
$injectValue = $this->parseInjectValue();
if (!($method instanceof ReflectionProperty)) {
$method = Snowflake::getDi()->getClassProperty($class, $method);
if (!$method) {
return false;
}
}
/** @var ReflectionProperty $class */
if ($method->isPrivate() || $method->isProtected()) {
$method = 'set' . ucfirst($class->getName());
if (!method_exists($class, $method)) {
return false;
}
$class->$method($injectValue);
} else {
$class->{$method->getName()} = $injectValue;
}
return true;
}
/** @var ReflectionProperty $class */
if ($method->isPrivate() || $method->isProtected()) {
$method = 'set' . ucfirst($class->getName());
if (!method_exists($class, $method)) {
return false;
}
$class->$method($injectValue);
} else {
$class->{$method->getName()} = $injectValue;
}
return true;
}
/**
* @return mixed
* @throws Exception
*/
private function parseInjectValue(): mixed
{
if (class_exists($this->className)) {
$injectValue = Snowflake::createObject($this->className, $this->args);
} else if (Snowflake::app()->has($this->className)) {
$injectValue = Snowflake::app()->get($this->className);
} else {
$injectValue = $this->className;
}
// if (!empty($this->args) && is_object($injectValue)) {
// Snowflake::configure($injectValue, $this->args);
// }
return $injectValue;
}
/**
* @return mixed
* @throws Exception
*/
private function parseInjectValue(): mixed
{
if (class_exists($this->className)) {
$injectValue = Snowflake::getDi()->get($this->className, $this->args);
} else if (Snowflake::app()->has($this->className)) {
$injectValue = Snowflake::app()->get($this->className);
} else {
$injectValue = $this->className;
}
return $injectValue;
}
}
+29 -18
View File
@@ -27,10 +27,16 @@ class Loader extends BaseObject
{
private array $_classes = [];
private static array $_classes = [];
private array $_directory = [];
private static array $_directory = [];
private static array $_property = [];
private static array $_methods = [];
private array $_annotationMaps = [];
@@ -41,7 +47,7 @@ class Loader extends BaseObject
*/
public function getDirectory(): array
{
return $this->_directory;
return static::$_directory;
}
/**
@@ -60,7 +66,7 @@ class Loader extends BaseObject
*/
public function getClasses(): array
{
return $this->_classes;
return static::$_classes;
}
@@ -71,14 +77,13 @@ class Loader extends BaseObject
*/
public function getProperty(string $class, string $property = ''): mixed
{
if (!isset($this->_classes[$class])) {
if (!isset(static::$_property[$class])) {
return null;
}
$properties = $this->_classes[$class]['property'];
if (!empty($property) && isset($properties[$property])) {
return $properties[$property];
if (!empty($property)) {
return static::$_property[$class][$property] ?? [];
}
return $properties;
return static::$_property[$class];
}
@@ -109,10 +114,10 @@ class Loader extends BaseObject
*/
public function getMethod(string $class, string $method = ''): array
{
if (!isset($this->_classes[$class])) {
if (!isset(static::$_classes[$class])) {
return [];
}
$properties = $this->_classes[$class]['methods'];
$properties = static::$_classes[$class]['methods'];
if (!empty($method) && isset($properties[$method])) {
return $properties[$method];
}
@@ -126,7 +131,7 @@ class Loader extends BaseObject
*/
public function getTarget(string $class): array
{
return $this->_classes[$class] ?? [];
return static::$_classes[$class] ?? [];
}
@@ -144,8 +149,8 @@ class Loader extends BaseObject
if ($path->isDir()) {
$iterator = new DirectoryIterator($path->getRealPath());
$directory = rtrim($path->getRealPath(), '/');
if (!isset($this->_directory[$directory])) {
$this->_directory[$directory] = [];
if (!isset(static::$_directory[$directory])) {
static::$_directory[$directory] = [];
}
$this->_scanDir($iterator, $namespace);
} else {
@@ -181,7 +186,7 @@ class Loader extends BaseObject
$_array = $this->_methods($replace, $_array);
$_array = $this->_properties($replace, $_array);
$this->_classes[$replace->getName()] = $_array;
static::$_classes[$replace->getName()] = $_array;
} catch (Throwable $throwable) {
$this->addError($throwable, 'throwable');
}
@@ -237,6 +242,9 @@ class Loader extends BaseObject
}
$_method[] = $attribute->newInstance();
}
static::$_methods[$replace->getName()][$method->getName()] = $_method;
$_array['methods'][$method->getName()] = $_method;
}
return $_array;
@@ -260,6 +268,9 @@ class Loader extends BaseObject
}
$_property[] = $attribute->newInstance();
}
static::$_property[$replace->getName()][$method->getName()] = $_property;
$_array['property'][$method->getName()] = $_property;
}
return $_array;
@@ -275,7 +286,7 @@ class Loader extends BaseObject
{
try {
$path = '/' . trim($path, '/');
foreach ($this->_directory as $key => $_path) {
foreach (static::$_directory as $key => $_path) {
$key = '/' . trim($key, '/');
if (!str_starts_with($key, $path) || in_array($key, $outPath)) {
continue;
@@ -317,7 +328,7 @@ class Loader extends BaseObject
$array = '/' . trim(implode('/', $array), '/');
$this->_directory[$array][] = $className;
static::$_directory[$array][] = $className;
}
@@ -333,7 +344,7 @@ class Loader extends BaseObject
$annotation = Snowflake::getAnnotation();
foreach ($classes as $className) {
$annotations = $this->_classes[$className] ?? null;
$annotations = static::$_classes[$className] ?? null;
if ($annotations === null) {
continue;
}