Skip to content

Commit

Permalink
fully use Latte 3's Extension (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
jiripudil authored Jun 12, 2022
1 parent 5bc9319 commit f29901b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
3 changes: 1 addition & 2 deletions src/DI/WebpackExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)]);
Expand Down
15 changes: 15 additions & 0 deletions src/Latte/WebpackExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
}
}
3 changes: 1 addition & 2 deletions tests/Latte/WebpackExtensionTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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}'));
Expand Down

0 comments on commit f29901b

Please sign in to comment.