vendor/pimcore/pimcore/lib/Routing/DynamicRouteProvider.php line 84

Open in your IDE?
  1. <?php
  2. /**
  3. * Pimcore
  4. *
  5. * This source file is available under two different licenses:
  6. * - GNU General Public License version 3 (GPLv3)
  7. * - Pimcore Commercial License (PCL)
  8. * Full copyright and license information is available in
  9. * LICENSE.md which is distributed with this source code.
  10. *
  11. * @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12. * @license http://www.pimcore.org/license GPLv3 and PCL
  13. */
  14. namespace Pimcore\Routing;
  15. use Pimcore\Http\Request\Resolver\SiteResolver;
  16. use Pimcore\Routing\Dynamic\DynamicRequestContext;
  17. use Pimcore\Routing\Dynamic\DynamicRouteHandlerInterface;
  18. use Symfony\Cmf\Component\Routing\RouteProviderInterface;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Component\Routing\Exception\RouteNotFoundException;
  21. use Symfony\Component\Routing\Route;
  22. use Symfony\Component\Routing\RouteCollection;
  23. /**
  24. * @internal
  25. */
  26. final class DynamicRouteProvider implements RouteProviderInterface
  27. {
  28. /**
  29. * @var SiteResolver
  30. */
  31. protected $siteResolver;
  32. /**
  33. * @var DynamicRouteHandlerInterface[]
  34. */
  35. protected $handlers = [];
  36. /**
  37. * @param SiteResolver $siteResolver
  38. * @param DynamicRouteHandlerInterface[] $handlers
  39. */
  40. public function __construct(SiteResolver $siteResolver, array $handlers = [])
  41. {
  42. $this->siteResolver = $siteResolver;
  43. foreach ($handlers as $handler) {
  44. $this->addHandler($handler);
  45. }
  46. }
  47. /**
  48. * @param DynamicRouteHandlerInterface $handler
  49. */
  50. public function addHandler(DynamicRouteHandlerInterface $handler)
  51. {
  52. if (!in_array($handler, $this->handlers, true)) {
  53. $this->handlers[] = $handler;
  54. }
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function getRouteCollectionForRequest(Request $request): RouteCollection
  60. {
  61. $collection = new RouteCollection();
  62. if ($request->attributes->has('_controller')) {
  63. return $collection;
  64. }
  65. $path = $originalPath = rawurldecode($request->getPathInfo());
  66. // site path handled by FrontendRoutingListener which runs before routing is started
  67. if (null !== $sitePath = $this->siteResolver->getSitePath($request)) {
  68. $path = $sitePath;
  69. }
  70. foreach ($this->handlers as $handler) {
  71. $handler->matchRequest($collection, new DynamicRequestContext($request, $path, $originalPath));
  72. }
  73. return $collection;
  74. }
  75. /**
  76. * {@inheritdoc}
  77. */
  78. public function getRouteByName($name): Route
  79. {
  80. foreach ($this->handlers as $handler) {
  81. try {
  82. return $handler->getRouteByName($name);
  83. } catch (RouteNotFoundException $e) {
  84. // noop
  85. }
  86. }
  87. throw new RouteNotFoundException(sprintf("Route for name '%s' was not found", $name));
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. public function getRoutesByNames($names): array
  93. {
  94. // TODO needs performance optimizations
  95. // TODO really return all routes here as documentation states? where is this used?
  96. $routes = [];
  97. if (is_array($names)) {
  98. foreach ($names as $name) {
  99. try {
  100. $route = $this->getRouteByName($name);
  101. $routes[] = $route;
  102. } catch (RouteNotFoundException $e) {
  103. // noop
  104. }
  105. }
  106. }
  107. return $routes;
  108. }
  109. }