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

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\RouteCollection;
  22. /**
  23. * @internal
  24. */
  25. final class DynamicRouteProvider implements RouteProviderInterface
  26. {
  27. /**
  28. * @var SiteResolver
  29. */
  30. protected $siteResolver;
  31. /**
  32. * @var DynamicRouteHandlerInterface[]
  33. */
  34. protected $handlers = [];
  35. /**
  36. * @param SiteResolver $siteResolver
  37. * @param DynamicRouteHandlerInterface[] $handlers
  38. */
  39. public function __construct(SiteResolver $siteResolver, array $handlers = [])
  40. {
  41. $this->siteResolver = $siteResolver;
  42. foreach ($handlers as $handler) {
  43. $this->addHandler($handler);
  44. }
  45. }
  46. /**
  47. * @param DynamicRouteHandlerInterface $handler
  48. */
  49. public function addHandler(DynamicRouteHandlerInterface $handler)
  50. {
  51. if (!in_array($handler, $this->handlers, true)) {
  52. $this->handlers[] = $handler;
  53. }
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function getRouteCollectionForRequest(Request $request)
  59. {
  60. $collection = new RouteCollection();
  61. $path = $originalPath = urldecode($request->getPathInfo());
  62. // site path handled by FrontendRoutingListener which runs before routing is started
  63. if (null !== $sitePath = $this->siteResolver->getSitePath($request)) {
  64. $path = $sitePath;
  65. }
  66. foreach ($this->handlers as $handler) {
  67. $handler->matchRequest($collection, new DynamicRequestContext($request, $path, $originalPath));
  68. }
  69. return $collection;
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. public function getRouteByName($name)
  75. {
  76. foreach ($this->handlers as $handler) {
  77. try {
  78. return $handler->getRouteByName($name);
  79. } catch (RouteNotFoundException $e) {
  80. // noop
  81. }
  82. }
  83. throw new RouteNotFoundException(sprintf("Route for name '%s' was not found", $name));
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. public function getRoutesByNames($names)
  89. {
  90. // TODO needs performance optimizations
  91. // TODO really return all routes here as documentation states? where is this used?
  92. $routes = [];
  93. if (is_array($names)) {
  94. foreach ($names as $name) {
  95. try {
  96. $route = $this->getRouteByName($name);
  97. $routes[] = $route;
  98. } catch (RouteNotFoundException $e) {
  99. // noop
  100. }
  101. }
  102. }
  103. return $routes;
  104. }
  105. }