From 58a8d2288dd2aae755538ccb380af183a356892d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Zl=C3=A1mal?= Date: Sun, 25 Sep 2016 11:46:02 +0200 Subject: [PATCH] TemplateFactory: added support for custom Template implementation [Closes #159][Closes #141] With this change it's possible to simply use custom Template implementation. Usage example in config.neon: latte: templateClass: App\CustomTemplateImplementation Custom template MUST extend Nette\Bridges\ApplicationLatte\Template. --- src/Bridges/ApplicationDI/LatteExtension.php | 4 +- .../ApplicationLatte/TemplateFactory.php | 11 ++- .../TemplateFactory.customTemplate.phpt | 76 +++++++++++++++++++ 3 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 tests/Bridges.Latte/TemplateFactory.customTemplate.phpt diff --git a/src/Bridges/ApplicationDI/LatteExtension.php b/src/Bridges/ApplicationDI/LatteExtension.php index b253c7180..40c7f03ef 100644 --- a/src/Bridges/ApplicationDI/LatteExtension.php +++ b/src/Bridges/ApplicationDI/LatteExtension.php @@ -19,6 +19,7 @@ class LatteExtension extends Nette\DI\CompilerExtension public $defaults = [ 'xhtml' => FALSE, 'macros' => [], + 'templateClass' => NULL, ]; /** @var bool */ @@ -54,7 +55,8 @@ public function loadConfiguration() $builder->addDefinition($this->prefix('templateFactory')) ->setClass(Nette\Application\UI\ITemplateFactory::class) - ->setFactory(Nette\Bridges\ApplicationLatte\TemplateFactory::class); + ->setFactory(Nette\Bridges\ApplicationLatte\TemplateFactory::class) + ->setArguments(['templateClass' => $config['templateClass']]); foreach ($config['macros'] as $macro) { $this->addMacro($macro); diff --git a/src/Bridges/ApplicationLatte/TemplateFactory.php b/src/Bridges/ApplicationLatte/TemplateFactory.php index e28e86674..0895552c1 100644 --- a/src/Bridges/ApplicationLatte/TemplateFactory.php +++ b/src/Bridges/ApplicationLatte/TemplateFactory.php @@ -31,14 +31,21 @@ class TemplateFactory implements UI\ITemplateFactory /** @var Nette\Caching\IStorage */ private $cacheStorage; + /** @var string */ + private $templateClass; + public function __construct(ILatteFactory $latteFactory, Nette\Http\IRequest $httpRequest = NULL, - Nette\Security\User $user = NULL, Nette\Caching\IStorage $cacheStorage = NULL) + Nette\Security\User $user = NULL, Nette\Caching\IStorage $cacheStorage = NULL, $templateClass = NULL) { $this->latteFactory = $latteFactory; $this->httpRequest = $httpRequest; $this->user = $user; $this->cacheStorage = $cacheStorage; + if ($templateClass && (!class_exists($templateClass) || !is_a($templateClass, Template::class, TRUE))) { + throw new Nette\InvalidArgumentException("Class $templateClass does not extend " . Template::class . ' or it does not exist.'); + } + $this->templateClass = $templateClass ?: Template::class; } @@ -48,7 +55,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) { diff --git a/tests/Bridges.Latte/TemplateFactory.customTemplate.phpt b/tests/Bridges.Latte/TemplateFactory.customTemplate.phpt new file mode 100644 index 000000000..39b2b831e --- /dev/null +++ b/tests/Bridges.Latte/TemplateFactory.customTemplate.phpt @@ -0,0 +1,76 @@ +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; + } +} + + +test(function () { + $factory = new TemplateFactory(new LatteFactoryMock(new Latte\Engine)); + Assert::type(Template::class, $factory->createTemplate()); +}); + +Assert::exception(function () { + $factory = new TemplateFactory(new LatteFactoryMock(new Latte\Engine), NULL, NULL, NULL, TemplateMockWithoutImplement::class); +}, \Nette\InvalidArgumentException::class, 'Class TemplateMockWithoutImplement does not extend Nette\Bridges\ApplicationLatte\Template or it does not exist.'); + + +test(function () { + $factory = new TemplateFactory(new LatteFactoryMock(new Latte\Engine), NULL, NULL, NULL, 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()); +});