vendor/knplabs/knp-menu/src/Knp/Menu/Twig/MenuRuntimeExtension.php line 45

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\RuntimeExtensionInterface;
  7. /**
  8.  * @final since 3.8.0
  9.  */
  10. class MenuRuntimeExtension implements RuntimeExtensionInterface
  11. {
  12.     public function __construct(
  13.         private readonly Helper $helper,
  14.         private readonly ?MatcherInterface $matcher null,
  15.         private readonly ?MenuManipulator $menuManipulator null,
  16.     ) {
  17.     }
  18.     /**
  19.      * Retrieves an item following a path in the tree.
  20.      *
  21.      * @param array<int, string>   $path
  22.      * @param array<string, mixed> $options
  23.      *
  24.      * @internal since 3.8.0
  25.      */
  26.     public function get(ItemInterface|string $menu, array $path = [], array $options = []): ItemInterface
  27.     {
  28.         return $this->helper->get($menu$path$options);
  29.     }
  30.     /**
  31.      * Renders a menu with the specified renderer.
  32.      *
  33.      * @param string|ItemInterface|array<ItemInterface|string> $menu
  34.      * @param array<string, mixed>                             $options
  35.      *
  36.      * @internal since 3.8.0
  37.      */
  38.     public function render(array|ItemInterface|string $menu, array $options = [], ?string $renderer null): string
  39.     {
  40.         return $this->helper->render($menu$options$renderer);
  41.     }
  42.     /**
  43.      * Returns an array ready to be used for breadcrumbs.
  44.      *
  45.      * @param string|ItemInterface|array<ItemInterface|string> $menu
  46.      *
  47.      * @phpstan-param string|array<int|string, string|int|float|null|array{label: string, uri: string|null, item: ItemInterface|null}|ItemInterface> $subItem
  48.      *
  49.      * @return array<int, array<string, mixed>>
  50.      * @phpstan-return list<array{label: string, uri: string|null, item: ItemInterface|null}>
  51.      *
  52.      * @internal since 3.8.0
  53.      */
  54.     public function getBreadcrumbsArray(array|ItemInterface|string $menu, array|string|null $subItem null): array
  55.     {
  56.         return $this->helper->getBreadcrumbsArray($menu$subItem);
  57.     }
  58.     /**
  59.      * Returns the current item of a menu.
  60.      *
  61.      * @internal since 3.8.0
  62.      */
  63.     public function getCurrentItem(ItemInterface|string $menu): ItemInterface
  64.     {
  65.         $rootItem $this->get($menu);
  66.         $currentItem $this->helper->getCurrentItem($rootItem);
  67.         if (null === $currentItem) {
  68.             $currentItem $rootItem;
  69.         }
  70.         return $currentItem;
  71.     }
  72.     /**
  73.      * A string representation of this menu item
  74.      *
  75.      * e.g. Top Level > Second Level > This menu
  76.      *
  77.      * @internal since 3.8.0
  78.      */
  79.     public function pathAsString(ItemInterface $menustring $separator ' > '): string
  80.     {
  81.         if (null === $this->menuManipulator) {
  82.             throw new \BadMethodCallException('The menu manipulator must be set to get the breadcrumbs array');
  83.         }
  84.         return $this->menuManipulator->getPathAsString($menu$separator);
  85.     }
  86.     /**
  87.      * Checks whether an item is current.
  88.      *
  89.      * @internal since 3.8.0
  90.      */
  91.     public function isCurrent(ItemInterface $item): bool
  92.     {
  93.         if (null === $this->matcher) {
  94.             throw new \BadMethodCallException('The matcher must be set to get the breadcrumbs array');
  95.         }
  96.         return $this->matcher->isCurrent($item);
  97.     }
  98.     /**
  99.      * Checks whether an item is the ancestor of a current item.
  100.      *
  101.      * @param int|null $depth The max depth to look for the item
  102.      *
  103.      * @internal since 3.8.0
  104.      */
  105.     public function isAncestor(ItemInterface $item, ?int $depth null): bool
  106.     {
  107.         if (null === $this->matcher) {
  108.             throw new \BadMethodCallException('The matcher must be set to get the breadcrumbs array');
  109.         }
  110.         return $this->matcher->isAncestor($item$depth);
  111.     }
  112. }