Skip to content

Commit 44c7677

Browse files
weaverryanfabpot
authored andcommitted
[Asset] [AssetMapper] New AssetMapper component: Map assets to publicly available, versioned paths
1 parent e32cbed commit 44c7677

File tree

4 files changed

+107
-0
lines changed

4 files changed

+107
-0
lines changed

Extension/ImportMapExtension.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Twig\Extension;
13+
14+
use Twig\Extension\AbstractExtension;
15+
use Twig\TwigFunction;
16+
17+
/**
18+
* @author Kévin Dunglas <[email protected]>
19+
*/
20+
final class ImportMapExtension extends AbstractExtension
21+
{
22+
public function getFunctions(): array
23+
{
24+
return [
25+
new TwigFunction('importmap', [ImportMapRuntime::class, 'importmap'], ['is_safe' => ['html']]),
26+
];
27+
}
28+
}

Extension/ImportMapRuntime.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Twig\Extension;
13+
14+
use Symfony\Component\AssetMapper\ImportMap\ImportMapRenderer;
15+
16+
/**
17+
* @author Kévin Dunglas <[email protected]>
18+
*/
19+
class ImportMapRuntime
20+
{
21+
public function __construct(private readonly ImportMapRenderer $importMapRenderer)
22+
{
23+
}
24+
25+
public function importmap(?string $entryPoint = 'app'): string
26+
{
27+
return $this->importMapRenderer->render($entryPoint);
28+
}
29+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Extension;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bridge\Twig\Extension\ImportMapExtension;
16+
use Symfony\Bridge\Twig\Extension\ImportMapRuntime;
17+
use Symfony\Component\AssetMapper\ImportMap\ImportMapRenderer;
18+
use Twig\Environment;
19+
use Twig\Loader\ArrayLoader;
20+
use Twig\RuntimeLoader\RuntimeLoaderInterface;
21+
22+
class ImportMapExtensionTest extends TestCase
23+
{
24+
public function testItRendersTheImportmap()
25+
{
26+
$twig = new Environment(new ArrayLoader([
27+
'template' => '{{ importmap("application") }}',
28+
]), ['debug' => true, 'cache' => false, 'autoescape' => 'html', 'optimizations' => 0]);
29+
$twig->addExtension(new ImportMapExtension());
30+
$importMapRenderer = $this->createMock(ImportMapRenderer::class);
31+
$expected = '<script type="importmap">{ "imports": {}}</script>';
32+
$importMapRenderer->expects($this->once())
33+
->method('render')
34+
->with('application')
35+
->willReturn($expected);
36+
$runtime = new ImportMapRuntime($importMapRenderer);
37+
38+
$mockRuntimeLoader = $this->createMock(RuntimeLoaderInterface::class);
39+
$mockRuntimeLoader
40+
->method('load')
41+
->willReturnMap([
42+
[ImportMapRuntime::class, $runtime],
43+
])
44+
;
45+
$twig->addRuntimeLoader($mockRuntimeLoader);
46+
47+
$this->assertSame($expected, $twig->render('template'));
48+
}
49+
}

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"league/html-to-markdown": "^5.0",
2727
"phpdocumentor/reflection-docblock": "^3.0|^4.0|^5.0",
2828
"symfony/asset": "^5.4|^6.0",
29+
"symfony/asset-mapper": "^6.3",
2930
"symfony/dependency-injection": "^5.4|^6.0",
3031
"symfony/finder": "^5.4|^6.0",
3132
"symfony/form": "^6.3",

0 commit comments

Comments
 (0)