Skip to content

Commit

Permalink
DecoratorExtension: do not decorate accessors by inner type (possible…
Browse files Browse the repository at this point in the history
… BC break)

example:

services:
	- TestControlAccessor
	- TestControl
decorator:
	TestControl:
		setup:
			- method   # will not decorate TestControlAccessor
  • Loading branch information
dg committed Mar 28, 2018
1 parent 98eb0e6 commit e78d4c1
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/DI/Extensions/DecoratorExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ public function addTags(string $type, array $tags): void
private function findByType(string $type): array
{
return array_filter($this->getContainerBuilder()->getDefinitions(), function ($def) use ($type) {
return is_a($def->getType(), $type, true) || is_a($def->getImplement(), $type, true);
return $def->getImplementMode() === $def::IMPLEMENT_MODE_GET
? is_a($def->getImplement(), $type, true)
: (is_a($def->getType(), $type, true) || is_a($def->getImplement(), $type, true));
});
}
}
49 changes: 49 additions & 0 deletions tests/DI/DecoratorExtension.factories.accessor.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/**
* Test: Nette\DI\Compiler: service decorators && generated factories
*/

declare(strict_types=1);

use Nette\DI;
use Tester\Assert;


require __DIR__ . '/../bootstrap.php';

interface FooAccessor
{

/**
* @return Foo
*/
public function get();
}

class Foo
{
}


$compiler = new DI\Compiler;
$compiler->addExtension('decorator', new Nette\DI\Extensions\DecoratorExtension);
$container = createContainer($compiler, '
decorator:
Foo:
inject: yes
FooAccessor:
tags: [a]
services:
foo: Foo
acc: {implement: FooAccessor}
');


$builder = $compiler->getContainerBuilder();

Assert::true($builder->getDefinition('foo')->getTag('inject'));
Assert::null($builder->getDefinition('foo')->getTag('a'));

Assert::null($builder->getDefinition('acc')->getTag('inject'));
Assert::true($builder->getDefinition('acc')->getTag('a'));

0 comments on commit e78d4c1

Please sign in to comment.