Files
kiri-container/Container.php
T

405 lines
10 KiB
PHP
Raw Normal View History

2022-01-25 15:35:48 +08:00
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/24 0024
* Time: 17:27
*/
declare(strict_types=1);
namespace Kiri\Di;
2023-04-15 23:32:00 +08:00
2023-11-16 21:13:16 +08:00
use Closure;
2023-07-31 23:08:59 +08:00
use Exception;
2023-04-19 12:35:39 +08:00
use Kiri\Di\Interface\InjectProxyInterface;
2023-04-18 10:26:32 +08:00
use Kiri\Router\Interface\ValidatorInterface;
2023-04-15 23:32:00 +08:00
use Psr\Container\ContainerInterface;
2023-12-01 22:43:54 +08:00
use ReflectionAttribute;
2022-01-25 15:35:48 +08:00
use ReflectionClass;
use ReflectionException;
use ReflectionFunction;
use ReflectionMethod;
2023-04-15 23:32:00 +08:00
use ReflectionParameter;
2022-01-25 15:35:48 +08:00
2023-11-30 17:10:37 +08:00
/**
* Class Container
*/
2022-01-25 15:35:48 +08:00
class Container implements ContainerInterface
{
2023-04-15 23:32:00 +08:00
2023-05-25 16:59:17 +08:00
/**
* @var array
*
* instance class by className
*/
private array $_singletons = [];
/**
* @var array
*
* implements \ReflectClass
*/
private array $_reflection = [];
/**
* @var array
*/
private array $_parameters = [];
/**
* @var array
*/
private array $_interfaces = [];
2023-12-01 22:57:25 +08:00
/**
* @var Container|null
*/
private static ?Container $container = null;
2023-05-25 16:59:17 +08:00
2023-07-31 23:08:59 +08:00
/**
2023-12-01 22:57:25 +08:00
* Construct \ContainerInterface
2023-07-31 23:08:59 +08:00
*/
2023-05-25 16:59:17 +08:00
private function __construct()
{
2023-12-01 22:57:25 +08:00
$this->_singletons[ContainerInterface::class] = $this;
2023-05-25 16:59:17 +08:00
}
/**
* @return static
*/
public static function instance(): static
{
if (static::$container === null) {
static::$container = new Container();
}
return static::$container;
}
/**
* @param string $id
* @return mixed
2023-12-12 15:35:35 +08:00
* @throws
2023-05-25 16:59:17 +08:00
*/
2023-05-26 10:16:21 +08:00
public function get(string $id): object
2023-05-25 16:59:17 +08:00
{
2023-12-01 22:43:54 +08:00
if (isset($this->_singletons[$id])) return $this->_singletons[$id];
2023-11-16 23:01:58 +08:00
if (isset($this->_interfaces[$id])) {
2023-12-01 22:57:25 +08:00
return $this->_singletons[$id] = $this->make($this->_interfaces[$id]);
} else {
return $this->_singletons[$id] = $this->make($id);
2023-11-16 23:01:58 +08:00
}
2023-05-25 16:59:17 +08:00
}
/**
* @param string $id
2023-08-03 14:08:16 +08:00
* @return object
2023-12-12 15:35:35 +08:00
* @throws
2023-05-25 16:59:17 +08:00
*/
2023-08-03 14:08:16 +08:00
public function parse(string $id): object
2023-05-25 16:59:17 +08:00
{
2023-11-16 23:01:58 +08:00
if (!isset($this->_singletons[$id])) {
return $this->make($id);
2023-08-03 14:02:06 +08:00
}
2023-11-16 23:01:58 +08:00
return $this->_singletons[$id];
2023-05-25 16:59:17 +08:00
}
/**
* @param string $interface
* @param string $class
* @return void
*/
public function set(string $interface, string $class): void
{
$this->_interfaces[$interface] = $class;
}
/**
* @param string $interface
* @param object $object
2023-11-30 17:02:20 +08:00
* @return object
2023-05-25 16:59:17 +08:00
*/
2023-11-30 17:02:20 +08:00
public function bind(string $interface, object $object): object
2023-05-25 16:59:17 +08:00
{
$this->_singletons[$interface] = $object;
2023-11-30 17:02:20 +08:00
return $object;
2023-05-25 16:59:17 +08:00
}
/**
* @param string $className
* @return ReflectionClass
2023-12-12 15:35:35 +08:00
* @throws
2023-05-25 16:59:17 +08:00
*/
public function getReflectionClass(string $className): ReflectionClass
{
2023-12-01 22:43:54 +08:00
if (!isset($this->_reflection[$className])) {
$this->_reflection[$className] = new ReflectionClass($className);
2023-05-25 16:59:17 +08:00
}
2023-12-01 22:43:54 +08:00
return $this->_reflection[$className];
2023-05-25 16:59:17 +08:00
}
/**
* @param string $className
* @param array $construct
* @param array $config
* @return object|null
2023-12-12 15:35:35 +08:00
* @throws
2023-05-25 16:59:17 +08:00
*/
public function make(string $className, array $construct = [], array $config = []): ?object
{
$reflect = $this->getReflectionClass($className);
if (!$reflect->isInstantiable()) {
throw new ReflectionException('Class ' . $className . ' cannot be instantiated');
}
2023-12-01 22:43:54 +08:00
if (($handler = $reflect->getConstructor()) !== null) {
$construct = $this->getMethodParams($handler);
2023-05-25 16:59:17 +08:00
}
2023-12-01 22:43:54 +08:00
$newInstance = $reflect->newInstanceArgs($construct);
2023-05-25 16:59:17 +08:00
2023-12-01 22:43:54 +08:00
return $this->runInit($reflect, static::configure($newInstance, $config));
2023-05-25 16:59:17 +08:00
}
/**
2023-12-01 22:43:54 +08:00
* @param ReflectionClass $reflect
2023-05-25 16:59:17 +08:00
* @param object $object
2023-12-01 22:43:54 +08:00
* @return void
*/
protected function injectClassTarget(ReflectionClass $reflect, object $object): void
{
$this->resolveProperties($reflect, $object);
$attributes = $reflect->getAttributes();
foreach ($attributes as $attribute) {
if (class_exists($attribute->getName())) {
$instance = $attribute->newInstance();
if ($object instanceof InjectProxyInterface) {
$instance->dispatch($reflect->getFileName(), $object);
} else {
$instance->dispatch($object);
}
}
}
}
/**
2023-05-25 16:59:17 +08:00
* @param ReflectionClass $reflect
2023-12-01 22:43:54 +08:00
* @param object $object
2023-05-25 16:59:17 +08:00
* @return object
*/
2023-12-01 22:43:54 +08:00
protected function runInit(ReflectionClass $reflect, object $object): object
2023-05-25 16:59:17 +08:00
{
2023-12-01 22:43:54 +08:00
$this->injectClassTarget($reflect, $object);
if ($reflect->getName() === 'Symfony\Component\Console\Application') {
return $object;
2023-05-25 16:59:17 +08:00
}
2023-12-01 22:43:54 +08:00
if (method_exists($object, 'init')) {
2023-05-25 16:59:17 +08:00
call_user_func([$object, 'init']);
}
return $object;
}
/**
2023-12-01 22:43:54 +08:00
* @param ReflectionClass $reflectionClass
2023-05-25 16:59:17 +08:00
* @param object $class
* @return void
*/
2023-12-01 22:43:54 +08:00
public function resolveProperties(ReflectionClass $reflectionClass, object $class): void
2023-05-25 16:59:17 +08:00
{
2023-12-01 22:43:54 +08:00
$properties = $reflectionClass->getProperties();
$filename = $reflectionClass->getFileName();
2023-05-25 16:59:17 +08:00
foreach ($properties as $property) {
$propertyAttributes = $property->getAttributes();
foreach ($propertyAttributes as $attribute) {
2023-12-01 22:43:54 +08:00
if (!class_exists($attribute->getName()) || $this->isValidatorInterface($attribute)) {
2023-05-25 16:59:17 +08:00
continue;
}
2023-12-01 22:43:54 +08:00
$instance = $attribute->newInstance();
2023-05-25 16:59:17 +08:00
if ($class instanceof InjectProxyInterface) {
2023-12-01 22:43:54 +08:00
$instance->dispatch($filename, $class, $property->getName());
2023-05-25 16:59:17 +08:00
} else {
2023-12-01 22:43:54 +08:00
$instance->dispatch($class, $property->getName());
2023-05-25 16:59:17 +08:00
}
}
}
}
2023-12-01 22:43:54 +08:00
/**
* @param ReflectionAttribute $attribute
* @return bool
*/
protected function isValidatorInterface(ReflectionAttribute $attribute): bool
{
return in_array(ValidatorInterface::class, class_implements($attribute->getName()));
}
2023-05-25 16:59:17 +08:00
/**
* @param string $className
* @param string $method
* @return ReflectionMethod
2023-12-12 15:35:35 +08:00
* @throws
2023-05-25 16:59:17 +08:00
*/
public function getMethod(string $className, string $method): ReflectionMethod
{
$reflection = $this->getReflectionClass($className);
return $reflection->getMethod($method);
}
/**
* @param string $className
* @return ReflectionMethod[]
2023-12-12 15:35:35 +08:00
* @throws
2023-05-25 16:59:17 +08:00
*/
public function getMethods(string $className): array
{
$reflection = $this->getReflectionClass($className);
return $reflection->getMethods();
}
/**
* @param ReflectionMethod $parameters
* @return array
2023-12-12 15:35:35 +08:00
* @throws
2023-05-25 16:59:17 +08:00
*/
public function getMethodParams(ReflectionMethod $parameters): array
{
2023-11-16 21:13:16 +08:00
$className = $parameters->getDeclaringClass()->getName();
2023-05-25 16:59:17 +08:00
$methodName = $parameters->getName();
2023-12-02 11:28:19 +08:00
if (!isset($this->_parameters[$className])) $this->_parameters[$className] = [];
2023-05-25 16:59:17 +08:00
if (!isset($this->_parameters[$className][$methodName])) {
2023-12-01 22:43:54 +08:00
return $this->_parameters[$className][$methodName] = $this->resolveMethodParams($parameters);
} else {
return $this->_parameters[$className][$methodName];
2023-05-25 16:59:17 +08:00
}
}
/**
2023-11-16 21:13:16 +08:00
* @param Closure $parameters
2023-05-25 16:59:17 +08:00
* @return array
2023-12-12 15:35:35 +08:00
* @throws
2023-05-25 16:59:17 +08:00
*/
2023-11-16 21:13:16 +08:00
public function getFunctionParams(Closure $parameters): array
2023-05-25 16:59:17 +08:00
{
return $this->resolveMethodParams(new ReflectionFunction($parameters));
}
/**
* @param ReflectionMethod|ReflectionFunction $parameters
* @return array
2023-12-12 15:35:35 +08:00
* @throws
2023-05-25 16:59:17 +08:00
*/
public function resolveMethodParams(ReflectionMethod|ReflectionFunction $parameters): array
{
$params = [];
if ($parameters->getNumberOfParameters() < 1) {
return $params;
}
$parametersArray = $parameters->getParameters();
2023-12-01 22:43:54 +08:00
$class = $parameters->getDeclaringClass()->getName();
2023-05-25 16:59:17 +08:00
foreach ($parametersArray as $parameter) {
$parameterAttributes = $parameter->getAttributes();
2023-12-01 22:43:54 +08:00
$name = $parameter->getName();
if (count($parameterAttributes) > 0) {
$attribute = $parameterAttributes[0]->newInstance();
$params[$name] = $attribute->dispatch($class, $parameters->getName());
2023-05-25 16:59:17 +08:00
} else {
2023-12-01 22:43:54 +08:00
$params[$name] = $this->contractParams($parameter);
2023-05-25 16:59:17 +08:00
}
}
return $params;
}
2023-12-01 22:43:54 +08:00
/**
* @param $parameter
* @return bool|int|mixed|object|string|null
2023-12-12 15:35:35 +08:00
* @throws
2023-12-01 22:43:54 +08:00
*/
protected function contractParams($parameter): mixed
{
if ($parameter->isDefaultValueAvailable()) {
return $parameter->getDefaultValue();
}
if ($parameter->getType() === null) {
return $parameter->getType();
}
$value = $parameter->getType()->getName();
if (class_exists($value) || interface_exists($value)) {
return $this->get($value);
} else {
return $this->getTypeValue($parameter);
}
}
2023-05-25 16:59:17 +08:00
/**
* @param ReflectionParameter $parameter
* @return string|int|bool|null
2023-12-18 16:20:38 +08:00
* @throws
2023-05-25 16:59:17 +08:00
*/
private function getTypeValue(ReflectionParameter $parameter): string|int|bool|null
{
2023-12-18 16:20:38 +08:00
return match ($parameter->getType()->getName()) {
'string' => '',
'int', 'float' => 0,
'', 'mixed' => NULL,
'array' => [],
2023-12-18 16:32:47 +08:00
'object' => throw new Exception('Param type must has default value.'),
2023-12-18 16:20:38 +08:00
'bool' => false,
default => null
2023-05-25 16:59:17 +08:00
};
}
/**
* @param object $object
* @param array $config
* @return object
*/
public static function configure(object $object, array $config): object
{
foreach ($config as $key => $value) {
if (!property_exists($object, $key)) {
continue;
}
$object->{$key} = $value;
}
return $object;
}
/**
* @param string $id
* @return bool
*/
public function has(string $id): bool
{
// TODO: Implement has() method.
return isset($this->_singletons[$id]) && isset($this->_reflection[$id]);
}
2023-04-15 23:32:00 +08:00
2022-01-25 15:35:48 +08:00
}