-
-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #66 from dg/pull-invalid
PresenterFactory as callback & changed invalid link handling [WIP]
- Loading branch information
Showing
9 changed files
with
225 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the Nette Framework (http://nette.org) | ||
* Copyright (c) 2004 David Grudl (http://davidgrudl.com) | ||
*/ | ||
|
||
namespace Nette\Bridges\ApplicationDI; | ||
|
||
use Nette; | ||
|
||
|
||
/** | ||
* PresenterFactory callback. | ||
* @internal | ||
*/ | ||
class PresenterFactoryCallback | ||
{ | ||
/** @var Nette\DI\Container */ | ||
private $container; | ||
|
||
/** @var int */ | ||
private $invalidLinkMode; | ||
|
||
/** @var string|NULL */ | ||
private $touchToRefresh; | ||
|
||
|
||
public function __construct(Nette\DI\Container $container, $invalidLinkMode, $touchToRefresh) | ||
{ | ||
$this->container = $container; | ||
$this->invalidLinkMode = $invalidLinkMode; | ||
$this->touchToRefresh = $touchToRefresh; | ||
} | ||
|
||
|
||
/** | ||
* @return Nette\Application\IPresenter | ||
*/ | ||
public function __invoke($class) | ||
{ | ||
$services = array_keys($this->container->findByTag('nette.presenter'), $class); | ||
if (count($services) > 1) { | ||
throw new Nette\Application\InvalidPresenterException("Multiple services of type $class found: " . implode(', ', $services) . '.'); | ||
|
||
} elseif (!$services) { | ||
if ($this->touchToRefresh) { | ||
touch($this->touchToRefresh); | ||
} | ||
|
||
$presenter = $this->container->createInstance($class); | ||
$this->container->callInjects($presenter); | ||
if ($presenter instanceof Nette\Application\UI\Presenter && $presenter->invalidLinkMode === NULL) { | ||
$presenter->invalidLinkMode = $this->invalidLinkMode; | ||
} | ||
return $presenter; | ||
} | ||
|
||
return $this->container->createService($services[0]); | ||
} | ||
|
||
} |
110 changes: 110 additions & 0 deletions
110
tests/Application.DI/ApplicationExtension.invalidLink.phpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
<?php | ||
|
||
/** | ||
* Test: ApplicationExtension | ||
*/ | ||
|
||
use Nette\DI, | ||
Nette\Bridges\ApplicationDI\ApplicationExtension, | ||
Nette\Application\UI\Presenter, | ||
Tester\Assert; | ||
|
||
|
||
require __DIR__ . '/../bootstrap.php'; | ||
require __DIR__ . '/files/MyPresenter.php'; | ||
|
||
|
||
function createCompiler($config) | ||
{ | ||
$compiler = new DI\Compiler; | ||
$compiler->loadConfig(Tester\FileMock::create($config, 'neon')); | ||
$builder = $compiler->getContainerBuilder(); | ||
$builder->addDefinition('myRouter')->setClass('Nette\Application\Routers\SimpleRouter'); | ||
$builder->addDefinition('myHttpRequest')->setFactory('Nette\Http\Request', array(new DI\Statement('Nette\Http\UrlScript'))); | ||
$builder->addDefinition('myHttpResponse')->setClass('Nette\Http\Response'); | ||
return $compiler; | ||
} | ||
|
||
|
||
test(function() { | ||
$compiler = createCompiler(' | ||
application: | ||
debugger: no | ||
silentLinks: yes | ||
services: | ||
presenter: Presenter1 | ||
'); | ||
$compiler->addExtension('application', new ApplicationExtension(TRUE)); | ||
$code = $compiler->compile(NULL, 'Container4'); | ||
eval($code); | ||
|
||
$container = new Container4; | ||
Assert::same( | ||
Presenter::INVALID_LINK_TEXTUAL, | ||
$container->getService('presenter')->invalidLinkMode | ||
); | ||
}); | ||
|
||
|
||
test(function() { | ||
$compiler = createCompiler(' | ||
application: | ||
debugger: no | ||
silentLinks: no | ||
services: | ||
presenter: Presenter1 | ||
'); | ||
$compiler->addExtension('application', new ApplicationExtension(TRUE)); | ||
$code = $compiler->compile(NULL, 'Container5'); | ||
eval($code); | ||
|
||
$container = new Container5; | ||
Assert::same( | ||
Presenter::INVALID_LINK_WARNING | Presenter::INVALID_LINK_TEXTUAL, | ||
$container->getService('presenter')->invalidLinkMode | ||
); | ||
}); | ||
|
||
|
||
test(function() { | ||
$compiler = createCompiler(' | ||
application: | ||
debugger: no | ||
silentLinks: yes | ||
services: | ||
presenter: Presenter1 | ||
'); | ||
$compiler->addExtension('application', new ApplicationExtension(FALSE)); | ||
$code = $compiler->compile(NULL, 'Container6'); | ||
eval($code); | ||
|
||
$container = new Container6; | ||
Assert::same( | ||
Presenter::INVALID_LINK_WARNING, | ||
$container->getService('presenter')->invalidLinkMode | ||
); | ||
}); | ||
|
||
|
||
test(function() { | ||
$compiler = createCompiler(' | ||
application: | ||
debugger: no | ||
silentLinks: no | ||
services: | ||
presenter: Presenter1 | ||
'); | ||
$compiler->addExtension('application', new ApplicationExtension(FALSE)); | ||
$code = $compiler->compile(NULL, 'Container7'); | ||
eval($code); | ||
|
||
$container = new Container7; | ||
Assert::same( | ||
Presenter::INVALID_LINK_WARNING, | ||
$container->getService('presenter')->invalidLinkMode | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters