Skip to content

Commit

Permalink
ApplicationExtension: tag nette.presenter replaced by findByType()
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Mar 9, 2019
1 parent 87696e0 commit cb35435
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 17 deletions.
3 changes: 1 addition & 2 deletions src/Bridges/ApplicationDI/ApplicationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,7 @@ public function beforeCompile()
}

foreach ($all as $def) {
$def->addTag(Nette\DI\Extensions\InjectExtension::TAG_INJECT)
->addTag('nette.presenter', $def->getType());
$def->addTag(Nette\DI\Extensions\InjectExtension::TAG_INJECT);

if (is_subclass_of($def->getType(), UI\Presenter::class)) {
$def->addSetup('$invalidLinkMode', [$this->invalidLinkMode]);
Expand Down
2 changes: 1 addition & 1 deletion src/Bridges/ApplicationDI/PresenterFactoryCallback.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __construct(Nette\DI\Container $container, int $invalidLinkMode,

public function __invoke(string $class): Nette\Application\IPresenter
{
$services = array_keys($this->container->findByTag('nette.presenter'), $class, true);
$services = $this->container->findByType($class);
if (count($services) > 1) {
throw new Nette\Application\InvalidPresenterException("Multiple services of type $class found: " . implode(', ', $services) . '.');

Expand Down
24 changes: 10 additions & 14 deletions tests/Bridges.DI/ApplicationExtension.scan.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ test(function () {
eval($code);

$container = new Container1;
$tags = $container->findByTag('nette.presenter');
Assert::count(1, array_keys($tags, NetteModule\ErrorPresenter::class, true));
Assert::count(1, array_keys($tags, NetteModule\MicroPresenter::class, true));
Assert::count(0, array_keys($tags, Nette\Application\UI\Presenter::class, true));
Assert::count(1, $container->findByType(NetteModule\ErrorPresenter::class));
Assert::count(1, $container->findByType(NetteModule\MicroPresenter::class));
Assert::count(0, $container->findByType(Nette\Application\UI\Presenter::class));
});


Expand All @@ -46,10 +45,9 @@ test(function () {
eval($code);

$container = new Container2;
$tags = $container->findByTag('nette.presenter');
Assert::count(1, array_keys($tags, 'BasePresenter', true));
Assert::count(1, array_keys($tags, 'Presenter1', true));
Assert::count(1, array_keys($tags, 'Presenter2', true));
Assert::count(3, $container->findByType(BasePresenter::class));
Assert::count(1, $container->findByType(Presenter1::class));
Assert::count(1, $container->findByType(Presenter2::class));
});


Expand All @@ -73,11 +71,9 @@ test(function () {
eval($code);

$container = new Container3;
$tags = $container->findByTag('nette.presenter');
Assert::count(1, array_keys($tags, 'BasePresenter', true));
Assert::count(1, array_keys($tags, 'Presenter1', true));
Assert::count(1, array_keys($tags, 'Presenter2', true));
Assert::count(3, $container->findByType(BasePresenter::class));
Assert::count(1, $container->findByType(Presenter1::class));
Assert::count(1, $container->findByType(Presenter2::class));

$tmp = array_keys($tags, 'Presenter1', true);
Assert::same('test', $container->getService((string) $tmp[0])->getView());
Assert::same('test', $container->getByType(Presenter1::class)->getView());
});

0 comments on commit cb35435

Please sign in to comment.