Skip to content

Commit

Permalink
Added method updating container config
Browse files Browse the repository at this point in the history
  • Loading branch information
radek-ziemniewicz committed Jan 13, 2022
1 parent 897ace4 commit fad2eac
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 40 deletions.
50 changes: 10 additions & 40 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -96,8 +80,6 @@ public function has($id): bool

/**
* Method to create a new instance.
*
* @param $id
*/
private function createNewInstance(string $id): void
{
Expand All @@ -110,20 +92,21 @@ 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;
}

/**
* Gets a class name for the given interface from the configuration.
*
* @param string $interface
*
* @return string
*/
public function getInstantiableClassForInterface(string $interface): string
{
Expand All @@ -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
{
Expand All @@ -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)) {
Expand Down Expand Up @@ -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];
Expand All @@ -220,9 +188,6 @@ private function getPotentialClassFactory(string $id): ?string
return null;
}

/**
* @return Container
*/
public static function getInstance(): Container
{
if (!isset(Container::$instance)) {
Expand All @@ -241,4 +206,9 @@ public function getValue(string $id): object
{
return $this->config[$id];
}

public function addToConfig(array $config = []): void
{
$this->config += $config;
}
}
10 changes: 10 additions & 0 deletions tests/Mocks/Config/MockClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Quillstack\DI\Tests\Mocks\Config;

class MockClass
{
//
}
10 changes: 10 additions & 0 deletions tests/Mocks/Config/MockInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Quillstack\DI\Tests\Mocks\Config;

interface MockInterface
{
//
}
40 changes: 40 additions & 0 deletions tests/Unit/TestAddToConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Quillstack\DI\Tests\Unit;

use Quillstack\DI\Container;
use Quillstack\DI\Exceptions\InterfaceDefinitionNotFoundException;
use Quillstack\DI\Tests\Mocks\Config\MockClass;
use Quillstack\DI\Tests\Mocks\Config\MockInterface;
use Quillstack\UnitTests\AssertEqual;
use Quillstack\UnitTests\AssertExceptions;

class TestAddToConfig
{
private Container $container;

public function __construct(private AssertExceptions $assertExceptions, private AssertEqual $assertEqual)
{
$this->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());
}
}
2 changes: 2 additions & 0 deletions tests/unit.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];

0 comments on commit fad2eac

Please sign in to comment.