vendor/se7enxweb/legacy-bridge/bundle/Routing/FallbackRouter.php line 37

Open in your IDE?
  1. <?php
  2. /**
  3. * @copyright Copyright (C) eZ Systems AS. All rights reserved.
  4. * @license For full copyright and license information view LICENSE file distributed with this source code.
  5. */
  6. namespace eZ\Bundle\EzPublishLegacyBundle\Routing;
  7. use Symfony\Component\Routing\RouterInterface;
  8. use Symfony\Component\Routing\RequestContext;
  9. use Symfony\Component\Routing\RouteCollection;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
  12. use Symfony\Component\Routing\Exception\RouteNotFoundException;
  13. use Psr\Log\LoggerInterface;
  14. use RuntimeException;
  15. class FallbackRouter implements RouterInterface, RequestMatcherInterface
  16. {
  17. const ROUTE_NAME = 'ez_legacy';
  18. /**
  19. * @var \Symfony\Component\Routing\RequestContext
  20. */
  21. private $context;
  22. /**
  23. * @var \Psr\Log\LoggerInterface
  24. */
  25. private $logger;
  26. /**
  27. * @var UrlGenerator
  28. */
  29. private $urlGenerator;
  30. public function __construct(UrlGenerator $urlGenerator, RequestContext $context = null, LoggerInterface $logger = null)
  31. {
  32. $this->urlGenerator = $urlGenerator;
  33. $this->context = $context = $context ?: new RequestContext();
  34. $this->logger = $logger;
  35. }
  36. /**
  37. * Sets the request context.
  38. *
  39. * @param \Symfony\Component\Routing\RequestContext $context The context
  40. */
  41. public function setContext(RequestContext $context)
  42. {
  43. $this->context = $context;
  44. }
  45. /**
  46. * Gets the request context.
  47. *
  48. * @return \Symfony\Component\Routing\RequestContext The context
  49. */
  50. public function getContext()
  51. {
  52. return $this->context;
  53. }
  54. /**
  55. * Gets the RouteCollection instance associated with this Router.
  56. *
  57. * @return RouteCollection A RouteCollection instance
  58. */
  59. public function getRouteCollection()
  60. {
  61. // No route registered for legacy fallback, request will be forwarded directly to the legacy kernel
  62. return new RouteCollection();
  63. }
  64. /**
  65. * Generates a URL for an eZ Publish legacy fallback route, from the given parameters.
  66. * "module_uri" must be provided as a key in $parameters. The module URI must contain ordered parameters if any
  67. * (e.g. /content/view/full/2, "full", and "2" being regular ordered parameters. See your module definition for more info.).
  68. * All additional named parameters will be passed as unordered params in the form "/(<paramName>)/<paramValue".
  69. *
  70. * Example :
  71. * <code>
  72. * $params = array(
  73. * 'module_uri' => '/content/view/full/2',
  74. * 'offset' => 30,
  75. * 'limit' => 10
  76. * );
  77. * $url = $legacyRouter->generate( 'ez_legacy', $params );
  78. * // $url will be "/content/view/full/2/(offset)/30/(limit)/10"
  79. * </code>
  80. *
  81. * @param string $name The name of the route
  82. * @param mixed $parameters An array of parameters
  83. * @param int $referenceType The type of reference to be generated (one of the constants)
  84. *
  85. * @throws \Symfony\Component\Routing\Exception\RouteNotFoundException
  86. * @throws \InvalidArgumentException
  87. *
  88. * @return string The generated URL
  89. *
  90. * @api
  91. */
  92. public function generate($name, $parameters = [], $referenceType = self::ABSOLUTE_PATH)
  93. {
  94. if ($name === self::ROUTE_NAME) {
  95. if (!isset($parameters['module_uri'])) {
  96. throw new \InvalidArgumentException('When generating an eZ Publish legacy fallback route, "uri" parameter must be provided.');
  97. }
  98. $moduleUri = $parameters['module_uri'];
  99. unset($parameters['module_uri']);
  100. return $this->urlGenerator->generate($moduleUri, $parameters, $referenceType);
  101. }
  102. throw new RouteNotFoundException();
  103. }
  104. public function match($pathinfo)
  105. {
  106. throw new RuntimeException("The UrlAliasRouter doesn't support the match() method. Please use matchRequest() instead.");
  107. }
  108. public function matchRequest(Request $request)
  109. {
  110. $moduleUri = rtrim($request->attributes->get('semanticPathinfo', $request->getPathInfo()), '/')
  111. . $request->attributes->get('viewParametersString', '')
  112. . '?' . $request->getQueryString();
  113. return [
  114. '_route' => self::ROUTE_NAME,
  115. '_controller' => 'ezpublish_legacy.controller:indexAction',
  116. 'module_uri' => $moduleUri,
  117. ];
  118. }
  119. }