Skip to content

Commit

Permalink
Add twig helper functions to check if entrypoint exists
Browse files Browse the repository at this point in the history
  • Loading branch information
acrobat committed Feb 10, 2022
1 parent 5a86aad commit f9ca828
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Asset/EntrypointLookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,9 @@ private function getEntriesData(): array

return $this->entriesData;
}

public function entryExists(string $entryName): bool
{
return isset($entriesData['entrypoints'][$entryName]);
}
}
12 changes: 12 additions & 0 deletions src/Twig/EntryFilesTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Symfony\WebpackEncoreBundle\Twig;

use Psr\Container\ContainerInterface;
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookup;
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupInterface;
use Symfony\WebpackEncoreBundle\Asset\TagRenderer;
use Twig\Extension\AbstractExtension;
Expand All @@ -31,6 +32,7 @@ public function getFunctions(): array
new TwigFunction('encore_entry_css_files', [$this, 'getWebpackCssFiles']),
new TwigFunction('encore_entry_script_tags', [$this, 'renderWebpackScriptTags'], ['is_safe' => ['html']]),
new TwigFunction('encore_entry_link_tags', [$this, 'renderWebpackLinkTags'], ['is_safe' => ['html']]),
new TwigFunction('encore_entry_exists', [$this, 'entryExists']),
];
}

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

public function entryExists(string $entryName, string $entrypointName = '_default'): bool
{
$entrypointLookup = $this->getEntrypointLookup($entrypointName);
if (!$entrypointLookup instanceof EntrypointLookup) {
throw new \LogicException(sprintf('Cannot use entryExists() unless the entrypoint lookup is an instance of "%s"', EntrypointLookup::class));
}

return $entrypointLookup->entryExists($entryName);
}

private function getEntrypointLookup(string $entrypointName): EntrypointLookupInterface
{
return $this->container->get('webpack_encore.entrypoint_lookup_collection')
Expand Down

0 comments on commit f9ca828

Please sign in to comment.