Files
kiri-core/System/Di/Container.php
T

376 lines
8.3 KiB
PHP
Raw Normal View History

2020-08-31 01:27:08 +08:00
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/24 0024
* Time: 17:27
*/
2020-10-29 18:17:25 +08:00
declare(strict_types=1);
2020-08-31 01:27:08 +08:00
namespace Snowflake\Di;
2021-03-24 18:08:50 +08:00
use Annotation\Inject;
2021-04-25 11:52:31 +08:00
use Exception;
2020-08-31 01:27:08 +08:00
use ReflectionClass;
use ReflectionException;
2021-04-25 11:52:31 +08:00
use ReflectionMethod;
use ReflectionProperty;
use Snowflake\Abstracts\BaseObject;
2020-08-31 01:27:08 +08:00
use Snowflake\Exception\NotFindClassException;
2021-02-23 03:26:18 +08:00
use Snowflake\Snowflake;
2020-08-31 01:27:08 +08:00
/**
* Class Container
2020-08-31 12:38:32 +08:00
* @package Snowflake\Di
2020-08-31 01:27:08 +08:00
*/
class Container extends BaseObject
{
2021-02-23 15:02:35 +08:00
/**
* @var array
*
* instance class by className
*/
private array $_singletons = [];
2020-08-31 01:27:08 +08:00
2021-02-23 15:02:35 +08:00
/**
* @var array
*
* class new instance construct parameter
*/
private array $_constructs = [];
2020-08-31 01:27:08 +08:00
2021-02-23 15:02:35 +08:00
/**
* @var array
*
* implements \ReflectClass
*/
private array $_reflection = [];
2020-08-31 01:27:08 +08:00
2021-04-25 11:52:31 +08:00
private array $_property = [];
2021-02-23 15:02:35 +08:00
/**
* @var array
*
* The construct parameter
*/
private array $_param = [];
2020-08-31 01:27:08 +08:00
2021-02-23 15:02:35 +08:00
/**
* @param $class
* @param array $constrict
* @param array $config
*
* @return mixed
* @throws NotFindClassException
* @throws ReflectionException
2021-04-25 11:52:31 +08:00
* @throws Exception
2021-02-23 15:02:35 +08:00
*/
public function get($class, $constrict = [], $config = []): mixed
{
if (isset($this->_singletons[$class])) {
return $this->_singletons[$class];
} else if (!isset($this->_constructs[$class])) {
return $this->resolve($class, $constrict, $config);
}
2021-02-23 03:26:18 +08:00
2021-02-23 15:02:35 +08:00
$definition = $this->_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);
} else if (is_object($definition)) {
return $this->_singletons[$class] = $definition;
} else {
throw new NotFindClassException($class);
}
return $this->_singletons[$class] = $object;
}
2020-08-31 01:27:08 +08:00
2021-02-23 03:26:18 +08:00
2021-02-23 15:02:35 +08:00
/**
* @param $definition
* @param $class
* @param $config
* @param $constrict
* @return mixed
* @throws NotFindClassException
* @throws ReflectionException
2021-04-25 11:52:31 +08:00
* @throws Exception
2021-02-23 15:02:35 +08:00
*/
2021-04-25 11:52:31 +08:00
private function resolveDefinition($definition, $class, $config, $constrict): mixed
2021-02-23 15:02:35 +08:00
{
if (!isset($definition['class'])) {
throw new NotFindClassException($class);
}
$_className = $definition['class'];
unset($definition['class']);
2021-02-23 03:17:32 +08:00
2021-04-25 12:41:41 +08:00
// $config = array_merge($definition, $config);
2021-02-23 15:02:35 +08:00
$definition = $this->mergeParam($class, $constrict);
2021-02-23 03:17:32 +08:00
2021-02-23 15:02:35 +08:00
if ($_className === $class) {
$object = $this->resolve($class, $definition, $config);
} else {
$object = $this->get($class, $definition, $config);
}
return $object;
}
2020-08-31 01:27:08 +08:00
2021-02-23 03:26:18 +08:00
2021-02-23 15:02:35 +08:00
/**
* @param $class
* @param $constrict
* @param $config
*
* @return object
2021-04-25 11:52:31 +08:00
* @throws Exception
2021-02-23 15:02:35 +08:00
*/
private function resolve($class, $constrict, $config): object
{
2021-03-24 18:08:50 +08:00
/** @var ReflectionClass $reflect */
2021-04-25 11:58:29 +08:00
[$reflect, $dependencies] = $this->resolveDependencies($class);
2021-03-23 10:36:44 +08:00
if (empty($reflect)) {
throw new NotFindClassException($class);
}
2021-02-23 15:02:35 +08:00
foreach ($constrict as $index => $param) {
$dependencies[$index] = $param;
}
2021-02-23 03:26:18 +08:00
2021-02-23 15:02:35 +08:00
if (!$reflect->isInstantiable()) {
throw new NotFindClassException($reflect->getName());
}
2021-02-23 03:26:18 +08:00
2021-04-25 12:05:34 +08:00
unset($dependencies['class']);
2021-02-23 15:02:35 +08:00
if (empty($config) || !is_array($config)) {
2021-04-25 12:05:34 +08:00
$object = $this->newInstance($reflect, $dependencies);
2021-03-24 18:08:50 +08:00
} else if (!empty($dependencies) && $reflect->implementsInterface('Snowflake\Abstracts\Configure')) {
2021-02-23 15:02:35 +08:00
$dependencies[count($dependencies) - 1] = $config;
2021-04-25 12:05:34 +08:00
$object = $this->newInstance($reflect, $dependencies);
2021-03-24 18:08:50 +08:00
} else {
if (!empty($config)) $this->_param[$class] = $config;
2021-02-23 03:26:18 +08:00
2021-04-25 12:05:34 +08:00
$object = $this->onAfterInit($this->newInstance($reflect, $dependencies), $config);
2021-03-24 18:08:50 +08:00
}
return $this->propertyInject($reflect, $object);
}
2021-02-23 03:26:18 +08:00
2021-04-25 12:05:34 +08:00
/**
* @param $reflect
* @param $dependencies
* @return mixed
*/
2021-04-25 12:25:49 +08:00
private function newInstance($reflect, $dependencies): mixed
2021-04-25 12:05:34 +08:00
{
if (!empty($dependencies)) {
return $reflect->newInstanceArgs($dependencies);
}
return $reflect->newInstance();
}
2021-03-24 18:08:50 +08:00
/**
* @param ReflectionClass $reflect
* @param $object
* @return mixed
2021-04-25 11:52:31 +08:00
* @throws Exception
2021-03-24 18:08:50 +08:00
*/
private function propertyInject(ReflectionClass $reflect, $object): mixed
{
2021-04-25 11:52:31 +08:00
if (!isset($this->_property[$reflect->getName()])) {
return $object;
}
foreach ($this->_property[$reflect->getName()] as $property => $inject) {
/** @var Inject $inject */
$inject->execute([$object, $property]);
2021-03-24 18:08:50 +08:00
}
return $object;
2021-02-23 15:02:35 +08:00
}
2021-02-23 03:26:18 +08:00
2021-03-24 18:08:50 +08:00
2021-04-25 11:52:31 +08:00
/**
* @param string $class
* @param string|null $property
* @return ReflectionProperty|array|null
*/
public function getClassProperty(string $class, string $property = null): ReflectionProperty|null|array
{
if (!isset($this->_property[$class])) {
return null;
}
$properties = $this->_property[$class];
if (!empty($property)) {
return $properties[$property] ?? null;
}
return $properties;
}
2021-02-23 15:02:35 +08:00
/**
* @param $object
* @param $config
* @return mixed
*/
2021-04-25 11:52:31 +08:00
private function onAfterInit($object, $config): mixed
2021-02-23 15:02:35 +08:00
{
2021-04-25 12:32:34 +08:00
Snowflake::configure($object, $config);
2021-02-23 15:02:35 +08:00
if (method_exists($object, 'afterInit')) {
call_user_func([$object, 'afterInit']);
}
return $object;
}
2020-08-31 22:33:50 +08:00
2021-02-23 11:04:32 +08:00
/**
* @param $class
* @return array|null
2021-04-25 11:52:31 +08:00
* @throws ReflectionException|NotFindClassException
2021-02-23 11:04:32 +08:00
*/
2021-04-25 11:58:29 +08:00
private function resolveDependencies($class): ?array
2021-02-23 15:02:35 +08:00
{
2021-04-25 12:03:03 +08:00
if (!isset($this->_constructs[$class])) {
$this->_constructs[$class] = ['class' => $class];
}
2021-04-25 11:58:29 +08:00
if (isset($this->_reflection[$class])) {
2021-04-25 12:00:14 +08:00
return [$this->_reflection[$class], $this->_constructs[$class] ?? []];
2021-02-23 15:02:35 +08:00
}
2021-04-25 11:58:29 +08:00
$reflection = new ReflectionClass($class);
if (!$reflection->isInstantiable()) {
return null;
2021-02-23 15:02:35 +08:00
}
2021-04-25 11:52:31 +08:00
$this->scanProperty($reflection);
2021-04-25 11:58:29 +08:00
$this->_reflection[$class] = $reflection;
if (!is_null($construct = $reflection->getConstructor())) {
2021-04-25 12:03:03 +08:00
$this->_constructs[$class] = array_merge($this->_constructs[$class],
$this->resolveMethodParam($construct)
);
2021-04-25 11:58:29 +08:00
}
2021-04-25 11:59:39 +08:00
return [$reflection, $this->_constructs[$class] ?? []];
2021-02-23 15:02:35 +08:00
}
2021-02-23 03:26:18 +08:00
2021-02-23 15:02:35 +08:00
/**
2021-04-25 11:52:31 +08:00
* @param ReflectionClass $reflectionClass
* @return $this
*/
private function scanProperty(ReflectionClass $reflectionClass): static
{
$lists = $reflectionClass->getProperties(ReflectionProperty::IS_PUBLIC |
ReflectionProperty::IS_PRIVATE | ReflectionProperty::IS_PROTECTED
);
$className = $reflectionClass->getName();
foreach ($lists as $list) {
$targets = $list->getAttributes(Inject::class);
2021-04-25 11:53:20 +08:00
if (count($targets) < 1) {
2021-04-25 11:52:31 +08:00
continue;
}
$this->_property[$className][$list->getName()] = $targets[0]->newInstance();
}
return $this;
}
/**
* @param ReflectionMethod|null $method
2021-02-23 15:02:35 +08:00
* @return array
* @throws NotFindClassException
* @throws ReflectionException
*/
2021-04-25 11:52:31 +08:00
private function resolveMethodParam(?ReflectionMethod $method): array
2021-02-23 15:02:35 +08:00
{
$array = [];
foreach ($method->getParameters() as $key => $parameter) {
if ($parameter->isDefaultValueAvailable()) {
$array[] = $parameter->getDefaultValue();
} else {
$type = $parameter->getType();
if (is_string($type) && class_exists($type)) {
$type = Snowflake::createObject($type);
}
$array[] = match ($parameter->getType()) {
'string' => '',
'int', 'float' => 0,
'', null, 'object', 'mixed' => NULL,
'bool' => false,
default => $type
};
}
}
return $array;
}
2021-02-23 03:26:18 +08:00
2021-02-23 11:04:32 +08:00
/**
* @param $class
* @return ReflectionClass|null
* @throws NotFindClassException
* @throws ReflectionException
*/
2021-02-23 15:02:35 +08:00
public function getReflect($class): ?ReflectionClass
{
$reflect = $this->_reflection[$class] ?? null;
if (!is_null($reflect)) {
return $reflect;
}
$reflect = $this->resolveDependencies($class);
if (is_array($reflect)) {
return $reflect[0];
}
return null;
}
2021-02-22 18:23:33 +08:00
2021-02-23 15:02:35 +08:00
/**
* @param $class
*/
public function unset($class)
{
if (is_array($class) && isset($class['class'])) {
$class = $class['class'];
} else if (is_object($class)) {
2021-04-25 11:22:23 +08:00
$class = $class::class;
2021-02-23 15:02:35 +08:00
}
unset(
$this->_reflection[$class], $this->_singletons[$class],
$this->_param[$class], $this->_constructs[$class]
);
}
2020-08-31 01:27:08 +08:00
2021-02-23 15:02:35 +08:00
/**
* @return $this
*/
public function flush(): static
{
$this->_reflection = [];
$this->_singletons = [];
$this->_param = [];
$this->_constructs = [];
return $this;
}
2020-08-31 01:27:08 +08:00
2021-02-23 15:02:35 +08:00
/**
* @param $class
* @param $newParam
*
* @return mixed
*/
private function mergeParam($class, $newParam): array
{
if (empty($this->_param[$class])) {
return $newParam;
} else if (empty($newParam)) {
return $this->_param[$class];
}
$old = $this->_param[$class];
foreach ($newParam as $key => $val) {
$old[$key] = $val;
}
return $old;
}
2020-08-31 01:27:08 +08:00
}