This commit is contained in:
2023-04-07 00:04:59 +08:00
parent 9a8d2b8bea
commit 06e2b691bb
+73 -60
View File
@@ -12,7 +12,6 @@ namespace Kiri\Di;
use Closure; use Closure;
use Exception; use Exception;
use Kiri; use Kiri;
use Kiri\Di\ContainerInterface;
use ReflectionClass; use ReflectionClass;
use ReflectionException; use ReflectionException;
use ReflectionFunction; use ReflectionFunction;
@@ -25,45 +24,45 @@ use ReflectionProperty;
*/ */
class Container implements ContainerInterface class Container implements ContainerInterface
{ {
/** /**
* @var array * @var array
* *
* instance class by className * instance class by className
*/ */
private array $_singletons = []; private array $_singletons = [];
/** /**
* @var ReflectionMethod[] * @var ReflectionMethod[]
* *
* class new instance construct parameter * class new instance construct parameter
*/ */
private array $_constructs = []; private array $_constructs = [];
/** /**
* @var array * @var array
* *
* implements \ReflectClass * implements \ReflectClass
*/ */
private array $_reflection = []; private array $_reflection = [];
/** @var array */ /** @var array */
private array $_parameters = []; private array $_parameters = [];
/** @var array|string[] */ /** @var array|string[] */
private array $_interfaces = []; private array $_interfaces = [];
private static ?ContainerInterface $container = null; private static ?ContainerInterface $container = null;
private function __construct() private function __construct()
{ {
} }
/** /**
* @param string $id * @param string $id
* @return mixed * @return mixed
@@ -76,8 +75,22 @@ class Container implements ContainerInterface
} }
return $this->make($id, [], []); return $this->make($id, [], []);
} }
/**
* @param $id
* @return mixed
* @throws Exception
*/
public function copy($id): mixed
{
if ($id == ContainerInterface::class) {
return $this;
}
return clone $this->make($id, [], []);
}
/** /**
* @return static * @return static
*/ */
@@ -88,8 +101,8 @@ class Container implements ContainerInterface
} }
return static::$container; return static::$container;
} }
/** /**
* @param $class * @param $class
* @param array $constrict * @param array $constrict
@@ -113,8 +126,8 @@ class Container implements ContainerInterface
} }
return $this->_singletons[$class]; return $this->_singletons[$class];
} }
/** /**
* @param string $interface * @param string $interface
* @param string $class * @param string $class
@@ -123,8 +136,8 @@ class Container implements ContainerInterface
{ {
$this->_interfaces[$interface] = $class; $this->_interfaces[$interface] = $class;
} }
/** /**
* @param $class * @param $class
* @return bool * @return bool
@@ -137,8 +150,8 @@ class Container implements ContainerInterface
} }
return false; return false;
} }
/** /**
* @param string $interface * @param string $interface
* @param $object * @param $object
@@ -148,13 +161,13 @@ class Container implements ContainerInterface
if (is_string($object)) { if (is_string($object)) {
$this->_interfaces[$interface] = $object; $this->_interfaces[$interface] = $object;
} else { } else {
$className = get_class($object); $className = get_class($object);
$this->_interfaces[$interface] = $className; $this->_interfaces[$interface] = $className;
$this->_singletons[$className] = $object; $this->_singletons[$className] = $object;
} }
} }
/** /**
* @param $class * @param $class
* @param array $constrict * @param array $constrict
@@ -166,8 +179,8 @@ class Container implements ContainerInterface
{ {
return $this->resolve($class, $constrict, $config); return $this->resolve($class, $constrict, $config);
} }
/** /**
* @param $class * @param $class
* @param $constrict * @param $constrict
@@ -182,15 +195,15 @@ class Container implements ContainerInterface
if (!$reflect->isInstantiable()) { if (!$reflect->isInstantiable()) {
throw new ReflectionException('Class ' . $class . ' cannot be instantiated'); throw new ReflectionException('Class ' . $class . ' cannot be instantiated');
} }
$object = $this->newInstance($reflect, $constrict); $object = $this->newInstance($reflect, $constrict);
$this->propertyInject($reflect, $object); $this->propertyInject($reflect, $object);
return $this->onAfterInit($object, $config); return $this->onAfterInit($object, $config);
} }
/** /**
* @param ReflectionClass $reflect * @param ReflectionClass $reflect
* @param $dependencies * @param $dependencies
@@ -209,8 +222,8 @@ class Container implements ContainerInterface
$parameters = $this->mergeParam($this->resolveParameters($construct), $dependencies); $parameters = $this->mergeParam($this->resolveParameters($construct), $dependencies);
return $reflect->newInstanceArgs($parameters); return $reflect->newInstanceArgs($parameters);
} }
/** /**
* @param ReflectionClass $reflect * @param ReflectionClass $reflect
* @param $object * @param $object
@@ -232,8 +245,8 @@ class Container implements ContainerInterface
} }
return $object; return $object;
} }
/** /**
* @param $className * @param $className
* @param string|null $method * @param string|null $method
@@ -244,8 +257,8 @@ class Container implements ContainerInterface
{ {
return TargetManager::get($className)->getMethodAttribute($method); return TargetManager::get($className)->getMethodAttribute($method);
} }
/** /**
* @param string $class * @param string $class
* @param string|null $property * @param string|null $property
@@ -256,8 +269,8 @@ class Container implements ContainerInterface
{ {
return TargetManager::get($class)->getProperty($property); return TargetManager::get($class)->getProperty($property);
} }
/** /**
* @param $object * @param $object
* @param $config * @param $config
@@ -271,8 +284,8 @@ class Container implements ContainerInterface
} }
return $object; return $object;
} }
/** /**
* @param $class * @param $class
* @return ReflectionClass * @return ReflectionClass
@@ -293,8 +306,8 @@ class Container implements ContainerInterface
} }
return $this->_reflection[$class] = $reflect; return $this->_reflection[$class] = $reflect;
} }
/** /**
* @param string $class * @param string $class
* @return ReflectionMethod[] * @return ReflectionMethod[]
@@ -303,8 +316,8 @@ class Container implements ContainerInterface
{ {
return TargetManager::get($class)->getMethods(); return TargetManager::get($class)->getMethods();
} }
/** /**
* @param string $class * @param string $class
* @param string $method * @param string $method
@@ -315,8 +328,8 @@ class Container implements ContainerInterface
{ {
return TargetManager::get($class)->getMethod($method); return TargetManager::get($class)->getMethod($method);
} }
/** /**
* @param string|Closure $method * @param string|Closure $method
* @param string|null $className * @param string|null $className
@@ -340,8 +353,8 @@ class Container implements ContainerInterface
} }
return $this->setParameters($className, $method, $this->resolveParameters($reflectMethod)); return $this->setParameters($className, $method, $this->resolveParameters($reflectMethod));
} }
/** /**
* @param $class * @param $class
* @param $method * @param $method
@@ -355,8 +368,8 @@ class Container implements ContainerInterface
} }
return $this->_parameters[$class][$method] = $parameters; return $this->_parameters[$class][$method] = $parameters;
} }
/** /**
* @param ReflectionMethod|ReflectionFunction $reflectionMethod * @param ReflectionMethod|ReflectionFunction $reflectionMethod
* @return array * @return array
@@ -388,8 +401,8 @@ class Container implements ContainerInterface
} }
return $params; return $params;
} }
/** /**
* @param $class * @param $class
* @return ReflectionClass|null * @return ReflectionClass|null
@@ -401,8 +414,8 @@ class Container implements ContainerInterface
} }
return $this->_reflection[$class]; return $this->_reflection[$class];
} }
/** /**
* @return $this * @return $this
*/ */
@@ -413,7 +426,7 @@ class Container implements ContainerInterface
$this->_constructs = []; $this->_constructs = [];
return $this; return $this;
} }
/** /**
* @param $old * @param $old
* @param $newParam * @param $newParam
@@ -432,7 +445,7 @@ class Container implements ContainerInterface
} }
return $old; return $old;
} }
/** /**
* @param string $id * @param string $id
* @return bool * @return bool