From 3a4f4017e36404b9191a18eaf89ec4893b9ec2a0 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Sat, 6 Apr 2024 17:54:06 +0200 Subject: [PATCH] Latte: CacheExtension & FormExtension are added before user extensions [Closes #320] https://forum.nette.org/cs/35525-latte-3-lze-ovlivnit-poradi-registrace-extension-v-config-neon --- src/Bridges/ApplicationDI/LatteExtension.php | 12 ++++++++++-- src/Bridges/ApplicationLatte/TemplateFactory.php | 9 --------- tests/Bridges.DI/LatteExtension.basic.phpt | 1 + 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Bridges/ApplicationDI/LatteExtension.php b/src/Bridges/ApplicationDI/LatteExtension.php index 6fffe737a..44e66888f 100644 --- a/src/Bridges/ApplicationDI/LatteExtension.php +++ b/src/Bridges/ApplicationDI/LatteExtension.php @@ -12,6 +12,7 @@ use Latte; use Nette; use Nette\Bridges\ApplicationLatte; +use Nette\DI\Definitions\Statement; use Nette\Schema\Expect; use Tracy; @@ -67,6 +68,13 @@ public function loadConfiguration(): void $latteFactory->addSetup('setStrictParsing', [$config->strictParsing]) ->addSetup('enablePhpLinter', [$config->phpLinter]); + if ($cache = $builder->getByType(Nette\Caching\Storage::class)) { + $this->addExtension(new Statement(Nette\Bridges\CacheLatte\CacheExtension::class, [$builder->getDefinition($cache)])); + } + if (class_exists(Nette\Bridges\FormsLatte\FormsExtension::class)) { + $this->addExtension(new Statement(Nette\Bridges\FormsLatte\FormsExtension::class)); + } + foreach ($config->extensions as $extension) { $this->addExtension($extension); } @@ -146,10 +154,10 @@ public function addMacro(string $macro): void } - public function addExtension(Nette\DI\Definitions\Statement|string $extension): void + public function addExtension(Statement|string $extension): void { $extension = is_string($extension) - ? new Nette\DI\Definitions\Statement($extension) + ? new Statement($extension) : $extension; $builder = $this->getContainerBuilder(); diff --git a/src/Bridges/ApplicationLatte/TemplateFactory.php b/src/Bridges/ApplicationLatte/TemplateFactory.php index 5c2381864..9fab76531 100644 --- a/src/Bridges/ApplicationLatte/TemplateFactory.php +++ b/src/Bridges/ApplicationLatte/TemplateFactory.php @@ -53,17 +53,8 @@ public function createTemplate(?UI\Control $control = null, ?string $class = nul if (version_compare(Latte\Engine::VERSION, '3', '<')) { $this->setupLatte2($latte, $control, $presenter, $template); - } else { $latte->addExtension(new UIExtension($control)); - - if ($this->cacheStorage && class_exists(Nette\Bridges\CacheLatte\CacheExtension::class)) { - $latte->addExtension(new Nette\Bridges\CacheLatte\CacheExtension($this->cacheStorage)); - } - - if (class_exists(Nette\Bridges\FormsLatte\FormsExtension::class)) { - $latte->addExtension(new Nette\Bridges\FormsLatte\FormsExtension); - } } // default parameters diff --git a/tests/Bridges.DI/LatteExtension.basic.phpt b/tests/Bridges.DI/LatteExtension.basic.phpt index 1ee2f1416..1e856d14a 100644 --- a/tests/Bridges.DI/LatteExtension.basic.phpt +++ b/tests/Bridges.DI/LatteExtension.basic.phpt @@ -66,6 +66,7 @@ $extensions = Assert::with($latte, fn() => $this->extensions); Assert::equal([ new Latte\Essential\CoreExtension, new Latte\Sandbox\SandboxExtension, + new Nette\Bridges\FormsLatte\FormsExtension, new MyExtension, new MyExtension(1), new MyExtension(2),