Skip to content

Commit a238fe7

Browse files
committed
Handle the new Entrypoint collection to render paths.
Add a new parameter to define in which entrypoint the assets must be found. Also implement a fallaback system: If no named entrypoint is given, use the default one.
1 parent 2c38514 commit a238fe7

File tree

2 files changed

+40
-18
lines changed

2 files changed

+40
-18
lines changed

src/Asset/TagRenderer.php

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,39 @@
1010
namespace Symfony\WebpackEncoreBundle\Asset;
1111

1212
use Symfony\Component\Asset\Packages;
13+
use Symfony\Component\DependencyInjection\ServiceLocator;
1314

1415
final class TagRenderer
1516
{
16-
private $entrypointLookup;
17+
private $entrypointLookupCollection;
1718

1819
private $packages;
1920

20-
public function __construct(EntrypointLookupInterface $entrypointLookup, Packages $packages)
21-
{
22-
$this->entrypointLookup = $entrypointLookup;
21+
public function __construct(
22+
$entrypointLookupCollection,
23+
Packages $packages
24+
) {
25+
if ($entrypointLookupCollection instanceof EntrypointLookupInterface) {
26+
@trigger_error(sprintf('The "$entrypointLookupCollection" argument in method "%s()" must be an instance of EntrypointLookupCollection.', __METHOD__), E_USER_DEPRECATED);
27+
28+
$this->entrypointLookupCollection = new EntrypointLookupCollection(
29+
new ServiceLocator(['_default' => function() use ($entrypointLookupCollection) {
30+
return $entrypointLookupCollection;
31+
}])
32+
);
33+
} elseif ($entrypointLookupCollection instanceof EntrypointLookupCollection) {
34+
$this->entrypointLookupCollection = $entrypointLookupCollection;
35+
} else {
36+
throw new \TypeError('The "$entrypointLookupCollection" argument must be an instance of EntrypointLookupCollection.');
37+
}
38+
2339
$this->packages = $packages;
2440
}
2541

26-
public function renderWebpackScriptTags(string $entryName, string $packageName = null): string
42+
public function renderWebpackScriptTags(string $entryName, string $packageName = null, string $entrypointName = '_default'): string
2743
{
2844
$scriptTags = [];
29-
foreach ($this->entrypointLookup->getJavaScriptFiles($entryName) as $filename) {
45+
foreach ($this->getEntrypointLookup($entrypointName)->getJavaScriptFiles($entryName) as $filename) {
3046
$scriptTags[] = sprintf(
3147
'<script src="%s"></script>',
3248
htmlentities($this->getAssetPath($filename, $packageName))
@@ -36,10 +52,10 @@ public function renderWebpackScriptTags(string $entryName, string $packageName =
3652
return implode('', $scriptTags);
3753
}
3854

39-
public function renderWebpackLinkTags(string $entryName, string $packageName = null): string
55+
public function renderWebpackLinkTags(string $entryName, string $packageName = null, string $entrypointName = '_default'): string
4056
{
4157
$scriptTags = [];
42-
foreach ($this->entrypointLookup->getCssFiles($entryName) as $filename) {
58+
foreach ($this->getEntrypointLookup($entrypointName)->getCssFiles($entryName) as $filename) {
4359
$scriptTags[] = sprintf(
4460
'<link rel="stylesheet" href="%s">',
4561
htmlentities($this->getAssetPath($filename, $packageName))
@@ -60,4 +76,9 @@ private function getAssetPath(string $assetPath, string $packageName = null): st
6076
$packageName
6177
);
6278
}
79+
80+
private function getEntrypointLookup(string $buildName): EntrypointLookupInterface
81+
{
82+
return $this->entrypointLookupCollection->getEntrypointLookup($buildName);
83+
}
6384
}

src/Twig/EntryFilesTwigExtension.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,33 +34,34 @@ public function getFunctions()
3434
];
3535
}
3636

37-
public function getWebpackJsFiles(string $entryName): array
37+
public function getWebpackJsFiles(string $entryName, string $entrypointName = '_default'): array
3838
{
39-
return $this->getEntrypointLookup()
39+
return $this->getEntrypointLookup($entrypointName)
4040
->getJavaScriptFiles($entryName);
4141
}
4242

43-
public function getWebpackCssFiles(string $entryName): array
43+
public function getWebpackCssFiles(string $entryName, string $entrypointName = '_default'): array
4444
{
45-
return $this->getEntrypointLookup()
45+
return $this->getEntrypointLookup($entrypointName)
4646
->getCssFiles($entryName);
4747
}
4848

49-
public function renderWebpackScriptTags(string $entryName, string $packageName = null): string
49+
public function renderWebpackScriptTags(string $entryName, string $packageName = null, string $entrypointName = '_default'): string
5050
{
5151
return $this->getTagRenderer()
52-
->renderWebpackScriptTags($entryName, $packageName);
52+
->renderWebpackScriptTags($entryName, $packageName, $entrypointName);
5353
}
5454

55-
public function renderWebpackLinkTags(string $entryName, string $packageName = null): string
55+
public function renderWebpackLinkTags(string $entryName, string $packageName = null, string $entrypointName = '_default'): string
5656
{
5757
return $this->getTagRenderer()
58-
->renderWebpackLinkTags($entryName, $packageName);
58+
->renderWebpackLinkTags($entryName, $packageName, $entrypointName);
5959
}
6060

61-
private function getEntrypointLookup(): EntrypointLookupInterface
61+
private function getEntrypointLookup(string $entrypointName): EntrypointLookupInterface
6262
{
63-
return $this->container->get('webpack_encore.entrypoint_lookup');
63+
return $this->container->get('webpack_encore.entrypoint_lookup_collection')
64+
->getEntrypointLookup($entrypointName);
6465
}
6566

6667
private function getTagRenderer(): TagRenderer

0 commit comments

Comments
 (0)