vendor/pimcore/pimcore/lib/Twig/Extension/Templating/Navigation.php line 80

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * Pimcore
  5.  *
  6.  * This source file is available under two different licenses:
  7.  * - GNU General Public License version 3 (GPLv3)
  8.  * - Pimcore Commercial License (PCL)
  9.  * Full copyright and license information is available in
  10.  * LICENSE.md which is distributed with this source code.
  11.  *
  12.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  13.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  14.  */
  15. namespace Pimcore\Twig\Extension\Templating;
  16. use Pimcore\Model\Document;
  17. use Pimcore\Navigation\Builder;
  18. use Pimcore\Navigation\Container;
  19. use Pimcore\Navigation\Renderer\Breadcrumbs;
  20. use Pimcore\Navigation\Renderer\Menu;
  21. use Pimcore\Navigation\Renderer\Menu as MenuRenderer;
  22. use Pimcore\Navigation\Renderer\RendererInterface;
  23. use Pimcore\Twig\Extension\Templating\Navigation\Exception\InvalidRendererException;
  24. use Pimcore\Twig\Extension\Templating\Navigation\Exception\RendererNotFoundException;
  25. use Pimcore\Twig\Extension\Templating\Traits\HelperCharsetTrait;
  26. use Psr\Container\ContainerInterface;
  27. use Symfony\Component\OptionsResolver\OptionsResolver;
  28. use Twig\Extension\RuntimeExtensionInterface;
  29. /**
  30.  * @method MenuRenderer menu()
  31.  * @method Breadcrumbs breadcrumbs()
  32.  *
  33.  */
  34. class Navigation implements RuntimeExtensionInterface
  35. {
  36.     use HelperCharsetTrait;
  37.     /**
  38.      * @var Builder
  39.      */
  40.     private $builder;
  41.     /**
  42.      * @var ContainerInterface
  43.      */
  44.     private $rendererLocator;
  45.     /**
  46.      * @param Builder $builder
  47.      * @param ContainerInterface $rendererLocator
  48.      */
  49.     public function __construct(Builder $builderContainerInterface $rendererLocator)
  50.     {
  51.         $this->builder $builder;
  52.         $this->rendererLocator $rendererLocator;
  53.     }
  54.     /**
  55.      * Builds a navigation container by passing arguments
  56.      *
  57.      * @deprecated
  58.      *
  59.      * @param Document $activeDocument
  60.      * @param Document|null $navigationRootDocument
  61.      * @param string|null $htmlMenuPrefix
  62.      * @param callable|null $pageCallback
  63.      * @param bool|string $cache
  64.      * @param int|null $maxDepth
  65.      * @param int|null $cacheLifetime
  66.      *
  67.      * @return Container
  68.      *
  69.      * @throws \Exception
  70.      */
  71.     public function buildNavigation(
  72.         Document $activeDocument,
  73.         Document $navigationRootDocument null,
  74.         string $htmlMenuPrefix null,
  75.         callable $pageCallback null,
  76.         $cache true,
  77.         $maxDepth null,
  78.         $cacheLifetime null
  79.     ): Container {
  80.         return $this->builder->getNavigation(
  81.             $activeDocument,
  82.             $navigationRootDocument,
  83.             $htmlMenuPrefix,
  84.             $pageCallback,
  85.             $cache,
  86.             $maxDepth,
  87.             $cacheLifetime
  88.         );
  89.     }
  90.     /**
  91.      * Builds a navigation container by passing params
  92.      * Possible config params are: 'root', 'htmlMenuPrefix', 'pageCallback', 'cache', 'maxDepth', 'active'
  93.      *
  94.      * @param array $params
  95.      *
  96.      * @return Container
  97.      *
  98.      * @throws \Exception
  99.      */
  100.     public function build(array $params): Container
  101.     {
  102.         $optionsResolver = new OptionsResolver();
  103.         $optionsResolver->setDefaults([
  104.            'root' => null,
  105.            'htmlMenuPrefix' => null,
  106.            'pageCallback' => null,
  107.            'cache' => true,
  108.            'cacheLifetime' => null,
  109.            'maxDepth' => null,
  110.            'active' => null,
  111.         ]);
  112.         $options $optionsResolver->resolve($params);
  113.         return $this->builder->getNavigation(
  114.             $options['active'],
  115.             $options['root'],
  116.             $options['htmlMenuPrefix'],
  117.             $options['pageCallback'],
  118.             $options['cache'],
  119.             $options['maxDepth'],
  120.             $options['cacheLifetime']
  121.         );
  122.     }
  123.     /**
  124.      * Get a named renderer
  125.      *
  126.      * @param string $alias
  127.      *
  128.      * @return RendererInterface
  129.      */
  130.     public function getRenderer(string $alias): RendererInterface
  131.     {
  132.         if (!$this->rendererLocator->has($alias)) {
  133.             throw RendererNotFoundException::create($alias);
  134.         }
  135.         $renderer $this->rendererLocator->get($alias);
  136.         if (!$renderer instanceof RendererInterface) {
  137.             throw InvalidRendererException::create($alias$renderer);
  138.         }
  139.         return $renderer;
  140.     }
  141.     /**
  142.      * Renders a navigation with the given renderer
  143.      *
  144.      * @param Container $container
  145.      * @param string $rendererName
  146.      * @param string $renderMethod     Optional render method to use (e.g. menu -> renderMenu)
  147.      * @param array<int, mixed> $rendererArguments      Option arguments to pass to the render method after the container
  148.      *
  149.      * @return string
  150.      */
  151.     public function render(
  152.         Container $container,
  153.         string $rendererName 'menu',
  154.         string $renderMethod 'render',
  155.         ...$rendererArguments
  156.     ) {
  157.         $renderer $this->getRenderer($rendererName);
  158.         if (!method_exists($renderer$renderMethod)) {
  159.             throw new \InvalidArgumentException(sprintf('Method "%s" does not exist on renderer "%s"'$renderMethod$rendererName));
  160.         }
  161.         $args array_merge([$container], array_values($rendererArguments));
  162.         return call_user_func_array([$renderer$renderMethod], $args);
  163.     }
  164.     /**
  165.      * Magic overload is an alias to getRenderer()
  166.      *
  167.      * @param string $method
  168.      * @param array $arguments
  169.      *
  170.      * @return RendererInterface
  171.      */
  172.     public function __call($method, array $arguments = []): RendererInterface
  173.     {
  174.         return $this->getRenderer($method);
  175.     }
  176. }
  177. class_alias(Navigation::class, 'Pimcore\Templating\Helper\Navigation');