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

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