This commit is contained in:
2021-08-01 19:04:34 +08:00
parent 822f7a7f36
commit b71b45c5c8
5 changed files with 442 additions and 419 deletions
+3 -3
View File
@@ -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');
+2 -6
View File
@@ -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);
} }
/** /**
+4 -4
View File
@@ -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;
+1 -2
View File
@@ -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
*/ */
+65 -37
View File
@@ -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;
@@ -85,7 +84,7 @@ class Container extends BaseObject
private static array $_targetAttributes = []; private static array $_targetAttributes = [];
private static array $_attributeMaping = []; private static array $_attributeMapping = [];
/** /**
@@ -112,18 +111,16 @@ class Container extends BaseObject
} 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)) {
$object = $this->resolveDefinition($definition, $class, $config, $constrict); return static::$_singletons[$class] = $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;
} }
@@ -145,7 +142,6 @@ class Container extends BaseObject
$_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) {
@@ -168,29 +164,52 @@ class Container extends BaseObject
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;
return $this->setClassAnnotation($reflect, $object);
} }
unset($dependencies['class']);
/**
* @param $reflect
* @param $object
* @return mixed
* @throws Exception
*/
private function setClassAnnotation($reflect, $object): mixed
{
static::$_reflectionClass[$reflect->getName()] = [];
foreach ($reflect->getAttributes() as $attribute) {
static::$_reflectionClass[$reflect->getName()][] = $attribute->newInstance();
}
return $this->propertyInject($reflect, $object);
}
/**
* @param $config
* @param $reflect
* @param $dependencies
* @param $class
* @return mixed
*/
private function setConfig($config, $reflect, $dependencies, $class): mixed
{
if (empty($config) || !is_array($config)) { if (empty($config) || !is_array($config)) {
$object = $this->newInstance($reflect, $dependencies); $object = $this->newInstance($reflect, $dependencies);
} else if (!empty($dependencies) && $reflect->implementsInterface(Configure::class)) { } else if (!empty($dependencies) && $reflect->implementsInterface(Configure::class)) {
$dependencies[count($dependencies) - 1] = $config; $dependencies[count($dependencies) - 1] = $config;
$object = $this->newInstance($reflect, $dependencies); $object = $this->newInstance($reflect, $dependencies);
} else { } else {
if (!empty($config)) static::$_param[$class] = $config; static::$_param[$class] = $config;
$object = $this->onAfterInit($this->newInstance($reflect, $dependencies), $config); $object = $this->onAfterInit($this->newInstance($reflect, $dependencies), $config);
} }
static::$_reflectionClass[$reflect->getName()] = []; return $object;
foreach ($reflect->getAttributes() as $attribute) {
static::$_reflectionClass[$reflect->getName()][] = $attribute->newInstance();
}
return $this->propertyInject($reflect, $object);
} }
@@ -244,7 +263,6 @@ class Container extends BaseObject
if (!class_exists($attribute->getName())) { if (!class_exists($attribute->getName())) {
continue; continue;
} }
$this->setMethodsAttributes($class, $method, $attribute->newInstance()); $this->setMethodsAttributes($class, $method, $attribute->newInstance());
} }
} }
@@ -257,10 +275,10 @@ class Container extends BaseObject
*/ */
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;
} }
@@ -306,6 +324,16 @@ class Container extends BaseObject
} }
/**
* @param $className
* @return ReflectionMethod|null
*/
public function getTargetAnnotation($className): ?ReflectionMethod
{
return static::$_targetAttributes[$className] ?? null;
}
/** /**
* @param $className * @param $className
* @param $method * @param $method
@@ -314,11 +342,11 @@ class Container extends BaseObject
public function getMethodAttribute($className, $method = null): array public function getMethodAttribute($className, $method = null): array
{ {
$methods = static::$_methodsAttributes[$className] ?? []; $methods = static::$_methodsAttributes[$className] ?? [];
if ($method === null || empty($method)) { if (!empty($method)) {
return $methods;
}
return $methods[$method] ?? []; return $methods[$method] ?? [];
} }
return $methods;
}
/** /**
@@ -355,24 +383,28 @@ class Container extends BaseObject
/** /**
* @param $class * @param $class
* @param array $dependencies
* @return array|null * @return array|null
* @throws ReflectionException|NotFindClassException * @throws NotFindClassException
* @throws ReflectionException
*/ */
private function resolveDependencies($class): ?array private function resolveDependencies($class, array $dependencies = []): ?array
{ {
if (!isset(static::$_reflection[$class])) { if (!isset(static::$_reflection[$class])) {
static::$_reflection[$class] = new ReflectionClass($class); if (!($reflect = new ReflectionClass($class))->isInstantiable()) {
if (!static::$_reflection[$class]->isInstantiable()) { throw new ReflectionException('Class ' . $class . ' cannot be instantiated');
return null;
} }
static::$_reflection[$class] = $reflect;
$this->resolveTargetAttribute(static::$_reflection[$class]); $this->resolveTargetAttribute(static::$_reflection[$class]);
$this->resolveMethodAttribute(static::$_reflection[$class]); $this->resolveMethodAttribute(static::$_reflection[$class]);
$this->scanProperty(static::$_reflection[$class]); $this->scanProperty(static::$_reflection[$class]);
} }
if (!is_null($constructs = static::$_reflection[$class]->getConstructor())) { if (!is_null($construct = static::$_reflection[$class]->getConstructor())) {
$constructs = $this->resolveMethodParam($constructs); foreach ($this->resolveMethodParam($construct) as $key => $item) {
$dependencies[$key] = $item;
} }
return [static::$_reflection[$class], $constructs]; }
return [static::$_reflection[$class], $dependencies];
} }
@@ -409,7 +441,7 @@ class Container extends BaseObject
private function resolveMethodParam(?ReflectionMethod $method): array private function resolveMethodParam(?ReflectionMethod $method): array
{ {
$array = []; $array = [];
foreach ($method->getParameters() as $key => $parameter) { foreach ($method->getParameters() as $parameter) {
if ($parameter->isDefaultValueAvailable()) { if ($parameter->isDefaultValueAvailable()) {
$array[] = $parameter->getDefaultValue(); $array[] = $parameter->getDefaultValue();
} else { } else {
@@ -442,11 +474,7 @@ class Container extends BaseObject
if (!is_null($reflect)) { if (!is_null($reflect)) {
return $reflect; return $reflect;
} }
$reflect = $this->resolveDependencies($class); return $this->resolveDependencies($class)[0];
if (is_array($reflect)) {
return $reflect[0];
}
return null;
} }
/** /**