vendor/api-platform/core/src/Symfony/Routing/ApiLoader.php line 74

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\Core\Api\IdentifiersExtractorInterface;
  13. use ApiPlatform\Core\Api\OperationType;
  14. use ApiPlatform\Core\Bridge\Symfony\Routing\RouteNameGenerator;
  15. use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
  16. use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
  17. use ApiPlatform\Core\Operation\Factory\SubresourceOperationFactoryInterface;
  18. use ApiPlatform\Exception\InvalidResourceException;
  19. use ApiPlatform\Exception\RuntimeException;
  20. use ApiPlatform\Metadata\CollectionOperationInterface;
  21. use ApiPlatform\Metadata\HttpOperation;
  22. use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
  23. use ApiPlatform\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
  24. use ApiPlatform\PathResolver\OperationPathResolverInterface;
  25. use Symfony\Component\Config\FileLocator;
  26. use Symfony\Component\Config\Loader\Loader;
  27. use Symfony\Component\Config\Resource\DirectoryResource;
  28. use Symfony\Component\DependencyInjection\ContainerInterface;
  29. use Symfony\Component\HttpKernel\KernelInterface;
  30. use Symfony\Component\Routing\Loader\XmlFileLoader;
  31. use Symfony\Component\Routing\Route;
  32. use Symfony\Component\Routing\RouteCollection;
  33. /**
  34.  * Loads Resources.
  35.  *
  36.  * @author Kévin Dunglas <dunglas@gmail.com>
  37.  */
  38. final class ApiLoader extends Loader
  39. {
  40.     /**
  41.      * @deprecated since version 2.1, to be removed in 3.0. Use {@see RouteNameGenerator::ROUTE_NAME_PREFIX} instead.
  42.      */
  43.     public const ROUTE_NAME_PREFIX 'api_';
  44.     public const DEFAULT_ACTION_PATTERN 'api_platform.action.';
  45.     private $fileLoader;
  46.     private $resourceNameCollectionFactory;
  47.     private $resourceMetadataFactory;
  48.     private $operationPathResolver;
  49.     private $container;
  50.     private $formats;
  51.     private $resourceClassDirectories;
  52.     private $subresourceOperationFactory;
  53.     private $graphqlEnabled;
  54.     private $graphiQlEnabled;
  55.     private $graphQlPlaygroundEnabled;
  56.     private $entrypointEnabled;
  57.     private $docsEnabled;
  58.     private $identifiersExtractor;
  59.     public function __construct(KernelInterface $kernelResourceNameCollectionFactoryInterface $resourceNameCollectionFactory$resourceMetadataFactoryOperationPathResolverInterface $operationPathResolverContainerInterface $container, array $formats, array $resourceClassDirectories = [], SubresourceOperationFactoryInterface $subresourceOperationFactory nullbool $graphqlEnabled falsebool $entrypointEnabled truebool $docsEnabled truebool $graphiQlEnabled falsebool $graphQlPlaygroundEnabled falseIdentifiersExtractorInterface $identifiersExtractor null)
  60.     {
  61.         /** @var string[]|string $paths */
  62.         $paths $kernel->locateResource('@ApiPlatformBundle/Resources/config/routing');
  63.         $this->fileLoader = new XmlFileLoader(new FileLocator($paths));
  64.         $this->resourceNameCollectionFactory $resourceNameCollectionFactory;
  65.         if ($resourceMetadataFactory instanceof ResourceMetadataFactoryInterface) {
  66.             trigger_deprecation('api-platform/core''2.7'sprintf('Use "%s" instead of "%s".'ResourceMetadataCollectionFactoryInterface::class, ResourceMetadataFactoryInterface::class));
  67.         }
  68.         $this->resourceMetadataFactory $resourceMetadataFactory;
  69.         $this->operationPathResolver $operationPathResolver;
  70.         $this->container $container;
  71.         $this->formats $formats;
  72.         $this->resourceClassDirectories $resourceClassDirectories;
  73.         $this->subresourceOperationFactory $subresourceOperationFactory;
  74.         $this->graphqlEnabled $graphqlEnabled;
  75.         $this->graphiQlEnabled $graphiQlEnabled;
  76.         $this->graphQlPlaygroundEnabled $graphQlPlaygroundEnabled;
  77.         $this->entrypointEnabled $entrypointEnabled;
  78.         $this->docsEnabled $docsEnabled;
  79.         $this->identifiersExtractor $identifiersExtractor;
  80.     }
  81.     public function load($data$type null): RouteCollection
  82.     {
  83.         $routeCollection = new RouteCollection();
  84.         foreach ($this->resourceClassDirectories as $directory) {
  85.             $routeCollection->addResource(new DirectoryResource($directory'/\.php$/'));
  86.         }
  87.         $this->loadExternalFiles($routeCollection);
  88.         foreach ($this->resourceNameCollectionFactory->create() as $resourceClass) {
  89.             if ($this->resourceMetadataFactory instanceof ResourceMetadataFactoryInterface) {
  90.                 $this->loadLegacyMetadata($routeCollection$resourceClass);
  91.                 $this->loadLegacySubresources($routeCollection$resourceClass);
  92.                 continue;
  93.             }
  94.             foreach ($this->resourceMetadataFactory->create($resourceClass) as $resourceMetadata) {
  95.                 foreach ($resourceMetadata->getOperations() as $operationName => $operation) {
  96.                     if ($operation->getRouteName()) {
  97.                         continue;
  98.                     }
  99.                     if (SkolemIriConverter::$skolemUriTemplate === $operation->getUriTemplate()) {
  100.                         continue;
  101.                     }
  102.                     $legacyDefaults = [];
  103.                     if ($operation->getExtraProperties()['is_legacy_subresource'] ?? false) {
  104.                         $legacyDefaults['_api_subresource_operation_name'] = $operationName;
  105.                         $legacyDefaults['_api_subresource_context'] = [
  106.                             'property' => $operation->getExtraProperties()['legacy_subresource_property'],
  107.                             'identifiers' => $operation->getExtraProperties()['legacy_subresource_identifiers'],
  108.                             'collection' => $operation instanceof CollectionOperationInterface,
  109.                             'operationId' => $operation->getExtraProperties()['legacy_subresource_operation_name'] ?? null,
  110.                         ];
  111.                         $legacyDefaults['_api_identifiers'] = $operation->getExtraProperties()['legacy_subresource_identifiers'];
  112.                     } elseif ($operation->getExtraProperties()['is_legacy_resource_metadata'] ?? false) {
  113.                         $legacyDefaults[sprintf('_api_%s_operation_name'$operation instanceof CollectionOperationInterface OperationType::COLLECTION OperationType::ITEM)] = $operationName;
  114.                         $legacyDefaults['_api_identifiers'] = [];
  115.                         // Legacy identifiers
  116.                         $hasCompositeIdentifier false;
  117.                         foreach ($operation->getUriVariables() ?? [] as $parameterName => $identifiedBy) {
  118.                             $hasCompositeIdentifier $identifiedBy->getCompositeIdentifier();
  119.                             foreach ($identifiedBy->getIdentifiers() ?? [] as $identifier) {
  120.                                 $legacyDefaults['_api_identifiers'][] = $identifier;
  121.                             }
  122.                         }
  123.                         $legacyDefaults['_api_has_composite_identifier'] = $hasCompositeIdentifier;
  124.                     }
  125.                     $path = ($operation->getRoutePrefix() ?? '').$operation->getUriTemplate();
  126.                     foreach ($operation->getUriVariables() ?? [] as $parameterName => $link) {
  127.                         if (!$expandedValue $link->getExpandedValue()) {
  128.                             continue;
  129.                         }
  130.                         $path str_replace(sprintf('{%s}'$parameterName), $expandedValue$path);
  131.                     }
  132.                     $route = new Route(
  133.                         $path,
  134.                         $legacyDefaults + [
  135.                             '_controller' => $operation->getController() ?? 'api_platform.action.placeholder',
  136.                             '_format' => null,
  137.                             '_stateless' => $operation->getStateless(),
  138.                             '_api_resource_class' => $resourceClass,
  139.                             '_api_operation_name' => $operationName,
  140.                         ] + ($operation->getDefaults() ?? []),
  141.                         $operation->getRequirements() ?? [],
  142.                         $operation->getOptions() ?? [],
  143.                         $operation->getHost() ?? '',
  144.                         $operation->getSchemes() ?? [],
  145.                         [$operation->getMethod() ?? HttpOperation::METHOD_GET],
  146.                         $operation->getCondition() ?? ''
  147.                     );
  148.                     $routeCollection->add($operationName$route);
  149.                 }
  150.             }
  151.         }
  152.         return $routeCollection;
  153.     }
  154.     public function supports($resource$type null): bool
  155.     {
  156.         return 'api_platform' === $type;
  157.     }
  158.     /**
  159.      * Load external files.
  160.      */
  161.     private function loadExternalFiles(RouteCollection $routeCollection): void
  162.     {
  163.         $routeCollection->addCollection($this->fileLoader->load('genid.xml'));
  164.         if ($this->entrypointEnabled) {
  165.             $routeCollection->addCollection($this->fileLoader->load('api.xml'));
  166.         }
  167.         if ($this->docsEnabled) {
  168.             $routeCollection->addCollection($this->fileLoader->load('docs.xml'));
  169.         }
  170.         if ($this->graphqlEnabled) {
  171.             $graphqlCollection $this->fileLoader->load('graphql/graphql.xml');
  172.             $graphqlCollection->addDefaults(['_graphql' => true]);
  173.             $routeCollection->addCollection($graphqlCollection);
  174.         }
  175.         if ($this->graphiQlEnabled) {
  176.             $graphiQlCollection $this->fileLoader->load('graphql/graphiql.xml');
  177.             $graphiQlCollection->addDefaults(['_graphql' => true]);
  178.             $routeCollection->addCollection($graphiQlCollection);
  179.         }
  180.         if ($this->graphQlPlaygroundEnabled) {
  181.             $graphQlPlaygroundCollection $this->fileLoader->load('graphql/graphql_playground.xml');
  182.             $graphQlPlaygroundCollection->addDefaults(['_graphql' => true]);
  183.             $routeCollection->addCollection($graphQlPlaygroundCollection);
  184.         }
  185.         if (isset($this->formats['jsonld'])) {
  186.             $routeCollection->addCollection($this->fileLoader->load('jsonld.xml'));
  187.         }
  188.     }
  189.     /**
  190.      * TODO: remove in 3.0.
  191.      */
  192.     private function loadLegacyMetadata(RouteCollection $routeCollectionstring $resourceClass)
  193.     {
  194.         $resourceMetadata $this->resourceMetadataFactory->create($resourceClass);
  195.         $resourceShortName $resourceMetadata->getShortName();
  196.         if (null === $resourceShortName) {
  197.             throw new InvalidResourceException(sprintf('Resource %s has no short name defined.'$resourceClass));
  198.         }
  199.         if (null !== $collectionOperations $resourceMetadata->getCollectionOperations()) {
  200.             foreach ($collectionOperations as $operationName => $operation) {
  201.                 $this->addRoute($routeCollection$resourceClass$operationName$operation$resourceMetadataOperationType::COLLECTION);
  202.             }
  203.         }
  204.         if (null !== $itemOperations $resourceMetadata->getItemOperations()) {
  205.             foreach ($itemOperations as $operationName => $operation) {
  206.                 $this->addRoute($routeCollection$resourceClass$operationName$operation$resourceMetadataOperationType::ITEM);
  207.             }
  208.         }
  209.     }
  210.     /**
  211.      * TODO: remove in 3.0.
  212.      */
  213.     private function loadLegacySubresources(RouteCollection $routeCollectionstring $resourceClass)
  214.     {
  215.         if (null === $this->subresourceOperationFactory) {
  216.             return;
  217.         }
  218.         foreach ($this->subresourceOperationFactory->create($resourceClass) as $operationId => $operation) {
  219.             if (null === $controller $operation['controller'] ?? null) {
  220.                 $controller self::DEFAULT_ACTION_PATTERN.'get_subresource';
  221.                 if (!$this->container->has($controller)) {
  222.                     throw new RuntimeException(sprintf('There is no builtin action for the %s %s operation. You need to define the controller yourself.'OperationType::SUBRESOURCE'GET'));
  223.                 }
  224.             }
  225.             $routeCollection->add($operation['route_name'], new Route(
  226.                 $operation['path'],
  227.                 [
  228.                     '_controller' => $controller,
  229.                     '_format' => $operation['defaults']['_format'] ?? null,
  230.                     '_stateless' => $operation['stateless'] ?? null,
  231.                     '_api_resource_class' => $operation['resource_class'],
  232.                     '_api_identifiers' => $operation['identifiers'],
  233.                     '_api_has_composite_identifier' => false,
  234.                     '_api_subresource_operation_name' => $operation['route_name'],
  235.                     '_api_subresource_context' => [
  236.                         'property' => $operation['property'],
  237.                         'identifiers' => $operation['identifiers'],
  238.                         'collection' => $operation['collection'],
  239.                         'operationId' => $operationId,
  240.                     ],
  241.                 ] + ($operation['defaults'] ?? []),
  242.                 $operation['requirements'] ?? [],
  243.                 $operation['options'] ?? [],
  244.                 $operation['host'] ?? '',
  245.                 $operation['schemes'] ?? [],
  246.                 ['GET'],
  247.                 $operation['condition'] ?? ''
  248.             ));
  249.         }
  250.     }
  251.     /**
  252.      * Creates and adds a route for the given operation to the route collection.
  253.      *
  254.      * @throws RuntimeException
  255.      */
  256.     private function addRoute(RouteCollection $routeCollectionstring $resourceClassstring $operationName, array $operationResourceMetadata $resourceMetadatastring $operationType): void
  257.     {
  258.         $resourceShortName $resourceMetadata->getShortName();
  259.         if (isset($operation['route_name'])) {
  260.             if (!isset($operation['method'])) {
  261.                 @trigger_error(sprintf('Not setting the "method" attribute is deprecated and will not be supported anymore in API Platform 3.0, set it for the %s operation "%s" of the class "%s".'OperationType::COLLECTION === $operationType 'collection' 'item'$operationName$resourceClass), \E_USER_DEPRECATED);
  262.             }
  263.             return;
  264.         }
  265.         if (!isset($operation['method'])) {
  266.             throw new RuntimeException(sprintf('Either a "route_name" or a "method" operation attribute must exist for the operation "%s" of the resource "%s".'$operationName$resourceClass));
  267.         }
  268.         if (null === $controller $operation['controller'] ?? null) {
  269.             $controller sprintf('%s%s_%s'self::DEFAULT_ACTION_PATTERNstrtolower($operation['method']), $operationType);
  270.             if (!$this->container->has($controller)) {
  271.                 throw new RuntimeException(sprintf('There is no builtin action for the %s %s operation. You need to define the controller yourself.'$operationType$operation['method']));
  272.             }
  273.         }
  274.         if ($resourceMetadata->getItemOperations()) {
  275.             $operation['identifiers'] = (array) ($operation['identifiers'] ?? $resourceMetadata->getAttribute('identifiers'$this->identifiersExtractor $this->identifiersExtractor->getIdentifiersFromResourceClass($resourceClass) : ['id']));
  276.         }
  277.         $operation['has_composite_identifier'] = isset($operation['identifiers']) && \count($operation['identifiers']) > $resourceMetadata->getAttribute('composite_identifier'true) : false;
  278.         $path trim(trim($resourceMetadata->getAttribute('route_prefix''')), '/');
  279.         $path .= $this->operationPathResolver->resolveOperationPath($resourceShortName$operation$operationType$operationName);
  280.         $route = new Route(
  281.             $path,
  282.             [
  283.                 '_controller' => $controller,
  284.                 '_format' => $operation['defaults']['_format'] ?? null,
  285.                 '_stateless' => $operation['stateless'] ?? null,
  286.                 '_api_resource_class' => $resourceClass,
  287.                 '_api_identifiers' => $operation['identifiers'] ?? [],
  288.                 '_api_has_composite_identifier' => $operation['has_composite_identifier'] ?? true,
  289.                 '_api_exception_to_status' => $operation['exception_to_status'] ?? $resourceMetadata->getAttribute('exception_to_status') ?? [],
  290.                 '_api_operation_name' => RouteNameGenerator::generate($operationName$resourceShortName$operationType),
  291.                 sprintf('_api_%s_operation_name'$operationType) => $operationName,
  292.             ] + ($operation['defaults'] ?? []),
  293.             $operation['requirements'] ?? [],
  294.             $operation['options'] ?? [],
  295.             $operation['host'] ?? '',
  296.             $operation['schemes'] ?? [],
  297.             [$operation['method']],
  298.             $operation['condition'] ?? ''
  299.         );
  300.         $routeCollection->add(RouteNameGenerator::generate($operationName$resourceShortName$operationType), $route);
  301.     }
  302. }
  303. class_alias(ApiLoader::class, \ApiPlatform\Core\Bridge\Symfony\Routing\ApiLoader::class);