Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Asset] [AssetMapper] New AssetMapper component: Map assets to public…
…ly available, versioned paths
- Loading branch information
1 parent
e32cbed
commit 44c7677
Showing
4 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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']]), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters