-
-
Notifications
You must be signed in to change notification settings - Fork 116
TemplateFactory: add support for custom Template implementation #159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,6 +31,9 @@ class TemplateFactory implements UI\ITemplateFactory | |
| /** @var Nette\Caching\IStorage */ | ||
| private $cacheStorage; | ||
|
|
||
| /** @var string */ | ||
| private $templateClass = Template::class; | ||
|
|
||
|
|
||
| public function __construct(ILatteFactory $latteFactory, Nette\Http\IRequest $httpRequest = NULL, | ||
| Nette\Security\User $user = NULL, Nette\Caching\IStorage $cacheStorage = NULL) | ||
|
|
@@ -48,7 +51,7 @@ public function __construct(ILatteFactory $latteFactory, Nette\Http\IRequest $ht | |
| public function createTemplate(UI\Control $control = NULL) | ||
| { | ||
| $latte = $this->latteFactory->create(); | ||
| $template = new Template($latte); | ||
| $template = new $this->templateClass($latte); | ||
| $presenter = $control ? $control->getPresenter(FALSE) : NULL; | ||
|
|
||
| if ($control instanceof UI\Presenter) { | ||
|
|
@@ -112,4 +115,12 @@ public function createTemplate(UI\Control $control = NULL) | |
| return $template; | ||
| } | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing empty line |
||
| public function setTemplateClass($templateClass) | ||
| { | ||
| if (!class_exists($templateClass) || !is_a($templateClass, Template::class, TRUE)) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMHO |
||
| throw new Nette\InvalidArgumentException("Class $templateClass does not extend " . Template::class . ' or it does not exist.'); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. without |
||
| } | ||
| $this->templateClass = $templateClass; | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * Test: TemplateFactory custom template | ||
| */ | ||
|
|
||
| use Nette\Application\UI; | ||
| use Nette\Bridges\ApplicationLatte\Template; | ||
| use Nette\Bridges\ApplicationLatte\TemplateFactory; | ||
| use Tester\Assert; | ||
|
|
||
|
|
||
| require __DIR__ . '/../bootstrap.php'; | ||
|
|
||
|
|
||
| class LatteFactoryMock implements Nette\Bridges\ApplicationLatte\ILatteFactory | ||
| { | ||
| private $engine; | ||
|
|
||
| public function __construct(Latte\Engine $engine) | ||
| { | ||
| $this->engine = $engine; | ||
| } | ||
|
|
||
| public function create() | ||
| { | ||
| return $this->engine; | ||
| } | ||
| } | ||
|
|
||
| class TemplateMockWithoutImplement | ||
| { | ||
|
|
||
| } | ||
|
|
||
| class TemplateMock extends Nette\Bridges\ApplicationLatte\Template | ||
| { | ||
| private $file = 'ko'; | ||
|
|
||
| public function render() | ||
| { | ||
| return strrev($this->file); | ||
| } | ||
|
|
||
| public function setFile($file) | ||
| { | ||
| $this->file = $file; | ||
| } | ||
|
|
||
| public function getFile() | ||
| { | ||
| return $this->file; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| $factory = new TemplateFactory(new LatteFactoryMock(new Latte\Engine)); | ||
| Assert::type(Template::class, $factory->createTemplate()); | ||
|
|
||
|
|
||
| Assert::exception(function () use ($factory) { | ||
| $factory->setTemplateClass(TemplateMockWithoutImplement::class); | ||
| }, \Nette\InvalidArgumentException::class, 'Class TemplateMockWithoutImplement does not extend Nette\Bridges\ApplicationLatte\Template or it does not exist.'); | ||
|
|
||
|
|
||
| $factory->setTemplateClass(TemplateMock::class); | ||
| $template = $factory->createTemplate(); | ||
| Assert::type(TemplateMock::class, $template); | ||
| Assert::type(UI\ITemplate::class, $template); | ||
| Assert::same([], $template->flashes); | ||
| Assert::same('ok', $template->render()); | ||
| $template->setFile('bla'); | ||
| Assert::same('alb', $template->render()); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Awesome test, thank you. |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not simply
$templateFactory->addSetup('setTemplateClass', [$config['templateClass']]);?