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

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\Navigation\Builder;
  17. use Pimcore\Navigation\Container;
  18. use Pimcore\Navigation\Renderer\Breadcrumbs;
  19. use Pimcore\Navigation\Renderer\Menu as MenuRenderer;
  20. use Pimcore\Navigation\Renderer\RendererInterface;
  21. use Pimcore\Twig\Extension\Templating\Navigation\Exception\InvalidRendererException;
  22. use Pimcore\Twig\Extension\Templating\Navigation\Exception\RendererNotFoundException;
  23. use Pimcore\Twig\Extension\Templating\Traits\HelperCharsetTrait;
  24. use Psr\Container\ContainerInterface;
  25. use Twig\Extension\RuntimeExtensionInterface;
  26. /**
  27.  * @method MenuRenderer menu()
  28.  * @method Breadcrumbs breadcrumbs()
  29.  *
  30.  */
  31. class Navigation implements RuntimeExtensionInterface
  32. {
  33.     use HelperCharsetTrait;
  34.     /**
  35.      * @var Builder
  36.      */
  37.     private $builder;
  38.     /**
  39.      * @var ContainerInterface
  40.      */
  41.     private $rendererLocator;
  42.     /**
  43.      * @param Builder $builder
  44.      * @param ContainerInterface $rendererLocator
  45.      */
  46.     public function __construct(Builder $builderContainerInterface $rendererLocator)
  47.     {
  48.         $this->builder $builder;
  49.         $this->rendererLocator $rendererLocator;
  50.     }
  51.     /**
  52.      * Builds a navigation container by passing params
  53.      * Possible config params are: 'root', 'htmlMenuPrefix', 'pageCallback', 'cache', 'cacheLifetime', 'maxDepth', 'active', 'markActiveTrail'
  54.      *
  55.      * @param array $params
  56.      *
  57.      * @return Container
  58.      *
  59.      * @throws \Exception
  60.      */
  61.     public function build(array $params): Container
  62.     {
  63.         return $this->builder->getNavigation($params);
  64.     }
  65.     /**
  66.      * Get a named renderer
  67.      *
  68.      * @param string $alias
  69.      *
  70.      * @return RendererInterface
  71.      */
  72.     public function getRenderer(string $alias): RendererInterface
  73.     {
  74.         if (!$this->rendererLocator->has($alias)) {
  75.             throw RendererNotFoundException::create($alias);
  76.         }
  77.         $renderer $this->rendererLocator->get($alias);
  78.         if (!$renderer instanceof RendererInterface) {
  79.             throw InvalidRendererException::create($alias$renderer);
  80.         }
  81.         return $renderer;
  82.     }
  83.     /**
  84.      * Renders a navigation with the given renderer
  85.      *
  86.      * @param Container $container
  87.      * @param string $rendererName
  88.      * @param string $renderMethod     Optional render method to use (e.g. menu -> renderMenu)
  89.      * @param array<int, mixed> $rendererArguments      Option arguments to pass to the render method after the container
  90.      *
  91.      * @return string
  92.      */
  93.     public function render(
  94.         Container $container,
  95.         string $rendererName 'menu',
  96.         string $renderMethod 'render',
  97.         ...$rendererArguments
  98.     ) {
  99.         $renderer $this->getRenderer($rendererName);
  100.         if (!method_exists($renderer$renderMethod)) {
  101.             throw new \InvalidArgumentException(sprintf('Method "%s" does not exist on renderer "%s"'$renderMethod$rendererName));
  102.         }
  103.         $args array_merge([$container], array_values($rendererArguments));
  104.         return call_user_func_array([$renderer$renderMethod], $args);
  105.     }
  106.     /**
  107.      * Magic overload is an alias to getRenderer()
  108.      *
  109.      * @param string $method
  110.      * @param array $arguments
  111.      *
  112.      * @return RendererInterface
  113.      */
  114.     public function __call($method, array $arguments = []): RendererInterface
  115.     {
  116.         return $this->getRenderer($method);
  117.     }
  118. }