vendor/api-platform/core/src/Core/Bridge/Symfony/Routing/RouteNameResolver.php line 43

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\Core\Bridge\Symfony\Routing;
  12. use ApiPlatform\Core\Api\OperationType;
  13. use ApiPlatform\Core\Api\OperationTypeDeprecationHelper;
  14. use ApiPlatform\Exception\InvalidArgumentException;
  15. use Symfony\Component\Routing\RouterInterface;
  16. /**
  17.  * @author Kévin Dunglas <dunglas@gmail.com>
  18.  */
  19. final class RouteNameResolver implements RouteNameResolverInterface
  20. {
  21.     private $router;
  22.     public function __construct(RouterInterface $router)
  23.     {
  24.         $this->router $router;
  25.     }
  26.     public function getRouteName(string $resourceClass$operationType /* , array $context = [] */): string
  27.     {
  28.         if (\func_num_args() > 2) {
  29.             $context func_get_arg(2);
  30.         } else {
  31.             $context = [];
  32.         }
  33.         $operationType OperationTypeDeprecationHelper::getOperationType($operationType);
  34.         foreach ($this->router->getRouteCollection()->all() as $routeName => $route) {
  35.             $currentResourceClass $route->getDefault('_api_resource_class');
  36.             $operation $route->getDefault(sprintf('_api_%s_operation_name'$operationType));
  37.             $methods $route->getMethods();
  38.             if ($resourceClass === $currentResourceClass && null !== $operation && (empty($methods) || \in_array('GET'$methodstrue))) {
  39.                 if (OperationType::SUBRESOURCE === $operationType && false === $this->isSameSubresource($context$route->getDefault('_api_subresource_context'))) {
  40.                     continue;
  41.                 }
  42.                 return $routeName;
  43.             }
  44.         }
  45.         throw new InvalidArgumentException(sprintf('No %s route associated with the type "%s".'$operationType$resourceClass));
  46.     }
  47.     private function isSameSubresource(array $context, array $currentContext): bool
  48.     {
  49.         $subresources array_keys($context['subresource_resources']);
  50.         $currentSubresources = [];
  51.         foreach ($currentContext['identifiers'] as [$class]) {
  52.             $currentSubresources[] = $class;
  53.         }
  54.         return $currentSubresources === $subresources;
  55.     }
  56. }