vendor/api-platform/core/src/Symfony/Routing/Router.php line 66

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the API Platform project.
  4.  *
  5.  * (c) Kévin Dunglas <dunglas@gmail.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace ApiPlatform\Symfony\Routing;
  12. use ApiPlatform\Api\UrlGeneratorInterface;
  13. use Symfony\Component\HttpFoundation\Exception\RequestExceptionInterface;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\Routing\Exception\ResourceNotFoundException;
  16. use Symfony\Component\Routing\RequestContext;
  17. use Symfony\Component\Routing\RouteCollection;
  18. use Symfony\Component\Routing\RouterInterface;
  19. /**
  20.  * Symfony router decorator.
  21.  *
  22.  * Kévin Dunglas <dunglas@gmail.com>
  23.  */
  24. final class Router implements RouterInterfaceUrlGeneratorInterface
  25. {
  26.     public const CONST_MAP = [
  27.         UrlGeneratorInterface::ABS_URL => RouterInterface::ABSOLUTE_URL,
  28.         UrlGeneratorInterface::ABS_PATH => RouterInterface::ABSOLUTE_PATH,
  29.         UrlGeneratorInterface::REL_PATH => RouterInterface::RELATIVE_PATH,
  30.         UrlGeneratorInterface::NET_PATH => RouterInterface::NETWORK_PATH,
  31.     ];
  32.     private $router;
  33.     private $urlGenerationStrategy;
  34.     public function __construct(RouterInterface $routerint $urlGenerationStrategy self::ABS_PATH)
  35.     {
  36.         $this->router $router;
  37.         $this->urlGenerationStrategy $urlGenerationStrategy;
  38.     }
  39.     /**
  40.      * {@inheritdoc}
  41.      */
  42.     public function setContext(RequestContext $context)
  43.     {
  44.         $this->router->setContext($context);
  45.     }
  46.     /**
  47.      * {@inheritdoc}
  48.      */
  49.     public function getContext(): RequestContext
  50.     {
  51.         return $this->router->getContext();
  52.     }
  53.     /**
  54.      * {@inheritdoc}
  55.      */
  56.     public function getRouteCollection(): RouteCollection
  57.     {
  58.         return $this->router->getRouteCollection();
  59.     }
  60.     /**
  61.      * {@inheritdoc}
  62.      */
  63.     public function match($pathInfo): array
  64.     {
  65.         $baseContext $this->router->getContext();
  66.         $baseUrl $baseContext->getBaseUrl();
  67.         if ($baseUrl === substr($pathInfo0\strlen($baseUrl))) {
  68.             $pathInfo substr($pathInfo\strlen($baseUrl));
  69.         }
  70.         $request Request::create($pathInfo'GET', [], [], [], ['HTTP_HOST' => $baseContext->getHost()]);
  71.         try {
  72.             $context = (new RequestContext())->fromRequest($request);
  73.         } catch (RequestExceptionInterface $e) {
  74.             throw new ResourceNotFoundException('Invalid request context.');
  75.         }
  76.         $context->setPathInfo($pathInfo);
  77.         $context->setScheme($baseContext->getScheme());
  78.         $context->setHost($baseContext->getHost());
  79.         try {
  80.             $this->router->setContext($context);
  81.             return $this->router->match($request->getPathInfo());
  82.         } finally {
  83.             $this->router->setContext($baseContext);
  84.         }
  85.     }
  86.     /**
  87.      * {@inheritdoc}
  88.      */
  89.     public function generate($name$parameters = [], $referenceType null): string
  90.     {
  91.         return $this->router->generate($name$parametersself::CONST_MAP[$referenceType ?? $this->urlGenerationStrategy]);
  92.     }
  93. }
  94. class_alias(Router::class, \ApiPlatform\Core\Bridge\Symfony\Routing\Router::class);