From 4a2d940fb65dee32a647f58be95d67eedc901424 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Tue, 12 Jan 2021 12:14:44 +0100 Subject: [PATCH] refactoring --- src/Application/Application.php | 2 +- src/Application/UI/Presenter.php | 4 +-- src/Bridges/ApplicationDI/LatteExtension.php | 27 ++++++++++---------- 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/Application/Application.php b/src/Application/Application.php index 6e3351c7f..41c851aa3 100644 --- a/src/Application/Application.php +++ b/src/Application/Application.php @@ -179,7 +179,7 @@ public function processException(\Throwable $e): void $this->httpResponse->setCode($e instanceof BadRequestException ? ($e->getHttpCode() ?: 404) : 500); } - $args = ['exception' => $e, 'request' => end($this->requests) ?: null]; + $args = ['exception' => $e, 'request' => Arrays::last($this->requests) ?: null]; if ($this->presenter instanceof UI\Presenter) { try { $this->presenter->forward(":$this->errorPresenter:", $args); diff --git a/src/Application/UI/Presenter.php b/src/Application/UI/Presenter.php index 1261b0070..34b5a85c3 100644 --- a/src/Application/UI/Presenter.php +++ b/src/Application/UI/Presenter.php @@ -463,7 +463,7 @@ public function sendTemplate(Template $template = null): void } if (!$template->getFile()) { - $file = strtr(reset($files), '/', DIRECTORY_SEPARATOR); + $file = strtr(Arrays::first($files), '/', DIRECTORY_SEPARATOR); $this->error("Page not found. Missing template '$file'."); } } @@ -489,7 +489,7 @@ public function findLayoutTemplateFile(): ?string } if ($this->layout) { - $file = strtr(reset($files), '/', DIRECTORY_SEPARATOR); + $file = strtr(Arrays::first($files), '/', DIRECTORY_SEPARATOR); throw new Nette\FileNotFoundException("Layout not found. Missing template '$file'."); } return null; diff --git a/src/Bridges/ApplicationDI/LatteExtension.php b/src/Bridges/ApplicationDI/LatteExtension.php index cc3b1034a..68877aaf6 100644 --- a/src/Bridges/ApplicationDI/LatteExtension.php +++ b/src/Bridges/ApplicationDI/LatteExtension.php @@ -11,6 +11,8 @@ use Latte; use Nette; +use Nette\Bridges\ApplicationLatte; +use Nette\Schema\Expect; /** @@ -29,20 +31,17 @@ public function __construct(string $tempDir, bool $debugMode = false) { $this->tempDir = $tempDir; $this->debugMode = $debugMode; + } - $this->config = new class { - /** @var bool */ - public $xhtml = false; - - /** @var string[] */ - public $macros = []; - - /** @var ?string */ - public $templateClass; - /** @var bool */ - public $strictTypes = false; - }; + public function getConfigSchema(): Nette\Schema\Schema + { + return Expect::structure([ + 'xhtml' => Expect::bool(false), + 'macros' => Expect::arrayOf('string'), + 'templateClass' => Expect::string(), + 'strictTypes' => Expect::bool(false), + ]); } @@ -56,7 +55,7 @@ public function loadConfiguration() $builder = $this->getContainerBuilder(); $latteFactory = $builder->addFactoryDefinition($this->prefix('latteFactory')) - ->setImplement(Nette\Bridges\ApplicationLatte\LatteFactory::class) + ->setImplement(ApplicationLatte\LatteFactory::class) ->getResultDefinition() ->setFactory(Latte\Engine::class) ->addSetup('setTempDirectory', [$this->tempDir]) @@ -70,7 +69,7 @@ public function loadConfiguration() $builder->addDefinition($this->prefix('templateFactory')) ->setType(Nette\Application\UI\TemplateFactory::class) - ->setFactory(Nette\Bridges\ApplicationLatte\TemplateFactory::class) + ->setFactory(ApplicationLatte\TemplateFactory::class) ->setArguments(['templateClass' => $config->templateClass]); foreach ($config->macros as $macro) {