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

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