vendor/symfony/webpack-encore-bundle/src/Twig/EntryFilesTwigExtension.php line 45

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the Symfony WebpackEncoreBundle package.
  4. * (c) Fabien Potencier <fabien@symfony.com>
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. */
  8. namespace Symfony\WebpackEncoreBundle\Twig;
  9. use Psr\Container\ContainerInterface;
  10. use Symfony\WebpackEncoreBundle\Asset\EntrypointLookup;
  11. use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupInterface;
  12. use Symfony\WebpackEncoreBundle\Asset\TagRenderer;
  13. use Twig\Extension\AbstractExtension;
  14. use Twig\TwigFunction;
  15. final class EntryFilesTwigExtension extends AbstractExtension
  16. {
  17. private $container;
  18. public function __construct(ContainerInterface $container)
  19. {
  20. $this->container = $container;
  21. }
  22. public function getFunctions(): array
  23. {
  24. return [
  25. new TwigFunction('encore_entry_js_files', [$this, 'getWebpackJsFiles']),
  26. new TwigFunction('encore_entry_css_files', [$this, 'getWebpackCssFiles']),
  27. new TwigFunction('encore_entry_script_tags', [$this, 'renderWebpackScriptTags'], ['is_safe' => ['html']]),
  28. new TwigFunction('encore_entry_link_tags', [$this, 'renderWebpackLinkTags'], ['is_safe' => ['html']]),
  29. new TwigFunction('encore_entry_exists', [$this, 'entryExists']),
  30. ];
  31. }
  32. public function getWebpackJsFiles(string $entryName, string $entrypointName = '_default'): array
  33. {
  34. return $this->getEntrypointLookup($entrypointName)
  35. ->getJavaScriptFiles($entryName);
  36. }
  37. public function getWebpackCssFiles(string $entryName, string $entrypointName = '_default'): array
  38. {
  39. return $this->getEntrypointLookup($entrypointName)
  40. ->getCssFiles($entryName);
  41. }
  42. public function renderWebpackScriptTags(string $entryName, string $packageName = null, string $entrypointName = '_default', array $attributes = []): string
  43. {
  44. return $this->getTagRenderer()
  45. ->renderWebpackScriptTags($entryName, $packageName, $entrypointName, $attributes);
  46. }
  47. public function renderWebpackLinkTags(string $entryName, string $packageName = null, string $entrypointName = '_default', array $attributes = []): string
  48. {
  49. return $this->getTagRenderer()
  50. ->renderWebpackLinkTags($entryName, $packageName, $entrypointName, $attributes);
  51. }
  52. public function entryExists(string $entryName, string $entrypointName = '_default'): bool
  53. {
  54. $entrypointLookup = $this->getEntrypointLookup($entrypointName);
  55. if (!$entrypointLookup instanceof EntrypointLookup) {
  56. throw new \LogicException(sprintf('Cannot use entryExists() unless the entrypoint lookup is an instance of "%s"', EntrypointLookup::class));
  57. }
  58. return $entrypointLookup->entryExists($entryName);
  59. }
  60. private function getEntrypointLookup(string $entrypointName): EntrypointLookupInterface
  61. {
  62. return $this->container->get('webpack_encore.entrypoint_lookup_collection')
  63. ->getEntrypointLookup($entrypointName);
  64. }
  65. private function getTagRenderer(): TagRenderer
  66. {
  67. return $this->container->get('webpack_encore.tag_renderer');
  68. }
  69. }