Skip to content

Commit f9ca828

Browse files
committed
Add twig helper functions to check if entrypoint exists
1 parent 5a86aad commit f9ca828

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/Asset/EntrypointLookup.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,9 @@ private function getEntriesData(): array
140140

141141
return $this->entriesData;
142142
}
143+
144+
public function entryExists(string $entryName): bool
145+
{
146+
return isset($entriesData['entrypoints'][$entryName]);
147+
}
143148
}

src/Twig/EntryFilesTwigExtension.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace Symfony\WebpackEncoreBundle\Twig;
1111

1212
use Psr\Container\ContainerInterface;
13+
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookup;
1314
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupInterface;
1415
use Symfony\WebpackEncoreBundle\Asset\TagRenderer;
1516
use Twig\Extension\AbstractExtension;
@@ -31,6 +32,7 @@ public function getFunctions(): array
3132
new TwigFunction('encore_entry_css_files', [$this, 'getWebpackCssFiles']),
3233
new TwigFunction('encore_entry_script_tags', [$this, 'renderWebpackScriptTags'], ['is_safe' => ['html']]),
3334
new TwigFunction('encore_entry_link_tags', [$this, 'renderWebpackLinkTags'], ['is_safe' => ['html']]),
35+
new TwigFunction('encore_entry_exists', [$this, 'entryExists']),
3436
];
3537
}
3638

@@ -58,6 +60,16 @@ public function renderWebpackLinkTags(string $entryName, string $packageName = n
5860
->renderWebpackLinkTags($entryName, $packageName, $entrypointName, $attributes);
5961
}
6062

63+
public function entryExists(string $entryName, string $entrypointName = '_default'): bool
64+
{
65+
$entrypointLookup = $this->getEntrypointLookup($entrypointName);
66+
if (!$entrypointLookup instanceof EntrypointLookup) {
67+
throw new \LogicException(sprintf('Cannot use entryExists() unless the entrypoint lookup is an instance of "%s"', EntrypointLookup::class));
68+
}
69+
70+
return $entrypointLookup->entryExists($entryName);
71+
}
72+
6173
private function getEntrypointLookup(string $entrypointName): EntrypointLookupInterface
6274
{
6375
return $this->container->get('webpack_encore.entrypoint_lookup_collection')

0 commit comments

Comments
 (0)