diff --git a/src/Container.php b/src/Container.php index 614c331..c2d27a6 100644 --- a/src/Container.php +++ b/src/Container.php @@ -22,42 +22,26 @@ class Container implements ContainerInterface { /** * Instances array. - * - * @var array */ private array $instances = []; /** * Current classes stack to detect loops. - * - * @var array */ private array $stack = []; /** * Instance factory to create new instances. - * - * @var InstanceFactory */ private InstanceFactory $instanceFactory; /** * Configuration for interfaces and parameters. - * - * @var array */ private array $config; - /** - * @var Container - */ private static Container $instance; - /** - * Container constructor. - * - * @param array $config - */ public function __construct(array $config = []) { $this->config = $config; @@ -96,8 +80,6 @@ public function has($id): bool /** * Method to create a new instance. - * - * @param $id */ private function createNewInstance(string $id): void { @@ -110,9 +92,14 @@ private function createNewInstance(string $id): void try { $this->instances[$id] = $this->instanceFactory->create($id); } catch (ReflectionException $exception) { + array_pop($this->stack); $message = "Unable to create reflection class for `{$id}`"; throw new UnableToCreateReflectionClassException($message, 500, $exception); + } catch (\Exception $exception) { + array_pop($this->stack); + + throw $exception; } Container::$instance = $this; @@ -120,10 +107,6 @@ private function createNewInstance(string $id): void /** * Gets a class name for the given interface from the configuration. - * - * @param string $interface - * - * @return string */ public function getInstantiableClassForInterface(string $interface): string { @@ -146,11 +129,6 @@ public function getInstantiableClassForInterface(string $interface): string /** * Gets a parameter value for the given name from the configuration. - * - * @param string $className - * @param string $parameterName - * - * @return mixed */ public function getParameterForClass(string $className, string $parameterName): mixed { @@ -161,11 +139,6 @@ public function getParameterForClass(string $className, string $parameterName): return $this->config[$className][$parameterName]; } - /** - * @param string $classNameOrInterface - * - * @return string|null - */ public function getCustomFactoryClassName(string $classNameOrInterface): ?string { if (!class_exists($classNameOrInterface) && !interface_exists($classNameOrInterface)) { @@ -196,11 +169,6 @@ public function getCustomFactoryClassName(string $classNameOrInterface): ?string return null; } - /** - * @param string $id - * - * @return string|null - */ private function getPotentialClassFactory(string $id): ?string { $class = $this->config[$id]; @@ -220,9 +188,6 @@ private function getPotentialClassFactory(string $id): ?string return null; } - /** - * @return Container - */ public static function getInstance(): Container { if (!isset(Container::$instance)) { @@ -241,4 +206,9 @@ public function getValue(string $id): object { return $this->config[$id]; } + + public function addToConfig(array $config = []): void + { + $this->config += $config; + } } diff --git a/tests/Mocks/Config/MockClass.php b/tests/Mocks/Config/MockClass.php new file mode 100644 index 0000000..f4b6f2f --- /dev/null +++ b/tests/Mocks/Config/MockClass.php @@ -0,0 +1,10 @@ +container = new Container(); + } + + public function expectNoInterfaceInConfig() + { + $this->assertExceptions->expect(InterfaceDefinitionNotFoundException::class); + + $this->container->get(MockInterface::class); + } + + public function addToConfig() + { + $this->container->addToConfig([ + MockInterface::class => MockClass::class, + ]); + + $mockClass = $this->container->get(MockInterface::class); + + $this->assertEqual->equal($mockClass, new MockClass()); + } +} diff --git a/tests/unit.php b/tests/unit.php index 1f563b0..2dd312e 100644 --- a/tests/unit.php +++ b/tests/unit.php @@ -10,4 +10,6 @@ \Quillstack\DI\Tests\Unit\InstanceFactories\TestClassFromInterfaceFactory::class, \Quillstack\DI\Tests\Unit\InstanceFactories\TestExternalInstanceFactory::class, \Quillstack\DI\Tests\Unit\InstanceFactories\TestInstantiableClassFactory::class, + + \Quillstack\DI\Tests\Unit\TestAddToConfig::class, ];