vendor/pupax/keen-symfony-bundle/src/Twig/TwigExtension.php line 75

Open in your IDE?
  1. <?php
  2. /**
  3.  * This file is part of One AM Keen Symfony Bundle.
  4.  * (c) 2021 One AM SRL
  5.  */
  6. namespace Keen\Twig;
  7. use Keen\Event\Listener\BreadcrumbAnnotationListener;
  8. use Keen\Event\Listener\NavbarTagAnnotationListener;
  9. use Symfony\Component\HttpFoundation\RequestStack;
  10. use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupInterface;
  11. use Twig\Environment;
  12. use Twig\Extension\AbstractExtension;
  13. use Twig\TwigFunction;
  14. class TwigExtension extends AbstractExtension
  15. {
  16.     /** @var RequestStack */
  17.     private $requestStack;
  18.     /** @var BreadcrumbAnnotationListener */
  19.     private $breadcrumbAnnotationListener;
  20.     /** @var NavbarTagAnnotationListener */
  21.     private $navbarTagAnnotationListener;
  22.     /** @var EntrypointLookupInterface */
  23.     private $entrypointLookup;
  24.     /** @var string */
  25.     private $publicDir;
  26.     public function __construct(
  27.         RequestStack $requestStack,
  28.         BreadcrumbAnnotationListener $breadcrumbAnnotationListener,
  29.         NavbarTagAnnotationListener $navbarTagAnnotationListener,
  30.         EntrypointLookupInterface $entrypointLookup,
  31.         string $publicDir
  32.     ) {
  33.         $this->requestStack $requestStack;
  34.         $this->breadcrumbAnnotationListener $breadcrumbAnnotationListener;
  35.         $this->navbarTagAnnotationListener $navbarTagAnnotationListener;
  36.         $this->entrypointLookup $entrypointLookup;
  37.         $this->publicDir $publicDir;
  38.     }
  39.     public function getFunctions()
  40.     {
  41.         return [
  42.             new TwigFunction('breadcrumb', [$this'breadcrumb'], [
  43.                 'needs_environment' => true,
  44.                 'needs_context' => true,
  45.                 'is_safe' => ['html'],
  46.             ]),
  47.             new TwigFunction('pageTitle', [$this'pageTitle']),
  48.             new TwigFunction('isActive', [$this'isActive']),
  49.             new TwigFunction('encore_entry_css_source', [$this'encoreEntryCssSource'], [
  50.                 'is_safe' => ['all'],
  51.             ]),
  52.         ];
  53.     }
  54.     public function breadcrumb(Environment $environment$context$additionalVariables = [])
  55.     {
  56.         $request $this->requestStack->getCurrentRequest();
  57.         if (null === $request) {
  58.             return null;
  59.         }
  60.         return $environment->render('@Keen/breadcrumb.html.twig'array_merge($context, [
  61.             'items' => $this->breadcrumbAnnotationListener->getBreadcrumbItems(),
  62.             'request_uri' => $request->getUri(),
  63.         ], $additionalVariables));
  64.     }
  65.     public function isActive(string $tag)
  66.     {
  67.         return mb_strtolower($this->navbarTagAnnotationListener->getTag()) === mb_strtolower($tag);
  68.     }
  69.     public function pageTitle(int $offset null)
  70.     {
  71.         $items $this->breadcrumbAnnotationListener->getBreadcrumbItems();
  72.         if (=== count($items)) {
  73.             return 0;
  74.         }
  75.         if (null === $offset) {
  76.             $offset count($items) - 1;
  77.         }
  78.         if (!isset($items[$offset])) {
  79.             return null;
  80.         }
  81.         return $items[$offset]->name;
  82.     }
  83.     public function encoreEntryCssSource(string $entryName): string
  84.     {
  85.         // @see https://github.com/symfony/symfony/pull/39733
  86.         $this->entrypointLookup->reset();
  87.         $files $this->entrypointLookup->getCssFiles($entryName);
  88.         $source '';
  89.         foreach ($files as $file) {
  90.             $source .= file_get_contents($this->publicDir.'/'.$file);
  91.         }
  92.         return $source;
  93.     }
  94. }