Skip to content
Permalink
Browse files

InjectExtension: scans resolved class [Closes #218]

  • Loading branch information...
dg committed Oct 22, 2019
1 parent c01053f commit bf3701e24a5eedc34239dcfd3a07aed6a4e42d20
Showing with 52 additions and 2 deletions.
  1. +3 −2 src/DI/Extensions/InjectExtension.php
  2. +49 −0 tests/DI/InjectExtension.interface.phpt
@@ -44,7 +44,8 @@ public function beforeCompile()
private function updateDefinition(Definitions\ServiceDefinition $def): void
{
$class = $def->getType();
$resolver = new DI\Resolver($this->getContainerBuilder());
$class = $resolver->resolveEntityType($def->getFactory()) ?: $def->getType();
$setups = $def->getSetup();
foreach (self::getInjectProperties($class) as $property => $type) {
@@ -61,7 +62,7 @@ private function updateDefinition(Definitions\ServiceDefinition $def): void
array_unshift($setups, $inject);
}
foreach (array_reverse(self::getInjectMethods($def->getType())) as $method) {
foreach (array_reverse(self::getInjectMethods($class)) as $method) {
$inject = new Definitions\Statement($method);
foreach ($setups as $key => $setup) {
if ($setup->getEntity() === $inject->getEntity()) {
@@ -0,0 +1,49 @@
<?php
/**
* Test: Nette\DI\Compiler: inject.
*/
declare(strict_types=1);
use Nette\DI;
use Nette\DI\Definitions\Reference;
use Nette\DI\Definitions\Statement;
use Tester\Assert;
require __DIR__ . '/../bootstrap.php';
interface IService
{
}
class Service implements IService
{
public $foo;
public function injectFoo(stdClass $foo)
{
$this->foo = $foo;
}
}
$compiler = new DI\Compiler;
$compiler->addExtension('inject', new Nette\DI\Extensions\InjectExtension);
$container = createContainer($compiler, '
services:
one:
type: IService
factory: Service
inject: true
');
$builder = $compiler->getContainerBuilder();
Assert::equal([
new Statement([new Reference('self'), 'injectFoo']),
], $builder->getDefinition('one')->getSetup());

0 comments on commit bf3701e

Please sign in to comment.
You can’t perform that action at this time.