Skip to content

Commit 34d3e47

Browse files
committed
Resolver: fixed self-dependency [Closes #240]
1 parent 59068e3 commit 34d3e47

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/DI/Resolver.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,15 @@ public function getByType(string $type): Reference
365365
) {
366366
return new Reference(Reference::SELF);
367367
}
368-
return new Reference($this->builder->getByType($type, true));
368+
369+
$name = $this->builder->getByType($type, true);
370+
if (
371+
!$this->currentServiceAllowed
372+
&& $this->currentService === $this->builder->getDefinition($name)
373+
) {
374+
throw new MissingServiceException;
375+
}
376+
return new Reference($name);
369377
}
370378

371379

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\DI\ContainerBuilder and self-dependency.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
use Nette\DI;
10+
use Tester\Assert;
11+
12+
13+
require __DIR__ . '/../bootstrap.php';
14+
15+
16+
class Foo
17+
{
18+
public function __construct(self $foo)
19+
{
20+
}
21+
}
22+
23+
24+
$builder = new DI\ContainerBuilder;
25+
$builder->addDefinition(null)
26+
->setFactory(Foo::class);
27+
28+
Assert::exception(function () use ($builder) {
29+
createContainer($builder);
30+
}, Nette\DI\ServiceCreationException::class, 'Service of type Foo: Service of type Foo needed by $foo in __construct() not found. Did you register it in configuration file?');

0 commit comments

Comments
 (0)