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

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