From f29901bdcdf2a6481c0845e400b7662555f15ee5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Pudil?= Date: Sun, 12 Jun 2022 11:38:15 +0200 Subject: [PATCH] fully use Latte 3's Extension (#40) --- src/DI/WebpackExtension.php | 3 +-- src/Latte/WebpackExtension.php | 15 +++++++++++++++ tests/Latte/WebpackExtensionTest.phpt | 3 +-- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/DI/WebpackExtension.php b/src/DI/WebpackExtension.php index 954ecb5..1d7073a 100644 --- a/src/DI/WebpackExtension.php +++ b/src/DI/WebpackExtension.php @@ -116,10 +116,9 @@ public function loadConfiguration(): void $definition = $latteFactory->getResultDefinition(); \assert($definition instanceof ServiceDefinition); - $definition->addSetup('?->addProvider(?, ?)', ['@self', 'webpackAssetLocator', $assetLocator]); - // @phpstan-ignore-next-line latte 2 compatibility if (\version_compare(Engine::VERSION, '3', '<')) { + $definition->addSetup('?->addProvider(?, ?)', ['@self', 'webpackAssetLocator', $assetLocator]); $definition->addSetup('?->onCompile[] = function ($engine) { Contributte\Webpack\Latte\WebpackMacros::install($engine->getCompiler()); }', ['@self']); } else { $definition->addSetup('addExtension', [new Statement(\Contributte\Webpack\Latte\WebpackExtension::class)]); diff --git a/src/Latte/WebpackExtension.php b/src/Latte/WebpackExtension.php index 2205206..f2a7031 100644 --- a/src/Latte/WebpackExtension.php +++ b/src/Latte/WebpackExtension.php @@ -4,14 +4,29 @@ namespace Contributte\Webpack\Latte; +use Contributte\Webpack\AssetLocator; use Latte\Extension; final class WebpackExtension extends Extension { + private AssetLocator $assetLocator; + + public function __construct(AssetLocator $assetLocator) + { + $this->assetLocator = $assetLocator; + } + public function getTags(): array { return [ 'webpack' => [WebpackNode::class, 'create'], ]; } + + public function getProviders(): array + { + return [ + 'webpackAssetLocator' => $this->assetLocator, + ]; + } } diff --git a/tests/Latte/WebpackExtensionTest.phpt b/tests/Latte/WebpackExtensionTest.phpt index 8c2e94f..2eff020 100644 --- a/tests/Latte/WebpackExtensionTest.phpt +++ b/tests/Latte/WebpackExtensionTest.phpt @@ -38,8 +38,7 @@ final class WebpackExtensionTest extends TestCase ); $latte = new Engine(); - $latte->addProvider('webpackAssetLocator', $assetLocator); - $latte->addExtension(new WebpackExtension()); + $latte->addExtension(new WebpackExtension($assetLocator)); $latte->setLoader(new StringLoader()); Assert::same('/dist/asset.js', $latte->renderToString('{webpack asset.js}'));