Skip to content

Commit

Permalink
[Asset] [AssetMapper] New AssetMapper component: Map assets to public…
Browse files Browse the repository at this point in the history
…ly available, versioned paths
  • Loading branch information
weaverryan authored and fabpot committed May 1, 2023
1 parent e32cbed commit 44c7677
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 0 deletions.
28 changes: 28 additions & 0 deletions Extension/ImportMapExtension.php
@@ -0,0 +1,28 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 <kevin@dunglas.dev>
*/
final class ImportMapExtension extends AbstractExtension
{
public function getFunctions(): array
{
return [
new TwigFunction('importmap', [ImportMapRuntime::class, 'importmap'], ['is_safe' => ['html']]),
];
}
}
29 changes: 29 additions & 0 deletions Extension/ImportMapRuntime.php
@@ -0,0 +1,29 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 <kevin@dunglas.dev>
*/
class ImportMapRuntime
{
public function __construct(private readonly ImportMapRenderer $importMapRenderer)
{
}

public function importmap(?string $entryPoint = 'app'): string
{
return $this->importMapRenderer->render($entryPoint);
}
}
49 changes: 49 additions & 0 deletions Tests/Extension/ImportMapExtensionTest.php
@@ -0,0 +1,49 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 = '<script type="importmap">{ "imports": {}}</script>';
$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'));
}
}
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -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",
Expand Down

0 comments on commit 44c7677

Please sign in to comment.