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

409 lines
9.3 KiB
PHP
Raw Normal View History

2020-08-31 01:27:08 +08:00
<?php
2021-08-02 17:32:51 +08:00
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/24 0024
* Time: 17:27
*/
declare(strict_types=1);
2020-08-31 01:27:08 +08:00
2021-08-02 17:32:51 +08:00
namespace Snowflake\Di;
2020-08-31 01:27:08 +08:00
2021-08-02 17:32:51 +08:00
use Annotation\Inject;
2021-04-25 11:52:31 +08:00
use Exception;
2021-08-02 17:32:51 +08:00
use ReflectionClass;
2020-08-31 01:27:08 +08:00
use ReflectionException;
2021-08-02 18:12:32 +08:00
use ReflectionFunction;
2021-08-02 17:32:51 +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
/**
2021-08-02 17:32:51 +08:00
* Class Container
* @package Snowflake\Di
2020-08-31 01:27:08 +08:00
*/
2021-08-02 17:32:51 +08:00
class Container extends BaseObject
2020-08-31 01:27:08 +08:00
{
2021-08-02 17:32:51 +08:00
use Attributes;
2021-08-02 17:31:24 +08:00
2021-08-02 17:32:51 +08:00
/**
* @var array
*
* instance class by className
*/
private array $_singletons = [];
2021-08-01 19:04:34 +08:00
2021-08-02 17:32:51 +08:00
/**
* @var ReflectionMethod[]
*
* class new instance construct parameter
*/
private array $_constructs = [];
2021-08-01 19:04:34 +08:00
/**
2021-08-02 17:32:51 +08:00
* @var array
*
* implements \ReflectClass
2021-08-01 19:04:34 +08:00
*/
2021-08-02 17:32:51 +08:00
private array $_reflection = [];
/** @var array */
private array $_parameters = [];
2021-08-02 17:31:24 +08:00
2021-08-01 19:04:34 +08:00
/**
2021-08-02 17:32:51 +08:00
* @var array
*
* The construct parameter
2021-08-02 17:31:24 +08:00
*/
2021-08-02 17:32:51 +08:00
private array $_param = [];
2021-08-02 17:31:24 +08:00
/**
2021-08-02 17:32:51 +08:00
* @param $class
* @param array $constrict
* @param array $config
*
* @return mixed
2021-08-01 19:04:34 +08:00
* @throws NotFindClassException
* @throws ReflectionException
* @throws Exception
*/
2021-08-02 17:32:51 +08:00
public function get($class, array $constrict = [], array $config = []): mixed
2021-08-01 19:04:34 +08:00
{
2021-08-02 17:32:51 +08:00
if (!isset($this->_singletons[$class])) {
$this->_singletons[$class] = $this->resolve($class, $constrict, $config);
2021-08-01 19:04:34 +08:00
}
2021-08-02 17:32:51 +08:00
return $this->_singletons[$class];
2021-08-01 19:04:34 +08:00
}
/**
2021-08-02 17:32:51 +08:00
* @param $class
* @param array $constrict
* @param array $config
* @return object
2021-08-01 19:04:34 +08:00
* @throws NotFindClassException
* @throws ReflectionException
* @throws Exception
*/
2021-08-02 17:32:51 +08:00
public function newObject($class, array $constrict = [], array $config = []): object
2021-08-01 19:04:34 +08:00
{
2021-08-02 17:32:51 +08:00
return $this->resolve($class, $constrict, $config);
2021-08-01 19:04:34 +08:00
}
/**
2021-08-02 17:32:51 +08:00
* @param $class
* @param $constrict
* @param $config
*
* @return object
2021-08-01 19:04:34 +08:00
* @throws Exception
*/
2021-08-02 17:32:51 +08:00
private function resolve($class, $constrict, $config): object
2021-08-01 19:04:34 +08:00
{
2021-08-02 17:32:51 +08:00
$reflect = $this->resolveDependencies($class);
2021-08-04 17:51:33 +08:00
if (!$reflect->isInstantiable()) {
throw new ReflectionException('Class ' . $class . ' cannot be instantiated');
}
2021-08-04 16:58:10 +08:00
2021-08-03 18:26:11 +08:00
$object = $this->newInstance($reflect, $constrict);
2021-08-01 19:04:34 +08:00
2021-08-03 18:26:11 +08:00
$this->propertyInject($reflect, $object);
2021-08-01 19:04:34 +08:00
2021-08-02 17:32:51 +08:00
return $this->onAfterInit($object, $config);
2021-08-01 19:04:34 +08:00
}
/**
2021-08-02 17:32:51 +08:00
* @param ReflectionClass $reflect
* @param $dependencies
* @return object
2021-08-02 16:38:50 +08:00
* @throws ReflectionException
* @throws NotFindClassException
2021-08-01 19:04:34 +08:00
*/
2021-08-02 17:32:51 +08:00
private function newInstance(ReflectionClass $reflect, $dependencies): object
2021-08-01 19:04:34 +08:00
{
2021-08-02 17:32:51 +08:00
if (!isset($this->_constructs[$reflect->getName()])) {
return $reflect->newInstance();
2021-08-01 19:04:34 +08:00
}
2021-08-02 17:32:51 +08:00
$construct = $this->_constructs[$reflect->getName()];
if ($construct->getNumberOfParameters() < 1) {
return $reflect->newInstance();
}
2021-08-05 17:41:21 +08:00
$parameters = $this->mergeParam($this->resolveMethodParameters($construct), $dependencies);
return $reflect->newInstanceArgs($parameters);
2021-08-01 19:04:34 +08:00
}
/**
2021-08-02 17:32:51 +08:00
* @param ReflectionClass $reflect
* @param $object
* @return mixed
* @throws Exception
2021-08-01 19:04:34 +08:00
*/
2021-08-02 17:32:51 +08:00
public function propertyInject(ReflectionClass $reflect, $object): mixed
2021-08-01 19:04:34 +08:00
{
2021-08-02 17:32:51 +08:00
foreach ($this->getPropertyNote($reflect) as $property => $inject) {
/** @var Inject $inject */
$inject->execute($object, $property);
2021-08-01 19:04:34 +08:00
}
2021-08-02 17:32:51 +08:00
return $object;
2021-08-01 19:04:34 +08:00
}
/**
2021-08-02 17:32:51 +08:00
* @param $className
* @param $method
* @return array
2021-08-04 16:58:10 +08:00
* @throws ReflectionException
2021-08-01 19:04:34 +08:00
*/
2021-08-02 17:32:51 +08:00
public function getMethodAttribute($className, $method = null): array
2021-08-01 19:04:34 +08:00
{
2021-08-02 17:32:51 +08:00
$methods = $this->getMethodNote($this->getReflect($className));
if (!empty($method)) {
return $methods[$method] ?? [];
}
return $methods;
2021-08-01 19:04:34 +08:00
}
/**
2021-08-02 17:32:51 +08:00
* @param string $class
* @param string|null $property
* @return ReflectionProperty|ReflectionProperty[]|null
2021-08-04 16:58:10 +08:00
* @throws ReflectionException
2021-08-01 19:04:34 +08:00
*/
2021-08-02 17:32:51 +08:00
public function getClassReflectionProperty(string $class, string $property = null): ReflectionProperty|null|array
2021-08-01 19:04:34 +08:00
{
2021-08-02 17:32:51 +08:00
$lists = $this->getProperty($this->getReflect($class));
if (empty($lists)) {
return null;
2021-08-01 19:04:34 +08:00
}
2021-08-02 17:32:51 +08:00
if (!empty($property)) {
return $lists[$property] ?? null;
2021-08-01 19:04:34 +08:00
}
2021-08-02 17:32:51 +08:00
return $lists;
2021-08-01 19:04:34 +08:00
}
/**
2021-08-02 17:32:51 +08:00
* @param $object
* @param $config
* @return mixed
2021-08-01 19:04:34 +08:00
*/
2021-08-02 17:32:51 +08:00
private function onAfterInit($object, $config): mixed
2021-08-01 19:04:34 +08:00
{
2021-08-02 17:32:51 +08:00
Snowflake::configure($object, $config);
2021-08-03 18:26:11 +08:00
if (method_exists($object, 'init')) {
call_user_func([$object, 'init']);
2021-08-01 19:04:34 +08:00
}
2021-08-02 17:32:51 +08:00
return $object;
2021-08-01 19:04:34 +08:00
}
2021-08-02 16:38:50 +08:00
2021-08-01 19:04:34 +08:00
/**
2021-08-02 17:32:51 +08:00
* @param $class
* @return ReflectionClass
2021-08-01 19:04:34 +08:00
*/
2021-08-02 17:32:51 +08:00
private function resolveDependencies($class): ReflectionClass
2021-08-01 19:04:34 +08:00
{
2021-08-05 18:52:37 +08:00
if (isset($this->_reflection[$class])) {
return $this->_reflection[$class];
}
2021-08-02 17:32:51 +08:00
$reflect = new ReflectionClass($class);
2021-08-04 17:51:33 +08:00
if ($reflect->isAbstract() || $reflect->isTrait() || $reflect->isInterface()) {
return $this->_reflection[$class] = $reflect;
2021-08-04 16:58:10 +08:00
}
2021-08-02 17:32:51 +08:00
$this->setPropertyNote($reflect);
$this->setTargetNote($reflect);
$this->setMethodNote($reflect);
$construct = $reflect->getConstructor();
if (!empty($construct) && $construct->getNumberOfParameters() > 0) {
$this->_constructs[$class] = $construct;
2021-08-01 19:04:34 +08:00
}
2021-08-02 17:32:51 +08:00
return $this->_reflection[$class] = $reflect;
2021-08-01 19:04:34 +08:00
}
/**
2021-08-02 17:32:51 +08:00
* @param ReflectionClass|string $class
* @return ReflectionMethod[]
2021-08-04 16:58:10 +08:00
* @throws ReflectionException
2021-08-01 19:04:34 +08:00
*/
2021-08-02 18:15:00 +08:00
public function getReflectMethods(ReflectionClass|string $class): array
2021-08-01 19:04:34 +08:00
{
2021-08-02 18:15:00 +08:00
if (is_string($class)) {
$class = $this->getReflect($class);
}
2021-08-02 17:32:51 +08:00
return $this->getMethods($class);
2021-08-02 16:38:50 +08:00
}
2021-08-01 19:04:34 +08:00
2021-08-02 16:38:50 +08:00
/**
2021-08-02 17:32:51 +08:00
* @param ReflectionClass|string $class
* @param string $method
* @return ReflectionMethod|null
2021-08-04 16:58:10 +08:00
* @throws ReflectionException
2021-08-02 16:38:50 +08:00
*/
2021-08-02 18:15:00 +08:00
public function getReflectMethod(ReflectionClass|string $class, string $method): ?ReflectionMethod
2021-08-02 16:38:50 +08:00
{
2021-08-02 17:32:51 +08:00
return $this->getReflectMethods($class)[$method] ?? null;
2021-08-01 19:04:34 +08:00
}
/**
2021-08-02 17:32:51 +08:00
* @param ReflectionClass|string $class
* @param string $method
* @return array|null
* @throws NotFindClassException
* @throws ReflectionException
2021-08-01 19:04:34 +08:00
*/
2021-08-02 17:32:51 +08:00
public function getMethodParameters(ReflectionClass|string $class, string $method): ?array
2021-08-01 19:04:34 +08:00
{
2021-08-02 17:32:51 +08:00
$className = $class;
if (is_object($class)) $className = $class->getName();
if (isset($this->_parameters[$className]) && isset($this->_parameters[$className][$method])) {
return $this->_parameters[$className][$method];
2021-08-02 16:38:50 +08:00
}
2021-08-02 17:32:51 +08:00
$reflectMethod = $this->getReflectMethod($class, $method);
if (!($reflectMethod instanceof ReflectionMethod)) {
throw new ReflectionException("Class does not have a function $className::$method");
}
2021-08-02 18:12:32 +08:00
$className = $reflectMethod->getDeclaringClass()->getName();
if (isset($this->_parameters[$className]) && isset($this->_parameters[$className][$reflectMethod->getName()])) {
return $this->_parameters[$className][$reflectMethod->getName()];
}
return $this->setParameters($className, $reflectMethod->getName(), $this->resolveMethodParameters($reflectMethod));
}
/**
* @param $class
* @param $method
* @param $parameters
*/
private function setParameters($class, $method, $parameters)
{
if (!isset($this->_parameters[$class])) {
$this->_parameters[$class] = [];
}
if (!isset($this->_parameters[$class][$method])) {
$this->_parameters[$class][$method] = [];
}
return $this->_parameters[$class][$method] = $parameters;
2021-08-01 19:04:34 +08:00
}
/**
2021-08-02 18:12:32 +08:00
* @param \Closure $reflectionMethod
2021-08-02 17:32:51 +08:00
* @return array
2021-08-02 17:31:24 +08:00
* @throws NotFindClassException
2021-08-01 19:04:34 +08:00
* @throws ReflectionException
*/
2021-08-02 18:12:32 +08:00
public function resolveFunctionParameters(\Closure $reflectionMethod): array
{
return $this->resolveMethodParameters(new ReflectionFunction($reflectionMethod));
}
/**
* @param ReflectionMethod|ReflectionFunction $reflectionMethod
* @return array
* @throws NotFindClassException
* @throws ReflectionException
*/
private function resolveMethodParameters(ReflectionMethod|ReflectionFunction $reflectionMethod): array
2021-08-01 19:04:34 +08:00
{
2021-08-02 17:32:51 +08:00
if ($reflectionMethod->getNumberOfParameters() < 1) {
return [];
}
2021-08-02 18:12:32 +08:00
$params = [];
2021-08-02 17:32:51 +08:00
foreach ($reflectionMethod->getParameters() as $key => $parameter) {
if ($parameter->isDefaultValueAvailable()) {
$params[$key] = $parameter->getDefaultValue();
2021-08-05 15:30:23 +08:00
} else if ($parameter->getType() === null) {
$params[$key] = $parameter->getType();
2021-08-02 17:32:51 +08:00
} else {
$type = $parameter->getType()->getName();
if (is_string($type) && class_exists($type)) {
$type = Snowflake::getDi()->get($type);
}
$params[$key] = match ($parameter->getType()) {
'string' => '',
'int', 'float' => 0,
'', null, 'object', 'mixed' => NULL,
'bool' => false,
default => $type
};
2021-08-02 17:31:24 +08:00
}
2021-08-01 19:04:34 +08:00
}
2021-08-02 18:12:32 +08:00
return $params;
2021-08-01 19:04:34 +08:00
}
2021-08-02 17:31:24 +08:00
2021-08-01 19:04:34 +08:00
/**
2021-08-02 17:32:51 +08:00
* @param $class
* @return ReflectionClass|null
2021-08-04 16:58:10 +08:00
* @throws ReflectionException
2021-08-01 19:04:34 +08:00
*/
2021-08-02 17:32:51 +08:00
public function getReflect($class): ?ReflectionClass
2021-08-01 19:04:34 +08:00
{
2021-08-02 17:32:51 +08:00
if (!isset($this->_reflection[$class])) {
return $this->resolveDependencies($class);
2021-08-02 17:31:24 +08:00
}
2021-08-02 17:32:51 +08:00
return $this->_reflection[$class];
}
/**
* @param $class
*/
public function unset($class)
{
if (is_array($class) && isset($class['class'])) {
$class = $class['class'];
} else if (is_object($class)) {
$class = $class::class;
2021-08-01 19:04:34 +08:00
}
2021-08-02 17:32:51 +08:00
unset(
$this->_reflection[$class], $this->_singletons[$class],
$this->_param[$class], $this->_constructs[$class]
);
2021-08-01 19:04:34 +08:00
}
2021-08-02 17:32:51 +08:00
/**
* @return $this
*/
public function flush(): static
{
$this->_reflection = [];
$this->_singletons = [];
$this->_param = [];
$this->_constructs = [];
return $this;
}
2021-08-01 19:04:34 +08:00
/**
2021-08-02 17:32:51 +08:00
* @param $old
* @param $newParam
*
* @return mixed
2021-08-01 19:04:34 +08:00
*/
2021-08-02 17:32:51 +08:00
private function mergeParam($old, $newParam): array
2021-08-01 19:04:34 +08:00
{
2021-08-02 17:32:51 +08:00
if (empty($old)) {
return $newParam;
} else if (empty($newParam)) {
return $old;
}
foreach ($newParam as $key => $val) {
$old[$key] = $val;
2021-08-01 19:04:34 +08:00
}
2021-08-02 17:32:51 +08:00
return $old;
2021-08-01 19:04:34 +08:00
}
2020-08-31 01:27:08 +08:00
}