Skip to content

Commit 1dcd4a8

Browse files
committed
PresenterFactory: dependency on DI container moved to new class PresenterFactoryCallback (BC break)
1 parent b29b3db commit 1dcd4a8

6 files changed

Lines changed: 79 additions & 33 deletions

File tree

src/Application/PresenterFactory.php

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,16 @@ class PresenterFactory extends Nette\Object implements IPresenterFactory
2929
/** @var array */
3030
private $cache = array();
3131

32-
/** @var Nette\DI\Container */
33-
private $container;
32+
/** @var callable */
33+
private $factory;
3434

35-
/** @var string */
36-
private $touchToRebuild;
3735

38-
39-
public function __construct(Nette\DI\Container $container, $touchToRebuild = NULL)
36+
/**
37+
* @param callable function(string $class): IPresenter
38+
*/
39+
public function __construct($factory = NULL)
4040
{
41-
$this->container = $container;
42-
$this->touchToRebuild = $touchToRebuild;
41+
$this->factory = $factory ?: function($class) { return new $class; };
4342
}
4443

4544

@@ -50,25 +49,7 @@ public function __construct(Nette\DI\Container $container, $touchToRebuild = NUL
5049
*/
5150
public function createPresenter($name)
5251
{
53-
$class = $this->getPresenterClass($name);
54-
$services = array_keys($this->container->findByTag('nette.presenter'), $class);
55-
if (count($services) > 1) {
56-
throw new InvalidPresenterException("Multiple services of type $class found: " . implode(', ', $services) . '.');
57-
58-
} elseif (!$services) {
59-
if ($this->touchToRebuild) {
60-
touch($this->touchToRebuild);
61-
}
62-
63-
$presenter = $this->container->createInstance($class);
64-
$this->container->callInjects($presenter);
65-
if ($presenter instanceof UI\Presenter && $presenter->invalidLinkMode === NULL) {
66-
$presenter->invalidLinkMode = $this->container->parameters['debugMode'] ? UI\Presenter::INVALID_LINK_WARNING : UI\Presenter::INVALID_LINK_SILENT;
67-
}
68-
return $presenter;
69-
}
70-
71-
return $this->container->createService($services[0]);
52+
return call_user_func($this->factory, $this->getPresenterClass($name));
7253
}
7354

7455

src/Bridges/ApplicationDI/ApplicationExtension.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,13 @@ public function loadConfiguration()
5656
$application->addSetup('Nette\Bridges\ApplicationTracy\RoutingPanel::initializePanel');
5757
}
5858

59-
$touchToRebuild = $this->debugMode && $config['scanDirs'] ? reset($config['scanDirs']) : NULL;
59+
$touch = $this->debugMode && $config['scanDirs'] ? reset($config['scanDirs']) : NULL; // dir added as dependency
60+
$invalidLinkMode = $this->debugMode ? UI\Presenter::INVALID_LINK_WARNING : UI\Presenter::INVALID_LINK_SILENT;
6061
$presenterFactory = $container->addDefinition($this->prefix('presenterFactory'))
6162
->setClass('Nette\Application\IPresenterFactory')
62-
->setFactory('Nette\Application\PresenterFactory', array(1 => $touchToRebuild));
63+
->setFactory('Nette\Application\PresenterFactory', array(new Nette\DI\Statement(
64+
'Nette\Bridges\ApplicationDI\PresenterFactoryCallback', array(1 => $invalidLinkMode, $touch)
65+
)));
6366

6467
if ($config['mapping']) {
6568
$presenterFactory->addSetup('setMapping', array($config['mapping']));
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Nette Framework (http://nette.org)
5+
* Copyright (c) 2004 David Grudl (http://davidgrudl.com)
6+
*/
7+
8+
namespace Nette\Bridges\ApplicationDI;
9+
10+
use Nette;
11+
12+
13+
/**
14+
* PresenterFactory callback.
15+
* @internal
16+
*/
17+
class PresenterFactoryCallback
18+
{
19+
/** @var Nette\DI\Container */
20+
private $container;
21+
22+
/** @var int */
23+
private $invalidLinkMode;
24+
25+
/** @var string|NULL */
26+
private $touchToRefresh;
27+
28+
29+
public function __construct(Nette\DI\Container $container, $invalidLinkMode, $touchToRefresh)
30+
{
31+
$this->container = $container;
32+
$this->invalidLinkMode = $invalidLinkMode;
33+
$this->touchToRefresh = $touchToRefresh;
34+
}
35+
36+
37+
/**
38+
* @return Nette\Application\IPresenter
39+
*/
40+
public function __invoke($class)
41+
{
42+
$services = array_keys($this->container->findByTag('nette.presenter'), $class);
43+
if (count($services) > 1) {
44+
throw new Nette\Application\InvalidPresenterException("Multiple services of type $class found: " . implode(', ', $services) . '.');
45+
46+
} elseif (!$services) {
47+
if ($this->touchToRefresh) {
48+
touch($this->touchToRefresh);
49+
}
50+
51+
$presenter = $this->container->createInstance($class);
52+
$this->container->callInjects($presenter);
53+
if ($presenter instanceof Nette\Application\UI\Presenter && $presenter->invalidLinkMode === NULL) {
54+
$presenter->invalidLinkMode = $this->invalidLinkMode;
55+
}
56+
return $presenter;
57+
}
58+
59+
return $this->container->createService($services[0]);
60+
}
61+
62+
}

tests/Application.Routers/LinkGenerator.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ namespace {
4242
Tester\Assert;
4343

4444

45-
$pf = new PresenterFactory(new Nette\DI\Container);
45+
$pf = new PresenterFactory;
4646

4747

4848
test(function() use ($pf) {

tests/Application/PresenterFactory.formatPresenterClass.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ require __DIR__ . '/../bootstrap.php';
1212

1313

1414
test(function() {
15-
$factory = new PresenterFactory(new Nette\DI\Container);
15+
$factory = new PresenterFactory;
1616

1717
$factory->setMapping(array(
1818
'Foo2' => 'App2\*\*Presenter',
@@ -35,7 +35,7 @@ test(function() {
3535

3636

3737
test(function() {
38-
$factory = new PresenterFactory(new Nette\DI\Container);
38+
$factory = new PresenterFactory;
3939

4040
$factory->setMapping(array(
4141
'Foo2' => 'App2\*Presenter',

tests/Application/PresenterFactory.unformatPresenterClass.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use Nette\Application\PresenterFactory,
1111
require __DIR__ . '/../bootstrap.php';
1212

1313

14-
$factory = new PresenterFactory(new Nette\DI\Container);
14+
$factory = new PresenterFactory;
1515

1616
test(function() use ($factory) {
1717
$factory->setMapping(array(

0 commit comments

Comments
 (0)