vendor/knplabs/knp-menu/src/Knp/Menu/Twig/MenuExtension.php line 73

Open in your IDE?
  1. <?php
  2. namespace Knp\Menu\Twig;
  3. use Knp\Menu\ItemInterface;
  4. use Knp\Menu\Matcher\MatcherInterface;
  5. use Knp\Menu\Util\MenuManipulator;
  6. use Twig\Extension\AbstractExtension;
  7. use Twig\TwigFilter;
  8. use Twig\TwigFunction;
  9. use Twig\TwigTest;
  10. class MenuExtension extends AbstractExtension
  11. {
  12.     public function __construct(private Helper $helper, private ?MatcherInterface $matcher null, private ?MenuManipulator $menuManipulator null)
  13.     {
  14.     }
  15.     /**
  16.      * @return array<int, TwigFunction>
  17.      */
  18.     public function getFunctions(): array
  19.     {
  20.         return [
  21.              new TwigFunction('knp_menu_get', [$this'get']),
  22.              new TwigFunction('knp_menu_render', [$this'render'], ['is_safe' => ['html']]),
  23.              new TwigFunction('knp_menu_get_breadcrumbs_array', [$this'getBreadcrumbsArray']),
  24.              new TwigFunction('knp_menu_get_current_item', [$this'getCurrentItem']),
  25.         ];
  26.     }
  27.     /**
  28.      * @return array<int, TwigFilter>
  29.      */
  30.     public function getFilters(): array
  31.     {
  32.         return [
  33.             new TwigFilter('knp_menu_as_string', [$this'pathAsString']),
  34.         ];
  35.     }
  36.     /**
  37.      * @return array<int, TwigTest>
  38.      */
  39.     public function getTests(): array
  40.     {
  41.         return [
  42.             new TwigTest('knp_menu_current', [$this'isCurrent']),
  43.             new TwigTest('knp_menu_ancestor', [$this'isAncestor']),
  44.         ];
  45.     }
  46.     /**
  47.      * Retrieves an item following a path in the tree.
  48.      *
  49.      * @param ItemInterface|string $menu
  50.      * @param array<int, string>   $path
  51.      * @param array<string, mixed> $options
  52.      */
  53.     public function get($menu, array $path = [], array $options = []): ItemInterface
  54.     {
  55.         return $this->helper->get($menu$path$options);
  56.     }
  57.     /**
  58.      * Renders a menu with the specified renderer.
  59.      *
  60.      * @param ItemInterface|string|array<ItemInterface|string> $menu
  61.      * @param array<string, mixed>                             $options
  62.      */
  63.     public function render($menu, array $options = [], ?string $renderer null): string
  64.     {
  65.         return $this->helper->render($menu$options$renderer);
  66.     }
  67.     /**
  68.      * Returns an array ready to be used for breadcrumbs.
  69.      *
  70.      * @param ItemInterface|string|array<ItemInterface|string> $menu
  71.      * @param string|array<string|null>|null                   $subItem
  72.      *
  73.      * @phpstan-param string|ItemInterface|array<int|string, string|int|float|null|array{label: string, url: string|null, item: ItemInterface|null}|ItemInterface>|\Traversable<string|int|float|null|array{label: string, url: string|null, item: ItemInterface|null}|ItemInterface> $subItem
  74.      *
  75.      * @return array<int, array<string, mixed>>
  76.      * @phpstan-return list<array{label: string, uri: string|null, item: ItemInterface|null}>
  77.      */
  78.     public function getBreadcrumbsArray($menu$subItem null): array
  79.     {
  80.         return $this->helper->getBreadcrumbsArray($menu$subItem);
  81.     }
  82.     /**
  83.      * Returns the current item of a menu.
  84.      *
  85.      * @param ItemInterface|string $menu
  86.      */
  87.     public function getCurrentItem($menu): ItemInterface
  88.     {
  89.         $rootItem $this->get($menu);
  90.         $currentItem $this->helper->getCurrentItem($rootItem);
  91.         if (null === $currentItem) {
  92.             $currentItem $rootItem;
  93.         }
  94.         return $currentItem;
  95.     }
  96.     /**
  97.      * A string representation of this menu item
  98.      *
  99.      * e.g. Top Level > Second Level > This menu
  100.      */
  101.     public function pathAsString(ItemInterface $menustring $separator ' > '): string
  102.     {
  103.         if (null === $this->menuManipulator) {
  104.             throw new \BadMethodCallException('The menu manipulator must be set to get the breadcrumbs array');
  105.         }
  106.         return $this->menuManipulator->getPathAsString($menu$separator);
  107.     }
  108.     /**
  109.      * Checks whether an item is current.
  110.      */
  111.     public function isCurrent(ItemInterface $item): bool
  112.     {
  113.         if (null === $this->matcher) {
  114.             throw new \BadMethodCallException('The matcher must be set to get the breadcrumbs array');
  115.         }
  116.         return $this->matcher->isCurrent($item);
  117.     }
  118.     /**
  119.      * Checks whether an item is the ancestor of a current item.
  120.      *
  121.      * @param int|null $depth The max depth to look for the item
  122.      */
  123.     public function isAncestor(ItemInterface $item, ?int $depth null): bool
  124.     {
  125.         if (null === $this->matcher) {
  126.             throw new \BadMethodCallException('The matcher must be set to get the breadcrumbs array');
  127.         }
  128.         return $this->matcher->isAncestor($item$depth);
  129.     }
  130. }