From 44c7677619c163fb7ee968b1d68d68c7d3a5f172 Mon Sep 17 00:00:00 2001 From: Ryan Weaver Date: Thu, 20 Apr 2023 21:00:42 -0400 Subject: [PATCH] [Asset] [AssetMapper] New AssetMapper component: Map assets to publicly available, versioned paths --- Extension/ImportMapExtension.php | 28 +++++++++++++ Extension/ImportMapRuntime.php | 29 +++++++++++++ Tests/Extension/ImportMapExtensionTest.php | 49 ++++++++++++++++++++++ composer.json | 1 + 4 files changed, 107 insertions(+) create mode 100644 Extension/ImportMapExtension.php create mode 100644 Extension/ImportMapRuntime.php create mode 100644 Tests/Extension/ImportMapExtensionTest.php diff --git a/Extension/ImportMapExtension.php b/Extension/ImportMapExtension.php new file mode 100644 index 00000000..2156c74d --- /dev/null +++ b/Extension/ImportMapExtension.php @@ -0,0 +1,28 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Twig\Extension; + +use Twig\Extension\AbstractExtension; +use Twig\TwigFunction; + +/** + * @author Kévin Dunglas + */ +final class ImportMapExtension extends AbstractExtension +{ + public function getFunctions(): array + { + return [ + new TwigFunction('importmap', [ImportMapRuntime::class, 'importmap'], ['is_safe' => ['html']]), + ]; + } +} diff --git a/Extension/ImportMapRuntime.php b/Extension/ImportMapRuntime.php new file mode 100644 index 00000000..aa5097d3 --- /dev/null +++ b/Extension/ImportMapRuntime.php @@ -0,0 +1,29 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Twig\Extension; + +use Symfony\Component\AssetMapper\ImportMap\ImportMapRenderer; + +/** + * @author Kévin Dunglas + */ +class ImportMapRuntime +{ + public function __construct(private readonly ImportMapRenderer $importMapRenderer) + { + } + + public function importmap(?string $entryPoint = 'app'): string + { + return $this->importMapRenderer->render($entryPoint); + } +} diff --git a/Tests/Extension/ImportMapExtensionTest.php b/Tests/Extension/ImportMapExtensionTest.php new file mode 100644 index 00000000..26a572e1 --- /dev/null +++ b/Tests/Extension/ImportMapExtensionTest.php @@ -0,0 +1,49 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Extension; + +use PHPUnit\Framework\TestCase; +use Symfony\Bridge\Twig\Extension\ImportMapExtension; +use Symfony\Bridge\Twig\Extension\ImportMapRuntime; +use Symfony\Component\AssetMapper\ImportMap\ImportMapRenderer; +use Twig\Environment; +use Twig\Loader\ArrayLoader; +use Twig\RuntimeLoader\RuntimeLoaderInterface; + +class ImportMapExtensionTest extends TestCase +{ + public function testItRendersTheImportmap() + { + $twig = new Environment(new ArrayLoader([ + 'template' => '{{ importmap("application") }}', + ]), ['debug' => true, 'cache' => false, 'autoescape' => 'html', 'optimizations' => 0]); + $twig->addExtension(new ImportMapExtension()); + $importMapRenderer = $this->createMock(ImportMapRenderer::class); + $expected = ''; + $importMapRenderer->expects($this->once()) + ->method('render') + ->with('application') + ->willReturn($expected); + $runtime = new ImportMapRuntime($importMapRenderer); + + $mockRuntimeLoader = $this->createMock(RuntimeLoaderInterface::class); + $mockRuntimeLoader + ->method('load') + ->willReturnMap([ + [ImportMapRuntime::class, $runtime], + ]) + ; + $twig->addRuntimeLoader($mockRuntimeLoader); + + $this->assertSame($expected, $twig->render('template')); + } +} diff --git a/composer.json b/composer.json index 8cf462fe..cac49224 100644 --- a/composer.json +++ b/composer.json @@ -26,6 +26,7 @@ "league/html-to-markdown": "^5.0", "phpdocumentor/reflection-docblock": "^3.0|^4.0|^5.0", "symfony/asset": "^5.4|^6.0", + "symfony/asset-mapper": "^6.3", "symfony/dependency-injection": "^5.4|^6.0", "symfony/finder": "^5.4|^6.0", "symfony/form": "^6.3",