改名
This commit is contained in:
@@ -42,6 +42,8 @@ class Command extends Component
|
|||||||
/** @var string */
|
/** @var string */
|
||||||
private string $_modelName;
|
private string $_modelName;
|
||||||
|
|
||||||
|
public string $dbname = '';
|
||||||
|
|
||||||
private ?PDOStatement $prepare = null;
|
private ?PDOStatement $prepare = null;
|
||||||
|
|
||||||
|
|
||||||
@@ -130,9 +132,7 @@ class Command extends Component
|
|||||||
if (microtime(true) - $time >= 0.02) {
|
if (microtime(true) - $time >= 0.02) {
|
||||||
$this->warning('Mysql:' . Json::encode([$this->sql, $this->params]) . (microtime(true) - $time));
|
$this->warning('Mysql:' . Json::encode([$this->sql, $this->params]) . (microtime(true) - $time));
|
||||||
}
|
}
|
||||||
if ($this->prepare) {
|
$this->prepare?->closeCursor();
|
||||||
$this->prepare->closeCursor();
|
|
||||||
}
|
|
||||||
return $result;
|
return $result;
|
||||||
} catch (\Throwable $exception) {
|
} catch (\Throwable $exception) {
|
||||||
return $this->addError($this->sql . '. error: ' . $exception->getMessage(), 'mysql');
|
return $this->addError($this->sql . '. error: ' . $exception->getMessage(), 'mysql');
|
||||||
|
|||||||
@@ -42,9 +42,6 @@ class Connection extends Component
|
|||||||
|
|
||||||
public int $timeout = 1900;
|
public int $timeout = 1900;
|
||||||
|
|
||||||
public int $maxNumber = 30;
|
|
||||||
public int $minNumber = 10;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var bool
|
* @var bool
|
||||||
* enable database cache
|
* enable database cache
|
||||||
@@ -85,9 +82,8 @@ class Connection extends Component
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$this->beginTransaction();
|
$this->beginTransaction();
|
||||||
|
Event::on(Connection::TRANSACTION_ROLLBACK, [$this, 'rollback'], [], true);
|
||||||
Event::on(Connection::TRANSACTION_COMMIT, [$this, 'commit'], false, true);
|
Event::on(Connection::TRANSACTION_COMMIT, [$this, 'commit'], [], true);
|
||||||
Event::on(Connection::TRANSACTION_ROLLBACK, [$this, 'rollback'], false, true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -26,21 +26,21 @@ class Controller
|
|||||||
*
|
*
|
||||||
* @var Request|null
|
* @var Request|null
|
||||||
*/
|
*/
|
||||||
#[Inject(value: Request::class)]
|
#[Inject('request')]
|
||||||
public ?Request $request = null;
|
public ?Request $request = null;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var HttpParams|null
|
* @var HttpParams|null
|
||||||
*/
|
*/
|
||||||
#[Inject(value: HttpParams::class)]
|
#[Inject('input')]
|
||||||
public ?HttpParams $input = null;
|
public ?HttpParams $input = null;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var HttpHeaders|null
|
* @var HttpHeaders|null
|
||||||
*/
|
*/
|
||||||
#[Inject(value: HttpHeaders::class)]
|
#[Inject('header')]
|
||||||
public ?HttpHeaders $header = null;
|
public ?HttpHeaders $header = null;
|
||||||
|
|
||||||
|
|
||||||
@@ -49,7 +49,7 @@ class Controller
|
|||||||
*
|
*
|
||||||
* @var Response|null
|
* @var Response|null
|
||||||
*/
|
*/
|
||||||
#[Inject(value: Response::class)]
|
#[Inject('response')]
|
||||||
public ?Response $response = null;
|
public ?Response $response = null;
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -127,7 +127,7 @@ class Node extends HttpService
|
|||||||
* @throws NotFindClassException
|
* @throws NotFindClassException
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
#[Pure] private function createDispatch(): Closure
|
private function createDispatch(): Closure
|
||||||
{
|
{
|
||||||
/** @var Aop $aop */
|
/** @var Aop $aop */
|
||||||
$aop = Snowflake::app()->get('aop');
|
$aop = Snowflake::app()->get('aop');
|
||||||
@@ -157,7 +157,6 @@ class Node extends HttpService
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $application
|
|
||||||
* @param $handler
|
* @param $handler
|
||||||
* @return Closure
|
* @return Closure
|
||||||
*/
|
*/
|
||||||
|
|||||||
+432
-404
@@ -9,7 +9,6 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Snowflake\Di;
|
namespace Snowflake\Di;
|
||||||
|
|
||||||
use Annotation\Attribute;
|
|
||||||
use Annotation\Inject;
|
use Annotation\Inject;
|
||||||
use Annotation\Target;
|
use Annotation\Target;
|
||||||
use Exception;
|
use Exception;
|
||||||
@@ -29,239 +28,258 @@ use Snowflake\Snowflake;
|
|||||||
class Container extends BaseObject
|
class Container extends BaseObject
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
*
|
*
|
||||||
* instance class by className
|
* instance class by className
|
||||||
*/
|
*/
|
||||||
private static array $_singletons = [];
|
private static array $_singletons = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
*
|
*
|
||||||
* class new instance construct parameter
|
* class new instance construct parameter
|
||||||
*/
|
*/
|
||||||
private static array $_constructs = [];
|
private static array $_constructs = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
*
|
*
|
||||||
* implements \ReflectClass
|
* implements \ReflectClass
|
||||||
*/
|
*/
|
||||||
private static array $_reflection = [];
|
private static array $_reflection = [];
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var ReflectionProperty[]
|
* @var ReflectionProperty[]
|
||||||
*/
|
*/
|
||||||
private static array $_reflectionProperty = [];
|
private static array $_reflectionProperty = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var ReflectionMethod[]
|
* @var ReflectionMethod[]
|
||||||
*/
|
*/
|
||||||
private static array $_reflectionMethod = [];
|
private static array $_reflectionMethod = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var ReflectionClass[]
|
* @var ReflectionClass[]
|
||||||
*/
|
*/
|
||||||
private static array $_reflectionClass = [];
|
private static array $_reflectionClass = [];
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
private static array $_propertyAttributes = [];
|
private static array $_propertyAttributes = [];
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
private static array $_methodsAttributes = [];
|
private static array $_methodsAttributes = [];
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
private static array $_targetAttributes = [];
|
private static array $_targetAttributes = [];
|
||||||
|
|
||||||
|
|
||||||
private static array $_attributeMaping = [];
|
private static array $_attributeMapping = [];
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
*
|
*
|
||||||
* The construct parameter
|
* The construct parameter
|
||||||
*/
|
*/
|
||||||
private static array $_param = [];
|
private static array $_param = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $class
|
* @param $class
|
||||||
* @param array $constrict
|
* @param array $constrict
|
||||||
* @param array $config
|
* @param array $config
|
||||||
*
|
*
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws NotFindClassException
|
* @throws NotFindClassException
|
||||||
* @throws ReflectionException
|
* @throws ReflectionException
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function get($class, array $constrict = [], array $config = []): mixed
|
public function get($class, array $constrict = [], array $config = []): mixed
|
||||||
{
|
{
|
||||||
if (isset(static::$_singletons[$class])) {
|
if (isset(static::$_singletons[$class])) {
|
||||||
return static::$_singletons[$class];
|
return static::$_singletons[$class];
|
||||||
} else if (!isset(static::$_constructs[$class])) {
|
} else if (!isset(static::$_constructs[$class])) {
|
||||||
return $this->resolve($class, $constrict, $config);
|
return $this->resolve($class, $constrict, $config);
|
||||||
}
|
}
|
||||||
|
$definition = static::$_constructs[$class];
|
||||||
$definition = static::$_constructs[$class];
|
if (is_callable($definition, TRUE)) {
|
||||||
if (is_callable($definition, TRUE)) {
|
return call_user_func($definition, $this, $constrict, $config);
|
||||||
return call_user_func($definition, $this, $constrict, $config);
|
} else if (is_array($definition)) {
|
||||||
} else if (is_array($definition)) {
|
return static::$_singletons[$class] = $this->resolveDefinition($definition, $class, $config, $constrict);
|
||||||
$object = $this->resolveDefinition($definition, $class, $config, $constrict);
|
} else if (is_object($definition)) {
|
||||||
} else if (is_object($definition)) {
|
return static::$_singletons[$class] = $definition;
|
||||||
return static::$_singletons[$class] = $definition;
|
} else {
|
||||||
} else {
|
throw new NotFindClassException($class);
|
||||||
throw new NotFindClassException($class);
|
}
|
||||||
}
|
}
|
||||||
return static::$_singletons[$class] = $object;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $definition
|
* @param $definition
|
||||||
* @param $class
|
* @param $class
|
||||||
* @param $config
|
* @param $config
|
||||||
* @param $constrict
|
* @param $constrict
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws NotFindClassException
|
* @throws NotFindClassException
|
||||||
* @throws ReflectionException
|
* @throws ReflectionException
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
private function resolveDefinition($definition, $class, $config, $constrict): mixed
|
private function resolveDefinition($definition, $class, $config, $constrict): mixed
|
||||||
{
|
{
|
||||||
if (!isset($definition['class'])) {
|
if (!isset($definition['class'])) {
|
||||||
throw new NotFindClassException($class);
|
throw new NotFindClassException($class);
|
||||||
}
|
}
|
||||||
$_className = $definition['class'];
|
$_className = $definition['class'];
|
||||||
unset($definition['class']);
|
unset($definition['class']);
|
||||||
|
|
||||||
// $config = array_merge($definition, $config);
|
$definition = $this->mergeParam($class, $constrict);
|
||||||
$definition = $this->mergeParam($class, $constrict);
|
|
||||||
|
|
||||||
if ($_className === $class) {
|
if ($_className === $class) {
|
||||||
$object = $this->resolve($class, $definition, $config);
|
$object = $this->resolve($class, $definition, $config);
|
||||||
} else {
|
} else {
|
||||||
$object = $this->get($class, $definition, $config);
|
$object = $this->get($class, $definition, $config);
|
||||||
}
|
}
|
||||||
return $object;
|
return $object;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $class
|
* @param $class
|
||||||
* @param $constrict
|
* @param $constrict
|
||||||
* @param $config
|
* @param $config
|
||||||
*
|
*
|
||||||
* @return object
|
* @return object
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
private function resolve($class, $constrict, $config): object
|
private function resolve($class, $constrict, $config): object
|
||||||
{
|
{
|
||||||
/** @var ReflectionClass $reflect */
|
/** @var ReflectionClass $reflect */
|
||||||
[$reflect, $dependencies] = $this->resolveDependencies($class);
|
[$reflect, $dependencies] = $this->resolveDependencies($class, $constrict);
|
||||||
if (empty($reflect) || !$reflect->isInstantiable()) {
|
if (empty($reflect) || !$reflect->isInstantiable()) {
|
||||||
throw new NotFindClassException($class);
|
throw new NotFindClassException($class);
|
||||||
}
|
}
|
||||||
foreach ($constrict as $key => $item) {
|
$object = $this->setConfig($config, $reflect, $dependencies, $class);
|
||||||
$dependencies[$key] = $item;
|
|
||||||
}
|
|
||||||
unset($dependencies['class']);
|
|
||||||
if (empty($config) || !is_array($config)) {
|
|
||||||
$object = $this->newInstance($reflect, $dependencies);
|
|
||||||
} else if (!empty($dependencies) && $reflect->implementsInterface(Configure::class)) {
|
|
||||||
$dependencies[count($dependencies) - 1] = $config;
|
|
||||||
$object = $this->newInstance($reflect, $dependencies);
|
|
||||||
} else {
|
|
||||||
if (!empty($config)) static::$_param[$class] = $config;
|
|
||||||
|
|
||||||
$object = $this->onAfterInit($this->newInstance($reflect, $dependencies), $config);
|
return $this->setClassAnnotation($reflect, $object);
|
||||||
}
|
}
|
||||||
static::$_reflectionClass[$reflect->getName()] = [];
|
|
||||||
foreach ($reflect->getAttributes() as $attribute) {
|
|
||||||
static::$_reflectionClass[$reflect->getName()][] = $attribute->newInstance();
|
|
||||||
}
|
|
||||||
return $this->propertyInject($reflect, $object);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $reflect
|
* @param $reflect
|
||||||
* @param $dependencies
|
* @param $object
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
* @throws Exception
|
||||||
private function newInstance($reflect, $dependencies): mixed
|
*/
|
||||||
{
|
private function setClassAnnotation($reflect, $object): mixed
|
||||||
if (!empty($dependencies)) {
|
{
|
||||||
return $reflect->newInstanceArgs($dependencies);
|
static::$_reflectionClass[$reflect->getName()] = [];
|
||||||
}
|
foreach ($reflect->getAttributes() as $attribute) {
|
||||||
return $reflect->newInstance();
|
static::$_reflectionClass[$reflect->getName()][] = $attribute->newInstance();
|
||||||
}
|
}
|
||||||
|
return $this->propertyInject($reflect, $object);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param ReflectionClass $reflect
|
* @param $config
|
||||||
* @param $object
|
* @param $reflect
|
||||||
* @return mixed
|
* @param $dependencies
|
||||||
* @throws Exception
|
* @param $class
|
||||||
*/
|
* @return mixed
|
||||||
public function propertyInject(ReflectionClass $reflect, $object): mixed
|
*/
|
||||||
{
|
private function setConfig($config, $reflect, $dependencies, $class): mixed
|
||||||
if (!isset(static::$_propertyAttributes[$reflect->getName()])) {
|
{
|
||||||
return $object;
|
if (empty($config) || !is_array($config)) {
|
||||||
}
|
$object = $this->newInstance($reflect, $dependencies);
|
||||||
foreach (static::$_propertyAttributes[$reflect->getName()] as $property => $inject) {
|
} else if (!empty($dependencies) && $reflect->implementsInterface(Configure::class)) {
|
||||||
/** @var Inject $inject */
|
$dependencies[count($dependencies) - 1] = $config;
|
||||||
$inject->execute($object, $property);
|
$object = $this->newInstance($reflect, $dependencies);
|
||||||
}
|
} else {
|
||||||
return $object;
|
static::$_param[$class] = $config;
|
||||||
}
|
|
||||||
|
$object = $this->onAfterInit($this->newInstance($reflect, $dependencies), $config);
|
||||||
|
}
|
||||||
|
return $object;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param ReflectionClass $class
|
* @param $reflect
|
||||||
*/
|
* @param $dependencies
|
||||||
private function resolveMethodAttribute(ReflectionClass $class)
|
* @return mixed
|
||||||
{
|
*/
|
||||||
if ($class->isAbstract() || $class->isTrait()) {
|
private function newInstance($reflect, $dependencies): mixed
|
||||||
return;
|
{
|
||||||
}
|
if (!empty($dependencies)) {
|
||||||
foreach ($class->getMethods() as $method) {
|
return $reflect->newInstanceArgs($dependencies);
|
||||||
if ($method->isStatic()) {
|
}
|
||||||
continue;
|
return $reflect->newInstance();
|
||||||
}
|
}
|
||||||
$this->setReflectionMethod($class, $method);
|
|
||||||
foreach ($method->getAttributes() as $attribute) {
|
|
||||||
if (!class_exists($attribute->getName())) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->setMethodsAttributes($class, $method, $attribute->newInstance());
|
|
||||||
}
|
/**
|
||||||
}
|
* @param ReflectionClass $reflect
|
||||||
}
|
* @param $object
|
||||||
|
* @return mixed
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function propertyInject(ReflectionClass $reflect, $object): mixed
|
||||||
|
{
|
||||||
|
if (!isset(static::$_propertyAttributes[$reflect->getName()])) {
|
||||||
|
return $object;
|
||||||
|
}
|
||||||
|
foreach (static::$_propertyAttributes[$reflect->getName()] as $property => $inject) {
|
||||||
|
/** @var Inject $inject */
|
||||||
|
$inject->execute($object, $property);
|
||||||
|
}
|
||||||
|
return $object;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param ReflectionClass $class
|
||||||
|
*/
|
||||||
|
private function resolveMethodAttribute(ReflectionClass $class)
|
||||||
|
{
|
||||||
|
if ($class->isAbstract() || $class->isTrait()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
foreach ($class->getMethods() as $method) {
|
||||||
|
if ($method->isStatic()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$this->setReflectionMethod($class, $method);
|
||||||
|
foreach ($method->getAttributes() as $attribute) {
|
||||||
|
if (!class_exists($attribute->getName())) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$this->setMethodsAttributes($class, $method, $attribute->newInstance());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param ReflectionClass $class
|
* @param ReflectionClass $class
|
||||||
* @param ReflectionMethod $method
|
* @param ReflectionMethod $method
|
||||||
*/
|
*/
|
||||||
private function setReflectionMethod(ReflectionClass $class, ReflectionMethod $method)
|
private function setReflectionMethod(ReflectionClass $class, ReflectionMethod $method)
|
||||||
{
|
{
|
||||||
if (!isset(static::$_attributeMaping[$class->getName()])) {
|
if (!isset(static::$_attributeMapping[$class->getName()])) {
|
||||||
static::$_attributeMaping[$class->getName()] = [];
|
static::$_attributeMapping[$class->getName()] = [];
|
||||||
}
|
}
|
||||||
static::$_attributeMaping[$class->getName()][$method->getName()][] = $method;
|
static::$_attributeMapping[$class->getName()][$method->getName()][] = $method;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -269,231 +287,241 @@ class Container extends BaseObject
|
|||||||
* @param ReflectionMethod $method
|
* @param ReflectionMethod $method
|
||||||
* @param $object
|
* @param $object
|
||||||
*/
|
*/
|
||||||
private function setMethodsAttributes(ReflectionClass $attribute, ReflectionMethod $method, $object)
|
private function setMethodsAttributes(ReflectionClass $attribute, ReflectionMethod $method, $object)
|
||||||
{
|
{
|
||||||
if (!isset(static::$_methodsAttributes[$attribute->getName()])) {
|
if (!isset(static::$_methodsAttributes[$attribute->getName()])) {
|
||||||
static::$_methodsAttributes[$attribute->getName()] = [];
|
static::$_methodsAttributes[$attribute->getName()] = [];
|
||||||
}
|
}
|
||||||
static::$_methodsAttributes[$attribute->getName()][$method->getName()][] = $object;
|
static::$_methodsAttributes[$attribute->getName()][$method->getName()][] = $object;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param ReflectionClass $class
|
* @param ReflectionClass $class
|
||||||
*/
|
*/
|
||||||
private function resolveTargetAttribute(ReflectionClass $class)
|
private function resolveTargetAttribute(ReflectionClass $class)
|
||||||
{
|
{
|
||||||
if ($class->isAbstract() || $class->isTrait()) {
|
if ($class->isAbstract() || $class->isTrait()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
foreach ($class->getAttributes() as $method) {
|
foreach ($class->getAttributes() as $method) {
|
||||||
if ($method->getName() == Target::class) {
|
if ($method->getName() == Target::class) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
static::$_targetAttributes[$class->getName()] = $method->newInstance();
|
static::$_targetAttributes[$class->getName()] = $method->newInstance();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $className
|
* @param $className
|
||||||
* @param $method
|
* @param $method
|
||||||
* @return ReflectionMethod|null
|
* @return ReflectionMethod|null
|
||||||
*/
|
*/
|
||||||
public function getReflectionMethod($className, $method): ?ReflectionMethod
|
public function getReflectionMethod($className, $method): ?ReflectionMethod
|
||||||
{
|
{
|
||||||
return static::$_reflectionMethod[$className][$method] ?? null;
|
return static::$_reflectionMethod[$className][$method] ?? null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $className
|
* @param $className
|
||||||
* @param $method
|
* @return ReflectionMethod|null
|
||||||
* @return array
|
*/
|
||||||
*/
|
public function getTargetAnnotation($className): ?ReflectionMethod
|
||||||
public function getMethodAttribute($className, $method = null): array
|
{
|
||||||
{
|
return static::$_targetAttributes[$className] ?? null;
|
||||||
$methods = static::$_methodsAttributes[$className] ?? [];
|
}
|
||||||
if ($method === null || empty($method)) {
|
|
||||||
return $methods;
|
|
||||||
}
|
|
||||||
return $methods[$method] ?? [];
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $class
|
* @param $className
|
||||||
* @param string|null $property
|
* @param $method
|
||||||
* @return ReflectionProperty|ReflectionProperty[]|null
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function getClassReflectionProperty(string $class, string $property = null): ReflectionProperty|null|array
|
public function getMethodAttribute($className, $method = null): array
|
||||||
{
|
{
|
||||||
if (!isset(static::$_reflectionProperty[$class])) {
|
$methods = static::$_methodsAttributes[$className] ?? [];
|
||||||
return null;
|
if (!empty($method)) {
|
||||||
}
|
return $methods[$method] ?? [];
|
||||||
$properties = static::$_reflectionProperty[$class];
|
}
|
||||||
if (!empty($property)) {
|
return $methods;
|
||||||
return $properties[$property] ?? null;
|
}
|
||||||
}
|
|
||||||
return $properties;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $object
|
* @param string $class
|
||||||
* @param $config
|
* @param string|null $property
|
||||||
* @return mixed
|
* @return ReflectionProperty|ReflectionProperty[]|null
|
||||||
*/
|
*/
|
||||||
private function onAfterInit($object, $config): mixed
|
public function getClassReflectionProperty(string $class, string $property = null): ReflectionProperty|null|array
|
||||||
{
|
{
|
||||||
Snowflake::configure($object, $config);
|
if (!isset(static::$_reflectionProperty[$class])) {
|
||||||
if (method_exists($object, 'afterInit')) {
|
return null;
|
||||||
call_user_func([$object, 'afterInit']);
|
}
|
||||||
}
|
$properties = static::$_reflectionProperty[$class];
|
||||||
return $object;
|
if (!empty($property)) {
|
||||||
}
|
return $properties[$property] ?? null;
|
||||||
|
}
|
||||||
/**
|
return $properties;
|
||||||
* @param $class
|
}
|
||||||
* @return array|null
|
|
||||||
* @throws ReflectionException|NotFindClassException
|
|
||||||
*/
|
|
||||||
private function resolveDependencies($class): ?array
|
|
||||||
{
|
|
||||||
if (!isset(static::$_reflection[$class])) {
|
|
||||||
static::$_reflection[$class] = new ReflectionClass($class);
|
|
||||||
if (!static::$_reflection[$class]->isInstantiable()) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
$this->resolveTargetAttribute(static::$_reflection[$class]);
|
|
||||||
$this->resolveMethodAttribute(static::$_reflection[$class]);
|
|
||||||
$this->scanProperty(static::$_reflection[$class]);
|
|
||||||
}
|
|
||||||
if (!is_null($constructs = static::$_reflection[$class]->getConstructor())) {
|
|
||||||
$constructs = $this->resolveMethodParam($constructs);
|
|
||||||
}
|
|
||||||
return [static::$_reflection[$class], $constructs];
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param ReflectionClass $reflectionClass
|
* @param $object
|
||||||
* @return void
|
* @param $config
|
||||||
*/
|
* @return mixed
|
||||||
private function scanProperty(ReflectionClass $reflectionClass): void
|
*/
|
||||||
{
|
private function onAfterInit($object, $config): mixed
|
||||||
$properties = $reflectionClass->getProperties(ReflectionProperty::IS_PUBLIC |
|
{
|
||||||
ReflectionProperty::IS_PRIVATE | ReflectionProperty::IS_PROTECTED
|
Snowflake::configure($object, $config);
|
||||||
);
|
if (method_exists($object, 'afterInit')) {
|
||||||
|
call_user_func([$object, 'afterInit']);
|
||||||
|
}
|
||||||
|
return $object;
|
||||||
|
}
|
||||||
|
|
||||||
$className = $reflectionClass->getName();
|
/**
|
||||||
foreach ($properties as $property) {
|
* @param $class
|
||||||
$targets = $property->getAttributes(Inject::class);
|
* @param array $dependencies
|
||||||
if (count($targets) < 1) {
|
* @return array|null
|
||||||
continue;
|
* @throws NotFindClassException
|
||||||
}
|
* @throws ReflectionException
|
||||||
|
*/
|
||||||
static::$_reflectionProperty[$className][$property->getName()] = $property;
|
private function resolveDependencies($class, array $dependencies = []): ?array
|
||||||
|
{
|
||||||
static::$_propertyAttributes[$className][$property->getName()] = $targets[0]->newInstance();
|
if (!isset(static::$_reflection[$class])) {
|
||||||
}
|
if (!($reflect = new ReflectionClass($class))->isInstantiable()) {
|
||||||
}
|
throw new ReflectionException('Class ' . $class . ' cannot be instantiated');
|
||||||
|
}
|
||||||
|
static::$_reflection[$class] = $reflect;
|
||||||
|
$this->resolveTargetAttribute(static::$_reflection[$class]);
|
||||||
|
$this->resolveMethodAttribute(static::$_reflection[$class]);
|
||||||
|
$this->scanProperty(static::$_reflection[$class]);
|
||||||
|
}
|
||||||
|
if (!is_null($construct = static::$_reflection[$class]->getConstructor())) {
|
||||||
|
foreach ($this->resolveMethodParam($construct) as $key => $item) {
|
||||||
|
$dependencies[$key] = $item;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return [static::$_reflection[$class], $dependencies];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param ReflectionMethod|null $method
|
* @param ReflectionClass $reflectionClass
|
||||||
* @return array
|
* @return void
|
||||||
* @throws NotFindClassException
|
*/
|
||||||
* @throws ReflectionException
|
private function scanProperty(ReflectionClass $reflectionClass): void
|
||||||
*/
|
{
|
||||||
private function resolveMethodParam(?ReflectionMethod $method): array
|
$properties = $reflectionClass->getProperties(ReflectionProperty::IS_PUBLIC |
|
||||||
{
|
ReflectionProperty::IS_PRIVATE | ReflectionProperty::IS_PROTECTED
|
||||||
$array = [];
|
);
|
||||||
foreach ($method->getParameters() as $key => $parameter) {
|
|
||||||
if ($parameter->isDefaultValueAvailable()) {
|
$className = $reflectionClass->getName();
|
||||||
$array[] = $parameter->getDefaultValue();
|
foreach ($properties as $property) {
|
||||||
} else {
|
$targets = $property->getAttributes(Inject::class);
|
||||||
$type = $parameter->getType();
|
if (count($targets) < 1) {
|
||||||
if (is_string($type) && class_exists($type)) {
|
continue;
|
||||||
$type = Snowflake::createObject($type);
|
}
|
||||||
}
|
|
||||||
$array[] = match ($parameter->getType()) {
|
static::$_reflectionProperty[$className][$property->getName()] = $property;
|
||||||
'string' => '',
|
|
||||||
'int', 'float' => 0,
|
static::$_propertyAttributes[$className][$property->getName()] = $targets[0]->newInstance();
|
||||||
'', null, 'object', 'mixed' => NULL,
|
}
|
||||||
'bool' => false,
|
}
|
||||||
default => $type
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return $array;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $class
|
* @param ReflectionMethod|null $method
|
||||||
* @return ReflectionClass|null
|
* @return array
|
||||||
* @throws NotFindClassException
|
* @throws NotFindClassException
|
||||||
* @throws ReflectionException
|
* @throws ReflectionException
|
||||||
*/
|
*/
|
||||||
public function getReflect($class): ?ReflectionClass
|
private function resolveMethodParam(?ReflectionMethod $method): array
|
||||||
{
|
{
|
||||||
$reflect = static::$_reflection[$class] ?? null;
|
$array = [];
|
||||||
if (!is_null($reflect)) {
|
foreach ($method->getParameters() as $parameter) {
|
||||||
return $reflect;
|
if ($parameter->isDefaultValueAvailable()) {
|
||||||
}
|
$array[] = $parameter->getDefaultValue();
|
||||||
$reflect = $this->resolveDependencies($class);
|
} else {
|
||||||
if (is_array($reflect)) {
|
$type = $parameter->getType();
|
||||||
return $reflect[0];
|
if (is_string($type) && class_exists($type)) {
|
||||||
}
|
$type = Snowflake::createObject($type);
|
||||||
return null;
|
}
|
||||||
}
|
$array[] = match ($parameter->getType()) {
|
||||||
|
'string' => '',
|
||||||
|
'int', 'float' => 0,
|
||||||
|
'', null, 'object', 'mixed' => NULL,
|
||||||
|
'bool' => false,
|
||||||
|
default => $type
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $array;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $class
|
|
||||||
*/
|
|
||||||
public function unset($class)
|
|
||||||
{
|
|
||||||
if (is_array($class) && isset($class['class'])) {
|
|
||||||
$class = $class['class'];
|
|
||||||
} else if (is_object($class)) {
|
|
||||||
$class = $class::class;
|
|
||||||
}
|
|
||||||
unset(
|
|
||||||
static::$_reflection[$class], static::$_singletons[$class],
|
|
||||||
static::$_param[$class], static::$_constructs[$class]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return $this
|
* @param $class
|
||||||
*/
|
* @return ReflectionClass|null
|
||||||
public function flush(): static
|
* @throws NotFindClassException
|
||||||
{
|
* @throws ReflectionException
|
||||||
static::$_reflection = [];
|
*/
|
||||||
static::$_singletons = [];
|
public function getReflect($class): ?ReflectionClass
|
||||||
static::$_param = [];
|
{
|
||||||
static::$_constructs = [];
|
$reflect = static::$_reflection[$class] ?? null;
|
||||||
return $this;
|
if (!is_null($reflect)) {
|
||||||
}
|
return $reflect;
|
||||||
|
}
|
||||||
|
return $this->resolveDependencies($class)[0];
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $class
|
* @param $class
|
||||||
* @param $newParam
|
*/
|
||||||
*
|
public function unset($class)
|
||||||
* @return mixed
|
{
|
||||||
*/
|
if (is_array($class) && isset($class['class'])) {
|
||||||
private function mergeParam($class, $newParam): array
|
$class = $class['class'];
|
||||||
{
|
} else if (is_object($class)) {
|
||||||
if (empty(static::$_param[$class])) {
|
$class = $class::class;
|
||||||
return $newParam;
|
}
|
||||||
} else if (empty($newParam)) {
|
unset(
|
||||||
return static::$_param[$class];
|
static::$_reflection[$class], static::$_singletons[$class],
|
||||||
}
|
static::$_param[$class], static::$_constructs[$class]
|
||||||
$old = static::$_param[$class];
|
);
|
||||||
foreach ($newParam as $key => $val) {
|
}
|
||||||
$old[$key] = $val;
|
|
||||||
}
|
/**
|
||||||
return $old;
|
* @return $this
|
||||||
}
|
*/
|
||||||
|
public function flush(): static
|
||||||
|
{
|
||||||
|
static::$_reflection = [];
|
||||||
|
static::$_singletons = [];
|
||||||
|
static::$_param = [];
|
||||||
|
static::$_constructs = [];
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $class
|
||||||
|
* @param $newParam
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
private function mergeParam($class, $newParam): array
|
||||||
|
{
|
||||||
|
if (empty(static::$_param[$class])) {
|
||||||
|
return $newParam;
|
||||||
|
} else if (empty($newParam)) {
|
||||||
|
return static::$_param[$class];
|
||||||
|
}
|
||||||
|
$old = static::$_param[$class];
|
||||||
|
foreach ($newParam as $key => $val) {
|
||||||
|
$old[$key] = $val;
|
||||||
|
}
|
||||||
|
return $old;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user