vendor/knplabs/knp-menu/src/Knp/Menu/Renderer/TwigRenderer.php line 37

Open in your IDE?
  1. <?php
  2. namespace Knp\Menu\Renderer;
  3. use Knp\Menu\ItemInterface;
  4. use Knp\Menu\Matcher\MatcherInterface;
  5. use Twig\Environment;
  6. class TwigRenderer implements RendererInterface
  7. {
  8.     /**
  9.      * @param array<string, mixed> $defaultOptions
  10.      */
  11.     public function __construct(
  12.         private Environment $environment,
  13.         string $template,
  14.         private MatcherInterface $matcher,
  15.         private array $defaultOptions = []
  16.     ) {
  17.         $this->defaultOptions \array_merge([
  18.             'depth' => null,
  19.             'matchingDepth' => null,
  20.             'currentAsLink' => true,
  21.             'currentClass' => 'current',
  22.             'ancestorClass' => 'current_ancestor',
  23.             'firstClass' => 'first',
  24.             'lastClass' => 'last',
  25.             'template' => $template,
  26.             'compressed' => false,
  27.             'allow_safe_labels' => false,
  28.             'clear_matcher' => true,
  29.             'leaf_class' => null,
  30.             'branch_class' => null,
  31.         ], $defaultOptions);
  32.     }
  33.     public function render(ItemInterface $item, array $options = []): string
  34.     {
  35.         $options \array_merge($this->defaultOptions$options);
  36.         $html $this->environment->render($options['template'], ['item' => $item'options' => $options'matcher' => $this->matcher]);
  37.         if ($options['clear_matcher']) {
  38.             $this->matcher->clear();
  39.         }
  40.         return $html;
  41.     }
  42. }