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 */
private string $_modelName;
public string $dbname = '';
private ?PDOStatement $prepare = null;
@@ -130,9 +132,7 @@ class Command extends Component
if (microtime(true) - $time >= 0.02) {
$this->warning('Mysql:' . Json::encode([$this->sql, $this->params]) . (microtime(true) - $time));
}
if ($this->prepare) {
$this->prepare->closeCursor();
}
$this->prepare?->closeCursor();
return $result;
} catch (\Throwable $exception) {
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 $maxNumber = 30;
public int $minNumber = 10;
/**
* @var bool
* enable database cache
@@ -85,9 +82,8 @@ class Connection extends Component
return;
}
$this->beginTransaction();
Event::on(Connection::TRANSACTION_COMMIT, [$this, 'commit'], false, true);
Event::on(Connection::TRANSACTION_ROLLBACK, [$this, 'rollback'], false, true);
Event::on(Connection::TRANSACTION_ROLLBACK, [$this, 'rollback'], [], true);
Event::on(Connection::TRANSACTION_COMMIT, [$this, 'commit'], [], true);
}
/**
+4 -4
View File
@@ -26,21 +26,21 @@ class Controller
*
* @var Request|null
*/
#[Inject(value: Request::class)]
#[Inject('request')]
public ?Request $request = null;
/**
* @var HttpParams|null
*/
#[Inject(value: HttpParams::class)]
#[Inject('input')]
public ?HttpParams $input = null;
/**
* @var HttpHeaders|null
*/
#[Inject(value: HttpHeaders::class)]
#[Inject('header')]
public ?HttpHeaders $header = null;
@@ -49,7 +49,7 @@ class Controller
*
* @var Response|null
*/
#[Inject(value: Response::class)]
#[Inject('response')]
public ?Response $response = null;
+1 -2
View File
@@ -127,7 +127,7 @@ class Node extends HttpService
* @throws NotFindClassException
* @throws Exception
*/
#[Pure] private function createDispatch(): Closure
private function createDispatch(): Closure
{
/** @var Aop $aop */
$aop = Snowflake::app()->get('aop');
@@ -157,7 +157,6 @@ class Node extends HttpService
/**
* @param $application
* @param $handler
* @return Closure
*/
+65 -37
View File
@@ -9,7 +9,6 @@ declare(strict_types=1);
namespace Snowflake\Di;
use Annotation\Attribute;
use Annotation\Inject;
use Annotation\Target;
use Exception;
@@ -85,7 +84,7 @@ class Container extends BaseObject
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])) {
return $this->resolve($class, $constrict, $config);
}
$definition = static::$_constructs[$class];
if (is_callable($definition, TRUE)) {
return call_user_func($definition, $this, $constrict, $config);
} 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)) {
return static::$_singletons[$class] = $definition;
} else {
throw new NotFindClassException($class);
}
return static::$_singletons[$class] = $object;
}
@@ -145,7 +142,6 @@ class Container extends BaseObject
$_className = $definition['class'];
unset($definition['class']);
// $config = array_merge($definition, $config);
$definition = $this->mergeParam($class, $constrict);
if ($_className === $class) {
@@ -168,29 +164,52 @@ class Container extends BaseObject
private function resolve($class, $constrict, $config): object
{
/** @var ReflectionClass $reflect */
[$reflect, $dependencies] = $this->resolveDependencies($class);
[$reflect, $dependencies] = $this->resolveDependencies($class, $constrict);
if (empty($reflect) || !$reflect->isInstantiable()) {
throw new NotFindClassException($class);
}
foreach ($constrict as $key => $item) {
$dependencies[$key] = $item;
$object = $this->setConfig($config, $reflect, $dependencies, $class);
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)) {
$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;
static::$_param[$class] = $config;
$object = $this->onAfterInit($this->newInstance($reflect, $dependencies), $config);
}
static::$_reflectionClass[$reflect->getName()] = [];
foreach ($reflect->getAttributes() as $attribute) {
static::$_reflectionClass[$reflect->getName()][] = $attribute->newInstance();
}
return $this->propertyInject($reflect, $object);
return $object;
}
@@ -244,7 +263,6 @@ class Container extends BaseObject
if (!class_exists($attribute->getName())) {
continue;
}
$this->setMethodsAttributes($class, $method, $attribute->newInstance());
}
}
@@ -257,10 +275,10 @@ class Container extends BaseObject
*/
private function setReflectionMethod(ReflectionClass $class, ReflectionMethod $method)
{
if (!isset(static::$_attributeMaping[$class->getName()])) {
static::$_attributeMaping[$class->getName()] = [];
if (!isset(static::$_attributeMapping[$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 $method
@@ -314,11 +342,11 @@ class Container extends BaseObject
public function getMethodAttribute($className, $method = null): array
{
$methods = static::$_methodsAttributes[$className] ?? [];
if ($method === null || empty($method)) {
return $methods;
}
if (!empty($method)) {
return $methods[$method] ?? [];
}
return $methods;
}
/**
@@ -355,24 +383,28 @@ class Container extends BaseObject
/**
* @param $class
* @param array $dependencies
* @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])) {
static::$_reflection[$class] = new ReflectionClass($class);
if (!static::$_reflection[$class]->isInstantiable()) {
return null;
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($constructs = static::$_reflection[$class]->getConstructor())) {
$constructs = $this->resolveMethodParam($constructs);
if (!is_null($construct = static::$_reflection[$class]->getConstructor())) {
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
{
$array = [];
foreach ($method->getParameters() as $key => $parameter) {
foreach ($method->getParameters() as $parameter) {
if ($parameter->isDefaultValueAvailable()) {
$array[] = $parameter->getDefaultValue();
} else {
@@ -442,11 +474,7 @@ class Container extends BaseObject
if (!is_null($reflect)) {
return $reflect;
}
$reflect = $this->resolveDependencies($class);
if (is_array($reflect)) {
return $reflect[0];
}
return null;
return $this->resolveDependencies($class)[0];
}
/**