This commit is contained in:
2021-02-23 15:02:35 +08:00
parent d63cdfa39a
commit 37b6170dba
3 changed files with 223 additions and 221 deletions
-1
View File
@@ -164,7 +164,6 @@ class Annotation extends Component
$object->{$value->getName()} = $annotation; $object->{$value->getName()} = $annotation;
} else { } else {
$name = 'set' . ucfirst($value->getName()); $name = 'set' . ucfirst($value->getName());
var_dump($name);
if (!method_exists($object, $name)) { if (!method_exists($object, $name)) {
throw new NotFindPropertyException('set property need method ' . $name); throw new NotFindPropertyException('set property need method ' . $name);
} }
+1 -1
View File
@@ -104,7 +104,7 @@ class Application extends BaseApplication
public function start(Input $argv): bool|string public function start(Input $argv): bool|string
{ {
try { try {
// $this->scan_system_annotation(); $this->scan_system_annotation();
fire(Event::SERVER_BEFORE_START); fire(Event::SERVER_BEFORE_START);
+222 -219
View File
@@ -24,142 +24,142 @@ use Snowflake\Snowflake;
class Container extends BaseObject class Container extends BaseObject
{ {
/** /**
* @var array * @var array
* *
* instance class by className * instance class by className
*/ */
private array $_singletons = []; private array $_singletons = [];
/** /**
* @var array * @var array
* *
* class new instance construct parameter * class new instance construct parameter
*/ */
private array $_constructs = []; private array $_constructs = [];
/** /**
* @var array * @var array
* *
* implements \ReflectClass * implements \ReflectClass
*/ */
private array $_reflection = []; private array $_reflection = [];
/** /**
* @var array * @var array
* *
* The construct parameter * The construct parameter
*/ */
private array $_param = []; private 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
*/ */
public function get($class, $constrict = [], $config = []): mixed public function get($class, $constrict = [], $config = []): mixed
{ {
if (isset($this->_singletons[$class])) { if (isset($this->_singletons[$class])) {
return $this->_singletons[$class]; return $this->_singletons[$class];
} else if (!isset($this->_constructs[$class])) { } else if (!isset($this->_constructs[$class])) {
return $this->resolve($class, $constrict, $config); return $this->resolve($class, $constrict, $config);
} }
$definition = $this->_constructs[$class]; $definition = $this->_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); $object = $this->resolveDefinition($definition, $class, $config, $constrict);
} else if (is_object($definition)) { } else if (is_object($definition)) {
return $this->_singletons[$class] = $definition; return $this->_singletons[$class] = $definition;
} else { } else {
throw new NotFindClassException($class); throw new NotFindClassException($class);
} }
return $this->_singletons[$class] = $object; return $this->_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
*/ */
private function resolveDefinition($definition, $class, $config, $constrict) private function resolveDefinition($definition, $class, $config, $constrict)
{ {
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); $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 NotFindClassException * @throws NotFindClassException
* @throws ReflectionException * @throws ReflectionException
*/ */
private function resolve($class, $constrict, $config): object private function resolve($class, $constrict, $config): object
{ {
[$reflect, $dependencies] = $this->resolveDependencies($class, $constrict); [$reflect, $dependencies] = $this->resolveDependencies($class, $constrict);
foreach ($constrict as $index => $param) { foreach ($constrict as $index => $param) {
$dependencies[$index] = $param; $dependencies[$index] = $param;
} }
if (!$reflect->isInstantiable()) { if (!$reflect->isInstantiable()) {
throw new NotFindClassException($reflect->getName()); throw new NotFindClassException($reflect->getName());
} }
if (empty($config) || !is_array($config)) { if (empty($config) || !is_array($config)) {
return $reflect->newInstanceArgs($dependencies); return $reflect->newInstanceArgs($dependencies);
} }
if (!empty($dependencies) && $reflect->implementsInterface('Snowflake\Abstracts\Configure')) { if (!empty($dependencies) && $reflect->implementsInterface('Snowflake\Abstracts\Configure')) {
$dependencies[count($dependencies) - 1] = $config; $dependencies[count($dependencies) - 1] = $config;
return $reflect->newInstanceArgs($dependencies); return $reflect->newInstanceArgs($dependencies);
} }
if (!empty($config)) $this->_param[$class] = $config; if (!empty($config)) $this->_param[$class] = $config;
return $this->onAfterInit($reflect->newInstanceArgs($dependencies), $config); return $this->onAfterInit($reflect->newInstanceArgs($dependencies), $config);
} }
/** /**
* @param $object * @param $object
* @param $config * @param $config
* @return mixed * @return mixed
*/ */
private function onAfterInit($object, $config) private function onAfterInit($object, $config)
{ {
Snowflake::configure($object, $config); Snowflake::configure($object, $config);
if (method_exists($object, 'afterInit')) { if (method_exists($object, 'afterInit')) {
call_user_func([$object, 'afterInit']); call_user_func([$object, 'afterInit']);
} }
return $object; return $object;
} }
/** /**
* @param $class * @param $class
@@ -168,52 +168,55 @@ class Container extends BaseObject
* @throws NotFindClassException * @throws NotFindClassException
* @throws ReflectionException * @throws ReflectionException
*/ */
private function resolveDependencies($class, $constrict = []): ?array private function resolveDependencies($class, $constrict = []): ?array
{ {
if (!isset($this->_reflection[$class])) { if (!isset($this->_reflection[$class])) {
$reflection = new ReflectionClass($class); if (!class_exists($class)) {
if (!$reflection->isInstantiable()) { return null;
return null; }
} $reflection = new ReflectionClass($class);
$this->_reflection[$class] = $reflection; if (!$reflection->isInstantiable()) {
} else { return null;
$reflection = $this->_reflection[$class]; }
} $this->_reflection[$class] = $reflection;
if (!is_null($construct = $reflection->getConstructor())) { } else {
$constrict = $this->resolveMethodParam($construct); $reflection = $this->_reflection[$class];
} }
return [$reflection, $constrict]; if (!is_null($construct = $reflection->getConstructor())) {
} $constrict = $this->resolveMethodParam($construct);
}
return [$reflection, $constrict];
}
/** /**
* @param \ReflectionMethod|null $method * @param \ReflectionMethod|null $method
* @return array * @return array
* @throws NotFindClassException * @throws NotFindClassException
* @throws ReflectionException * @throws ReflectionException
*/ */
private function resolveMethodParam(?\ReflectionMethod $method): array private function resolveMethodParam(?\ReflectionMethod $method): array
{ {
$array = []; $array = [];
foreach ($method->getParameters() as $key => $parameter) { foreach ($method->getParameters() as $key => $parameter) {
if ($parameter->isDefaultValueAvailable()) { if ($parameter->isDefaultValueAvailable()) {
$array[] = $parameter->getDefaultValue(); $array[] = $parameter->getDefaultValue();
} else { } else {
$type = $parameter->getType(); $type = $parameter->getType();
if (is_string($type) && class_exists($type)) { if (is_string($type) && class_exists($type)) {
$type = Snowflake::createObject($type); $type = Snowflake::createObject($type);
} }
$array[] = match ($parameter->getType()) { $array[] = match ($parameter->getType()) {
'string' => '', 'string' => '',
'int', 'float' => 0, 'int', 'float' => 0,
'', null, 'object', 'mixed' => NULL, '', null, 'object', 'mixed' => NULL,
'bool' => false, 'bool' => false,
default => $type default => $type
}; };
} }
} }
return $array; return $array;
} }
/** /**
@@ -222,64 +225,64 @@ class Container extends BaseObject
* @throws NotFindClassException * @throws NotFindClassException
* @throws ReflectionException * @throws ReflectionException
*/ */
public function getReflect($class): ?ReflectionClass public function getReflect($class): ?ReflectionClass
{ {
$reflect = $this->_reflection[$class] ?? null; $reflect = $this->_reflection[$class] ?? null;
if (!is_null($reflect)) { if (!is_null($reflect)) {
return $reflect; return $reflect;
} }
$reflect = $this->resolveDependencies($class); $reflect = $this->resolveDependencies($class);
if (is_array($reflect)) { if (is_array($reflect)) {
return $reflect[0]; return $reflect[0];
} }
return null; return null;
} }
/** /**
* @param $class * @param $class
*/ */
public function unset($class) public function unset($class)
{ {
if (is_array($class) && isset($class['class'])) { if (is_array($class) && isset($class['class'])) {
$class = $class['class']; $class = $class['class'];
} else if (is_object($class)) { } else if (is_object($class)) {
$class = get_class($class); $class = get_class($class);
} }
unset( unset(
$this->_reflection[$class], $this->_singletons[$class], $this->_reflection[$class], $this->_singletons[$class],
$this->_param[$class], $this->_constructs[$class] $this->_param[$class], $this->_constructs[$class]
); );
} }
/** /**
* @return $this * @return $this
*/ */
public function flush(): static public function flush(): static
{ {
$this->_reflection = []; $this->_reflection = [];
$this->_singletons = []; $this->_singletons = [];
$this->_param = []; $this->_param = [];
$this->_constructs = []; $this->_constructs = [];
return $this; return $this;
} }
/** /**
* @param $class * @param $class
* @param $newParam * @param $newParam
* *
* @return mixed * @return mixed
*/ */
private function mergeParam($class, $newParam): array private function mergeParam($class, $newParam): array
{ {
if (empty($this->_param[$class])) { if (empty($this->_param[$class])) {
return $newParam; return $newParam;
} else if (empty($newParam)) { } else if (empty($newParam)) {
return $this->_param[$class]; return $this->_param[$class];
} }
$old = $this->_param[$class]; $old = $this->_param[$class];
foreach ($newParam as $key => $val) { foreach ($newParam as $key => $val) {
$old[$key] = $val; $old[$key] = $val;
} }
return $old; return $old;
} }
} }