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

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.     public 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.     public const CONTENT_KEY 'contentDocument';
  35.     /**
  36.      * key for the request attribute that contains the template this document
  37.      * wants to use.
  38.      */
  39.     public const CONTENT_TEMPLATE 'template';
  40.     private RequestStack $requestStack;
  41.     /**
  42.      * Put content and template name into the request attributes instead of the
  43.      * route defaults.
  44.      *
  45.      * {@inheritdoc}
  46.      *
  47.      * The match should identify  a controller for symfony. This can either be
  48.      * the fully qualified class name or the service name of a controller that
  49.      * is registered as a service. In both cases, the action to call on that
  50.      * controller is appended, separated with two colons.
  51.      */
  52.     public function match(string $url): array
  53.     {
  54.         $defaults parent::match($url);
  55.         return $this->cleanDefaults($defaults);
  56.     }
  57.     public function matchRequest(Request $request): array
  58.     {
  59.         $defaults parent::matchRequest($request);
  60.         return $this->cleanDefaults($defaults$request);
  61.     }
  62.     /**
  63.      * Clean up the match data and move some values into the request attributes.
  64.      *
  65.      * @param array<string, string> $defaults The defaults from the match
  66.      * @param Request|null          $request  The request object if available
  67.      *
  68.      * @return array<string, string> the updated defaults to return for this match
  69.      */
  70.     protected function cleanDefaults(array $defaultsRequest $request null): array
  71.     {
  72.         if (null === $request) {
  73.             $request $this->getRequest();
  74.         }
  75.         if (\array_key_exists(RouteObjectInterface::ROUTE_OBJECT$defaults)) {
  76.             $request->attributes->set(self::ROUTE_KEY$defaults[RouteObjectInterface::ROUTE_OBJECT]);
  77.             unset($defaults[RouteObjectInterface::ROUTE_OBJECT]);
  78.         }
  79.         if (\array_key_exists(RouteObjectInterface::CONTENT_OBJECT$defaults)) {
  80.             $request->attributes->set(self::CONTENT_KEY$defaults[RouteObjectInterface::CONTENT_OBJECT]);
  81.             unset($defaults[RouteObjectInterface::CONTENT_OBJECT]);
  82.         }
  83.         if (\array_key_exists(RouteObjectInterface::TEMPLATE_NAME$defaults)) {
  84.             $request->attributes->set(self::CONTENT_TEMPLATE$defaults[RouteObjectInterface::TEMPLATE_NAME]);
  85.             unset($defaults[RouteObjectInterface::TEMPLATE_NAME]);
  86.         }
  87.         return $defaults;
  88.     }
  89.     /**
  90.      * Set the request stack so that we can find the current request.
  91.      */
  92.     public function setRequestStack(RequestStack $requestStack): void
  93.     {
  94.         $this->requestStack $requestStack;
  95.     }
  96.     /**
  97.      * Get the current request from the request stack.
  98.      *
  99.      * @throws ResourceNotFoundException
  100.      */
  101.     public function getRequest(): Request
  102.     {
  103.         $currentRequest $this->requestStack->getCurrentRequest();
  104.         if (!$currentRequest) {
  105.             throw new ResourceNotFoundException('There is no request in the request stack');
  106.         }
  107.         return $currentRequest;
  108.     }
  109. }