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

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.      * @var Environment
  10.      */
  11.     private $environment;
  12.     /**
  13.      * @var MatcherInterface
  14.      */
  15.     private $matcher;
  16.     /**
  17.      * @var array<string, mixed>
  18.      */
  19.     private $defaultOptions;
  20.     /**
  21.      * @param array<string, mixed> $defaultOptions
  22.      */
  23.     public function __construct(
  24.         Environment $environment,
  25.         string $template,
  26.         MatcherInterface $matcher,
  27.         array $defaultOptions = []
  28.     ) {
  29.         $this->environment $environment;
  30.         $this->matcher $matcher;
  31.         $this->defaultOptions = \array_merge([
  32.             'depth' => null,
  33.             'matchingDepth' => null,
  34.             'currentAsLink' => true,
  35.             'currentClass' => 'current',
  36.             'ancestorClass' => 'current_ancestor',
  37.             'firstClass' => 'first',
  38.             'lastClass' => 'last',
  39.             'template' => $template,
  40.             'compressed' => false,
  41.             'allow_safe_labels' => false,
  42.             'clear_matcher' => true,
  43.             'leaf_class' => null,
  44.             'branch_class' => null,
  45.         ], $defaultOptions);
  46.     }
  47.     public function render(ItemInterface $item, array $options = []): string
  48.     {
  49.         $options = \array_merge($this->defaultOptions$options);
  50.         $html $this->environment->render($options['template'], ['item' => $item'options' => $options'matcher' => $this->matcher]);
  51.         if ($options['clear_matcher']) {
  52.             $this->matcher->clear();
  53.         }
  54.         return $html;
  55.     }
  56. }