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

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