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

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