vendor/symfony-cmf/routing-bundle/src/Routing/DynamicRouter.php line 70

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the Symfony CMF package.
  4. *
  5. * (c) Symfony CMF
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Cmf\Bundle\RoutingBundle\Routing;
  11. use Symfony\Cmf\Component\Routing\DynamicRouter as BaseDynamicRouter;
  12. use Symfony\Cmf\Component\Routing\RouteObjectInterface;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\RequestStack;
  15. use Symfony\Component\Routing\Exception\ResourceNotFoundException;
  16. /**
  17. * Symfony framework integration of the CMF routing component DynamicRouter class.
  18. *
  19. * @author Filippo de Santis
  20. * @author David Buchmann
  21. * @author Lukas Smith
  22. * @author Nacho Martìn
  23. */
  24. class DynamicRouter extends BaseDynamicRouter
  25. {
  26. /**
  27. * key for the request attribute that contains the route document.
  28. */
  29. const ROUTE_KEY = 'routeDocument';
  30. /**
  31. * key for the request attribute that contains the content document if this
  32. * route has one associated.
  33. */
  34. const CONTENT_KEY = 'contentDocument';
  35. /**
  36. * key for the request attribute that contains the template this document
  37. * wants to use.
  38. */
  39. const CONTENT_TEMPLATE = 'template';
  40. /**
  41. * @var RequestStack
  42. */
  43. private $requestStack;
  44. /**
  45. * Put content and template name into the request attributes instead of the
  46. * route defaults.
  47. *
  48. * {@inheritdoc}
  49. *
  50. * The match should identify a controller for symfony. This can either be
  51. * the fully qualified class name or the service name of a controller that
  52. * is registered as a service. In both cases, the action to call on that
  53. * controller is appended, separated with two colons.
  54. */
  55. public function match($url)
  56. {
  57. $defaults = parent::match($url);
  58. return $this->cleanDefaults($defaults);
  59. }
  60. public function matchRequest(Request $request)
  61. {
  62. $defaults = parent::matchRequest($request);
  63. return $this->cleanDefaults($defaults, $request);
  64. }
  65. /**
  66. * Clean up the match data and move some values into the request attributes.
  67. *
  68. * @param array $defaults The defaults from the match
  69. * @param Request $request The request object if available
  70. *
  71. * @return array the updated defaults to return for this match
  72. */
  73. protected function cleanDefaults($defaults, Request $request = null)
  74. {
  75. if (null === $request) {
  76. $request = $this->getRequest();
  77. }
  78. if (\array_key_exists(RouteObjectInterface::ROUTE_OBJECT, $defaults)) {
  79. $request->attributes->set(self::ROUTE_KEY, $defaults[RouteObjectInterface::ROUTE_OBJECT]);
  80. unset($defaults[RouteObjectInterface::ROUTE_OBJECT]);
  81. }
  82. if (\array_key_exists(RouteObjectInterface::CONTENT_OBJECT, $defaults)) {
  83. $request->attributes->set(self::CONTENT_KEY, $defaults[RouteObjectInterface::CONTENT_OBJECT]);
  84. unset($defaults[RouteObjectInterface::CONTENT_OBJECT]);
  85. }
  86. if (\array_key_exists(RouteObjectInterface::TEMPLATE_NAME, $defaults)) {
  87. $request->attributes->set(self::CONTENT_TEMPLATE, $defaults[RouteObjectInterface::TEMPLATE_NAME]);
  88. // contentTemplate is deprecated as of version 2.0, to be removed in 3.0
  89. $request->attributes->set('contentTemplate', $defaults[RouteObjectInterface::TEMPLATE_NAME]);
  90. unset($defaults[RouteObjectInterface::TEMPLATE_NAME]);
  91. }
  92. return $defaults;
  93. }
  94. /**
  95. * Set the request stack so that we can find the current request.
  96. */
  97. public function setRequestStack(RequestStack $requestStack)
  98. {
  99. $this->requestStack = $requestStack;
  100. }
  101. /**
  102. * Get the current request from the request stack.
  103. *
  104. * @return Request
  105. *
  106. * @throws \Symfony\Component\Routing\Exception\ResourceNotFoundException
  107. */
  108. public function getRequest()
  109. {
  110. $currentRequest = $this->requestStack->getCurrentRequest();
  111. if (!$currentRequest) {
  112. throw new ResourceNotFoundException('There is no request in the request stack');
  113. }
  114. return $currentRequest;
  115. }
  116. }