Skip to content

Commit

Permalink
Merge pull request #317 from yiisoft/fix-delegated-container
Browse files Browse the repository at this point in the history
Fix delegated container
  • Loading branch information
xepozz committed Dec 2, 2022
2 parents 8a0d3e2 + 80db7b3 commit f184eb1
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Expand Up @@ -2,7 +2,7 @@

## 1.2.1 under development

- no changes in this release.
- Bug #317: Fix delegated container (@xepozz)

## 1.2.0 November 05, 2022

Expand Down
4 changes: 3 additions & 1 deletion src/Container.php
Expand Up @@ -247,6 +247,8 @@ private function addDefinitions(array $config): void
private function setDelegates(array $delegates): void
{
$this->delegates = new CompositeContainer();
$container = $this->get(ContainerInterface::class);

foreach ($delegates as $delegate) {
if (!$delegate instanceof Closure) {
throw new InvalidConfigException(
Expand All @@ -255,7 +257,7 @@ private function setDelegates(array $delegates): void
}

/** @var ContainerInterface */
$delegate = $delegate($this);
$delegate = $delegate($container);

if (!$delegate instanceof ContainerInterface) {
throw new InvalidConfigException(
Expand Down
53 changes: 53 additions & 0 deletions tests/Unit/ContainerTest.php
Expand Up @@ -1235,6 +1235,34 @@ static function (ContainerInterface $container) {
$this->assertSame(42, $engine->getNumber());
}

public function testNewContainerDefinitionInDelegates(): void
{
$firstContainer = null;
$secondContainer = null;

$config = ContainerConfig::create()
->withDefinitions([
ContainerInterface::class => new Container(ContainerConfig::create()),
])
->withDelegates([
function (ContainerInterface $container) use (&$firstContainer): ContainerInterface {
$firstContainer = $container;
return new Container(ContainerConfig::create());
},
function (ContainerInterface $container) use (&$secondContainer): ContainerInterface {
$secondContainer = $container;
return new Container(ContainerConfig::create());
},
]);
$originalContainer = new Container($config);

$container = $originalContainer->get(ContainerInterface::class);

$this->assertNotSame($container, $originalContainer);
$this->assertSame($container, $firstContainer);
$this->assertSame($container, $secondContainer);
}

public function testResetterInDelegatesWithCustomResetter(): void
{
$config = ContainerConfig::create()
Expand Down Expand Up @@ -1593,6 +1621,31 @@ public function getExtensions(): array
$container->get(B::class);
}

public function testDifferentContainerWithProviders(): void
{
$provider = new class () implements ServiceProviderInterface {
public function getDefinitions(): array
{
return [
ContainerInterface::class => static fn (ContainerInterface $container) => new Container(ContainerConfig::create()),
];
}

public function getExtensions(): array
{
return [];
}
};

$config = ContainerConfig::create()
->withProviders([$provider]);
$originalContainer = new Container($config);

$container = $originalContainer->get(ContainerInterface::class);

$this->assertNotSame($originalContainer, $container);
}

public function testErrorOnMethodTypo(): void
{
$this->expectException(InvalidConfigException::class);
Expand Down

0 comments on commit f184eb1

Please sign in to comment.