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

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.     public function setContext(RequestContext $context)
  40.     {
  41.         $this->router->setContext($context);
  42.     }
  43.     public function getContext(): RequestContext
  44.     {
  45.         return $this->router->getContext();
  46.     }
  47.     public function getRouteCollection(): RouteCollection
  48.     {
  49.         return $this->router->getRouteCollection();
  50.     }
  51.     public function match($pathInfo): array
  52.     {
  53.         $baseContext $this->router->getContext();
  54.         $baseUrl $baseContext->getBaseUrl();
  55.         if ($baseUrl === substr($pathInfo0, \strlen($baseUrl))) {
  56.             $pathInfo substr($pathInfo, \strlen($baseUrl));
  57.         }
  58.         $request Request::create($pathInfo'GET', [], [], [], ['HTTP_HOST' => $baseContext->getHost()]);
  59.         try {
  60.             $context = (new RequestContext())->fromRequest($request);
  61.         } catch (RequestExceptionInterface $e) {
  62.             throw new ResourceNotFoundException('Invalid request context.');
  63.         }
  64.         $context->setPathInfo($pathInfo);
  65.         $context->setScheme($baseContext->getScheme());
  66.         $context->setHost($baseContext->getHost());
  67.         try {
  68.             $this->router->setContext($context);
  69.             return $this->router->match($request->getPathInfo());
  70.         } finally {
  71.             $this->router->setContext($baseContext);
  72.         }
  73.     }
  74.     public function generate($name$parameters = [], $referenceType null): string
  75.     {
  76.         return $this->router->generate($name$parametersself::CONST_MAP[$referenceType ?? $this->urlGenerationStrategy]);
  77.     }
  78. }
  79. class_alias(Router::class, \ApiPlatform\Core\Bridge\Symfony\Routing\Router::class);