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

303 lines
6.5 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;
use ReflectionClass;
use Snowflake\Abstracts\BaseObject;
use ReflectionException;
use Snowflake\Exception\NotFindClassException;
2021-02-22 18:51:16 +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
{
/**
* @var array
*
* instance class by className
*/
2020-10-29 18:17:25 +08:00
private array $_singletons = [];
2020-08-31 01:27:08 +08:00
/**
* @var array
*
* class new instance construct parameter
*/
2020-10-29 18:17:25 +08:00
private array $_constructs = [];
2020-08-31 01:27:08 +08:00
/**
* @var array
*
* implements \ReflectClass
*/
2020-10-29 18:17:25 +08:00
private array $_reflection = [];
2020-08-31 01:27:08 +08:00
/**
* @var array
*
* The construct parameter
*/
2020-10-29 18:17:25 +08:00
private array $_param = [];
2020-08-31 01:27:08 +08:00
2020-12-14 17:35:35 +08:00
/**
* @var array
*
* The method attributes
*/
private array $_attributes = [];
2020-08-31 01:27:08 +08:00
/**
* @param $class
* @param array $constrict
* @param array $config
*
* @return mixed
* @throws NotFindClassException
* @throws ReflectionException
*/
2020-12-14 17:35:35 +08:00
public function get($class, $constrict = [], $config = []): mixed
2020-08-31 01:27:08 +08:00
{
if (isset($this->_singletons[$class])) {
return $this->_singletons[$class];
} else if (!isset($this->_constructs[$class])) {
return $this->resolve($class, $constrict, $config);
}
$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;
}
/**
* @param $definition
* @param $class
* @param $config
* @param $constrict
* @return mixed
* @throws NotFindClassException
* @throws ReflectionException
*/
2020-12-14 17:35:35 +08:00
private function resolveDefinition($definition, $class, $config, $constrict): mixed
2020-08-31 01:27:08 +08:00
{
if (!isset($definition['class'])) {
throw new NotFindClassException($class);
}
$_className = $definition['class'];
unset($definition['class']);
$config = array_merge($definition, $config);
$definition = $this->mergeParam($class, $constrict);
if ($_className === $class) {
$object = $this->resolve($class, $definition, $config);
} else {
$object = $this->get($class, $definition, $config);
}
return $object;
}
/**
* @param $class
* @param $constrict
* @param $config
*
2020-12-14 17:35:35 +08:00
* @return object
2020-08-31 01:27:08 +08:00
* @throws NotFindClassException
* @throws ReflectionException
*/
2020-12-14 17:35:35 +08:00
private function resolve($class, $constrict, $config): object
2020-08-31 01:27:08 +08:00
{
/**
* @var ReflectionClass $reflect
* @var array $dependencies
*/
list($reflect, $dependencies) = $this->resolveDependencies($class);
foreach ($constrict as $index => $param) {
$dependencies[$index] = $param;
}
if (!$reflect->isInstantiable()) {
throw new NotFindClassException($reflect->getName());
}
if (empty($config)) {
return $reflect->newInstanceArgs($dependencies ?? []);
}
2020-08-31 12:38:32 +08:00
if (!empty($dependencies) && $reflect->implementsInterface('Snowflake\Abstracts\Configure')) {
2020-08-31 01:27:08 +08:00
$dependencies[count($dependencies) - 1] = $config;
return $reflect->newInstanceArgs($dependencies);
}
2020-09-17 13:56:57 +08:00
$this->_param[$class] = $config;
2020-08-31 01:27:08 +08:00
$object = $reflect->newInstanceArgs($dependencies ?? []);
foreach ($config as $key => $val) {
$object->{$key} = $val;
}
if (method_exists($object, 'afterInit')) {
call_user_func([$object, 'afterInit']);
}
return $object;
}
/**
* @param $class
*
* @return array
* @throws ReflectionException
2021-02-22 18:51:16 +08:00
* @throws NotFindClassException
2020-08-31 01:27:08 +08:00
*/
2020-12-14 17:35:35 +08:00
private function resolveDependencies($class): array
2020-08-31 01:27:08 +08:00
{
$dependencies = [];
2021-02-22 18:51:16 +08:00
if (!class_exists($class) || !($reflection = $this->resolveClass($class))) {
2021-02-22 18:28:03 +08:00
return [];
2021-02-22 18:51:16 +08:00
};
2020-08-31 01:27:08 +08:00
$constructs = $reflection->getConstructor();
2021-02-22 18:51:16 +08:00
if (!($constructs instanceof \ReflectionMethod)) {
2020-08-31 01:27:08 +08:00
return [$reflection, []];
}
foreach ($constructs->getParameters() as $key => $param) {
2021-02-22 19:07:34 +08:00
$dependencies[] = $this->resolveDefaultValue($param, $class);
2020-08-31 01:27:08 +08:00
}
$this->_constructs[$class] = $dependencies;
return [$reflection, $dependencies];
}
2020-08-31 22:33:50 +08:00
2021-02-22 18:53:55 +08:00
/**
* @param $param
2021-02-22 18:54:26 +08:00
* @return mixed
2021-02-22 18:53:55 +08:00
* @throws NotFindClassException
* @throws ReflectionException
*/
2021-02-22 19:07:34 +08:00
private function resolveDefaultValue(\ReflectionParameter $param, $class): mixed
2021-02-22 18:53:55 +08:00
{
if ($param->isOptional()) {
return $param->getDefaultValue();
}
2021-02-22 19:07:34 +08:00
try {
2021-02-22 19:10:39 +08:00
$type = $param->getType();
var_dump($type);
return match ($type) {
2021-02-22 19:07:34 +08:00
'mixed' => $param->getDefaultValue(),
'string' => '',
'int', 'float' => 0,
'bool' => false,
'', null, 'object' => NULL,
default => Snowflake::createObject($type)
};
} catch (\Throwable $throwable) {
2021-02-22 19:09:55 +08:00
var_dump($class, $throwable->getMessage(), $throwable->getFile(), $throwable->getLine());
echo PHP_EOL;
2021-02-22 19:07:34 +08:00
return null;
}
2021-02-22 18:53:55 +08:00
}
2021-02-22 18:51:16 +08:00
/**
* @param $class
* @return ReflectionClass|null
*/
private function resolveClass($class): ?ReflectionClass
{
if (!isset($this->_reflection[$class])) {
$reflection = new ReflectionClass($class);
if (!$reflection->isInstantiable()) {
return null;
}
$this->_reflection[$class] = $reflection;
}
return $this->_reflection[$class];
}
2021-02-22 18:23:33 +08:00
/**
* @param $class
* @return mixed
*/
public function getDependencies($class): mixed
{
return $this->_constructs[$class] ?? [];
}
2020-08-31 22:33:50 +08:00
/**
* @param $class
2021-02-22 18:11:23 +08:00
* @return ReflectionClass|null
2020-08-31 22:33:50 +08:00
* @throws ReflectionException
2021-02-22 18:56:50 +08:00
* @throws NotFindClassException
2020-08-31 22:33:50 +08:00
*/
2021-02-22 18:11:23 +08:00
public function getReflect($class): ?ReflectionClass
2020-08-31 22:33:50 +08:00
{
if (!isset($this->_reflection[$class])) {
$this->resolveDependencies($class);
}
2021-02-22 18:11:23 +08:00
return $this->_reflection[$class] ?? null;
2020-12-14 17:35:35 +08:00
}
2020-08-31 01:27:08 +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]
);
}
/**
* @return $this
*/
2020-12-14 17:35:35 +08:00
public function flush(): static
2020-08-31 01:27:08 +08:00
{
$this->_reflection = [];
$this->_singletons = [];
$this->_param = [];
$this->_constructs = [];
return $this;
}
/**
* @param $class
* @param $newParam
*
* @return mixed
*/
2020-12-14 17:35:35 +08:00
private function mergeParam($class, $newParam): array
2020-08-31 01:27:08 +08:00
{
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;
}
}