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

256 lines
6.7 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-02-23 01:45:58 +08:00
use HttpServer\Http\HttpHeaders;
2020-08-31 01:27:08 +08:00
use ReflectionClass;
use Snowflake\Abstracts\BaseObject;
use ReflectionException;
use Snowflake\Exception\NotFindClassException;
2021-02-23 01:28:32 +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 00:31:31 +08:00
/**
* @var array
*
* instance class by className
*/
private array $_singletons = [];
2020-08-31 01:27:08 +08:00
2021-02-23 00:31:31 +08:00
/**
* @var array
*
* class new instance construct parameter
*/
private array $_constructs = [];
2020-08-31 01:27:08 +08:00
2021-02-23 00:31:31 +08:00
/**
* @var array
*
* implements \ReflectClass
*/
private array $_reflection = [];
2020-08-31 01:27:08 +08:00
2021-02-23 00:31:31 +08:00
/**
* @var array
*
* The construct parameter
*/
private array $_param = [];
2020-08-31 01:27:08 +08:00
2021-02-23 00:31:31 +08:00
/**
* @param $class
* @param array $constrict
* @param array $config
*
* @return mixed
* @throws NotFindClassException
* @throws ReflectionException
*/
public function get($class, $constrict = [], $config = []): mixed
{
if (isset($this->_singletons[$class])) {
return $this->_singletons[$class];
2021-02-23 01:06:07 +08:00
}
if (!isset($this->_constructs[$class])) {
2021-02-23 00:31:31 +08:00
return $this->resolve($class, $constrict, $config);
}
2021-02-23 02:32:18 +08:00
$definition = $this->_param[$class];
2021-02-23 00:31:31 +08:00
if (is_callable($definition, TRUE)) {
return call_user_func($definition, $this, $constrict, $config);
} else if (is_array($definition)) {
2021-02-23 02:32:18 +08:00
$definition = $this->_constructs[$class] ?? [];
if ($class === $definition['class']) {
$object = $this->resolve($class, $definition, $config);
} else {
$object = $this->get($class, $definition, $config);
}
return $this->_singletons[$class] = $object;
2021-02-23 00:31:31 +08:00
} else if (is_object($definition)) {
return $this->_singletons[$class] = $definition;
} else {
throw new NotFindClassException($class);
}
}
2020-08-31 01:27:08 +08:00
2021-02-23 00:31:31 +08:00
/**
* @param $class
* @param $constrict
* @param $config
*
* @return object
* @throws NotFindClassException
* @throws ReflectionException
*/
private function resolve($class, $constrict, $config): object
{
2021-02-23 01:28:32 +08:00
$reflect = $this->resolveDependencies($class, $constrict);
2021-02-23 00:31:31 +08:00
if (!$reflect->isInstantiable()) {
throw new NotFindClassException($reflect->getName());
}
2021-02-23 01:44:40 +08:00
$dependencies = $this->_constructs[$class] ?? [];
2021-02-23 00:31:31 +08:00
if (empty($config)) {
2021-02-23 01:44:40 +08:00
return $reflect->newInstanceArgs($dependencies);
2021-02-23 00:31:31 +08:00
}
2021-02-23 01:44:40 +08:00
$this->_param[$class] = $config;
2021-02-23 01:28:32 +08:00
2021-02-23 00:31:31 +08:00
if (!empty($dependencies) && $reflect->implementsInterface('Snowflake\Abstracts\Configure')) {
$dependencies[count($dependencies) - 1] = $config;
return $reflect->newInstanceArgs($dependencies);
}
2021-02-23 00:59:41 +08:00
2021-02-23 01:44:40 +08:00
return $this->onAfterInit($reflect->newInstanceArgs($dependencies), $config);
2021-02-23 00:31:31 +08:00
}
2020-08-31 01:27:08 +08:00
2021-02-23 00:31:31 +08:00
/**
2021-02-23 01:28:32 +08:00
* @param $object
* @param $config
* @return mixed
2021-02-23 00:31:31 +08:00
*/
2021-02-23 01:28:32 +08:00
private function onAfterInit($object, $config)
2021-02-23 00:31:31 +08:00
{
2021-02-23 01:28:32 +08:00
Snowflake::configure($object, $config);
if (method_exists($object, 'afterInit')) {
call_user_func([$object, 'afterInit']);
2021-02-23 00:31:31 +08:00
}
2021-02-23 01:28:32 +08:00
return $object;
2021-02-23 00:31:31 +08:00
}
2020-08-31 22:33:50 +08:00
2021-02-23 00:31:31 +08:00
/**
* @param $class
2021-02-23 01:28:32 +08:00
*
2021-02-23 00:53:30 +08:00
* @return array
2021-02-23 01:28:32 +08:00
* @throws ReflectionException
2021-02-23 00:31:31 +08:00
*/
2021-02-23 01:28:32 +08:00
private function resolveDependencies($class, $constrict = []): ?ReflectionClass
2021-02-23 00:31:31 +08:00
{
2021-02-23 01:47:49 +08:00
if (!isset($this->_reflection[$class])) {
2021-02-23 00:53:30 +08:00
$reflection = new ReflectionClass($class);
if (!$reflection->isInstantiable()) {
return null;
}
2021-02-23 01:52:57 +08:00
$this->_reflection[$class] = $reflection;
2021-02-23 01:47:49 +08:00
} else {
$reflection = $this->_reflection[$class];
2021-02-23 00:31:31 +08:00
}
2021-02-23 01:47:49 +08:00
2021-02-23 02:05:21 +08:00
if (!is_null($construct = $reflection->getConstructor())) {
$this->_constructs[$class] = $this->resolveMethodParam($construct, $constrict);
} else {
$this->_constructs[$class] = $constrict;
}
2021-02-23 00:53:30 +08:00
return $reflection;
2021-02-23 00:31:31 +08:00
}
2021-02-22 18:53:55 +08:00
2021-02-22 18:51:16 +08:00
2021-02-23 01:36:51 +08:00
/**
* @param \ReflectionMethod|null $method
* @return array
* @throws NotFindClassException
* @throws ReflectionException
*/
private function resolveMethodParam(?\ReflectionMethod $method, $default = []): array
{
$array = [];
foreach ($method->getParameters() as $key => $parameter) {
2021-02-23 01:37:43 +08:00
if (!is_null($default[$key] ?? null)) {
2021-02-23 01:36:51 +08:00
$array[] = $default[$key];
} else 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 00:31:31 +08:00
/**
2021-02-23 00:53:30 +08:00
* @param $class
* @return mixed
2021-02-23 00:31:31 +08:00
* @throws ReflectionException
*/
2021-02-23 01:40:48 +08:00
public function getReflect($class): ?ReflectionClass
2021-02-23 00:31:31 +08:00
{
2021-02-23 01:40:23 +08:00
$reflect = $this->_reflection[$class] ?? null;
if (!is_null($reflect)) {
return $reflect;
2021-02-23 00:31:31 +08:00
}
2021-02-23 01:40:23 +08:00
return $this->resolveDependencies($class);
2021-02-23 00:31:31 +08:00
}
2021-02-22 18:23:33 +08:00
2021-02-23 00:31:31 +08:00
/**
* @param $class
*/
public function unset($class)
{
if (is_array($class) && isset($class['class'])) {
$class = $class['class'];
} else if (is_object($class)) {
$class = get_class($class);
}
unset(
$this->_reflection[$class], $this->_singletons[$class],
$this->_param[$class], $this->_constructs[$class]
);
}
2020-08-31 01:27:08 +08:00
2021-02-23 00:31:31 +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 00:31:31 +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
}