src/Hitso/Bundle/RoutingBundle/Router/AliasRouter.php line 58

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Hitso\Bundle\RoutingBundle\Router;
  4. use Symfony\Cmf\Component\Routing\DynamicRouter as BaseDynamicRouter;
  5. use Symfony\Cmf\Component\Routing\RouteObjectInterface;
  6. use Symfony\Cmf\Component\Routing\RouteProviderInterface;
  7. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\Routing\Exception\ResourceNotFoundException;
  10. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  11. use Symfony\Component\Routing\RequestContext;
  12. class AliasRouter extends BaseDynamicRouter
  13. {
  14.     const ROUTE_KEY        'routeDocument';
  15.     const CONTENT_KEY      'contentDocument';
  16.     const CONTENT_TEMPLATE 'contentTemplate';
  17.     /**
  18.      * @var Request
  19.      */
  20.     protected $request;
  21.     public function __construct(
  22.         RequestContext $context,
  23.         $matcher,
  24.         UrlGeneratorInterface $generator,
  25.         $uriFilterRegexp '',
  26.         EventDispatcherInterface $eventDispatcher null,
  27.         RouteProviderInterface $provider null
  28.     ) {
  29.         parent::__construct($context$matcher$generator$uriFilterRegexp$eventDispatcher$provider);
  30.     }
  31.     /**
  32.      * Put content and template name into the request attributes instead of the
  33.      * route defaults.
  34.      *
  35.      * {@inheritdoc}
  36.      *
  37.      * The match should identify  a controller for symfony. This can either be
  38.      * the fully qualified class name or the service name of a controller that
  39.      * is registered as a service. In both cases, the action to call on that
  40.      * controller is appended, separated with two colons.
  41.      */
  42.     public function match($url)
  43.     {
  44.         $defaults parent::match($url);
  45.         return $this->cleanDefaults($defaults);
  46.     }
  47.     public function matchRequest(Request $request)
  48.     {
  49.         $defaults parent::matchRequest($request);
  50.         return $this->cleanDefaults($defaults$request);
  51.     }
  52.     /**
  53.      * Clean up the match data and move some values into the request attributes.
  54.      *
  55.      * @param array   $defaults The defaults from the match
  56.      * @param Request $request  The request object if available
  57.      *
  58.      * @return array the updated defaults to return for this match
  59.      */
  60.     protected function cleanDefaults($defaultsRequest $request null)
  61.     {
  62.         if (null === $request) {
  63.             $request $this->getRequest();
  64.         }
  65.         if (isset($defaults[RouteObjectInterface::ROUTE_OBJECT])) {
  66.             $request->attributes->set(self::ROUTE_KEY$defaults[RouteObjectInterface::ROUTE_OBJECT]);
  67.             unset($defaults[RouteObjectInterface::ROUTE_OBJECT]);
  68.         }
  69.         return $defaults;
  70.     }
  71.     /**
  72.      * @param Request $request
  73.      */
  74.     public function setRequest(Request $request null)
  75.     {
  76.         $this->request $request;
  77.     }
  78.     /**
  79.      * @return Request
  80.      *
  81.      * @throws ResourceNotFoundException
  82.      */
  83.     public function getRequest()
  84.     {
  85.         if (null === $this->request) {
  86.             throw new ResourceNotFoundException('Request object not available from container');
  87.         }
  88.         return $this->request;
  89.     }
  90. }