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

439 lines
9.7 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-11 01:04:57 +08:00
namespace Kiri\Di;
2020-08-31 01:27:08 +08:00
2021-08-02 17:32:51 +08:00
use Annotation\Inject;
2021-08-25 13:50:40 +08:00
use Closure;
2021-04-25 11:52:31 +08:00
use Exception;
2021-08-13 16:37:37 +08:00
use Kiri\Abstracts\BaseObject;
2021-08-24 19:11:14 +08:00
use Kiri\Abstracts\Logger;
2021-08-13 16:37:37 +08:00
use Kiri\Kiri;
2021-08-24 19:11:14 +08:00
use Psr\Log\LoggerInterface;
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;
2020-08-31 01:27:08 +08:00
/**
2021-08-02 17:32:51 +08:00
* Class Container
2021-08-11 01:04:57 +08:00
* @package Kiri\Di
2020-08-31 01:27:08 +08:00
*/
2021-08-17 16:23:49 +08:00
class Container extends BaseObject implements ContainerInterface
2020-08-31 01:27:08 +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-13 16:37:37 +08:00
/** @var array|string[] */
private array $_interfaces = [
2021-10-26 10:22:56 +08:00
LoggerInterface::class => Logger::class
2021-08-13 16:37:37 +08:00
];
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-09-06 14:30:57 +08:00
* @throws
2021-08-01 19:04:34 +08:00
*/
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-13 17:00:48 +08:00
if ($this->isInterface($class)) {
2021-08-13 16:37:37 +08:00
$class = $this->_interfaces[$class];
}
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-13 17:06:52 +08:00
/**
2021-08-13 17:10:10 +08:00
* @param string $interface
* @param string $class
2021-08-13 17:06:52 +08:00
*/
2021-08-13 17:10:10 +08:00
public function mapping(string $interface, string $class)
2021-08-13 17:06:52 +08:00
{
$this->_interfaces[$interface] = $class;
}
2021-08-13 16:37:37 +08:00
/**
* @param $class
* @return bool
* @throws ReflectionException
*/
2021-08-13 17:00:48 +08:00
public function isInterface($class): bool
2021-08-13 16:37:37 +08:00
{
$reflect = $this->getReflect($class);
if ($reflect->isInterface()) {
return true;
}
return false;
}
2021-08-17 16:23:49 +08:00
/**
* @param string $interface
* @param $object
*/
public function setBindings(string $interface, $object)
{
2021-10-26 10:22:56 +08:00
if (is_string($object)) {
$this->_interfaces[$interface] = $object;
} else {
2021-10-26 11:40:16 +08:00
$className = get_class($object);
$this->_interfaces[$interface] = $className;
$this->_singletons[$className] = $object;
2021-10-26 10:22:56 +08:00
}
2021-08-17 16:23:49 +08:00
}
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-09-06 14:31:40 +08:00
* @throws
2021-08-01 19:04:34 +08:00
*/
2021-09-29 17:32:56 +08:00
public function create($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
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-09-04 00:08:34 +08:00
foreach (NoteManager::getPropertyNote($reflect) as $property => $inject) {
2021-08-02 17:32:51 +08:00
/** @var Inject $inject */
2021-09-02 11:27:59 +08:00
$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-09-04 00:08:34 +08:00
$methods = NoteManager::getMethodNote($this->getReflect($className));
2021-08-02 17:32:51 +08:00
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-09-04 00:08:34 +08:00
$lists = NoteManager::getProperty($this->getReflect($class));
2021-08-02 17:32:51 +08:00
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-11 01:04:57 +08:00
Kiri::configure($object, $config);
2021-09-02 11:27:59 +08:00
if (method_exists($object, 'init') && is_callable([$object, 'init'])) {
2021-08-03 18:26:11 +08:00
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-25 13:50:40 +08:00
* @throws ReflectionException
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-10-28 17:11:20 +08:00
if ($reflect->isAbstract() || $reflect->isTrait() || $reflect->isInterface()) {
2021-08-04 17:51:33 +08:00
return $this->_reflection[$class] = $reflect;
2021-08-04 16:58:10 +08:00
}
2021-10-26 10:22:56 +08:00
$construct = NoteManager::resolveTarget($reflect);
2021-08-02 17:32:51 +08:00
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-09-04 00:08:34 +08:00
return NoteManager::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-09-17 18:55:08 +08:00
* @param string $className
2021-08-02 17:32:51 +08:00
* @param string $method
* @return array|null
* @throws ReflectionException
2021-08-01 19:04:34 +08:00
*/
2021-09-04 17:06:05 +08:00
public function getMethodParameters(string $className, string $method): ?array
2021-08-01 19:04:34 +08:00
{
2021-08-02 17:32:51 +08:00
if (isset($this->_parameters[$className]) && isset($this->_parameters[$className][$method])) {
return $this->_parameters[$className][$method];
2021-08-02 16:38:50 +08:00
}
2021-09-04 17:06:05 +08:00
$reflectMethod = $this->getReflectMethod($this->getReflect($className), $method);
2021-08-02 17:32:51 +08:00
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
2021-08-25 13:50:40 +08:00
* @return mixed
2021-08-02 18:12:32 +08:00
*/
2021-08-25 13:50:40 +08:00
private function setParameters($class, $method, $parameters): mixed
2021-08-02 18:12:32 +08:00
{
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-25 13:50:40 +08:00
* @param Closure $reflectionMethod
2021-08-02 17:32:51 +08:00
* @return array
2021-08-01 19:04:34 +08:00
* @throws ReflectionException
*/
2021-09-04 17:06:05 +08:00
public function getFunctionParameters(Closure $reflectionMethod): array
2021-08-02 18:12:32 +08:00
{
return $this->resolveMethodParameters(new ReflectionFunction($reflectionMethod));
}
/**
* @param ReflectionMethod|ReflectionFunction $reflectionMethod
* @return array
* @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();
2021-08-25 13:50:40 +08:00
if (is_string($type) && class_exists($type) || isset($this->_interfaces[$type])) {
2021-08-11 01:04:57 +08:00
$type = Kiri::getDi()->get($type);
2021-08-02 17:32:51 +08:00
}
$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(
2021-09-04 17:06:05 +08:00
$this->_reflection[$class], $this->_singletons[$class], $this->_constructs[$class]
2021-08-02 17:32:51 +08:00
);
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->_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
}