Skip to content

Commit 80cb31b

Browse files
soyukaalanpoulain
authored andcommitted
refactor: api loader
1 parent e12c44e commit 80cb31b

File tree

2 files changed

+13
-47
lines changed

2 files changed

+13
-47
lines changed

src/Symfony/Routing/ApiLoader.php

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace ApiPlatform\Symfony\Routing;
1515

16+
use ApiPlatform\Exception\RuntimeException;
1617
use ApiPlatform\Metadata\CollectionOperationInterface;
1718
use ApiPlatform\Metadata\HttpOperation;
1819
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
@@ -33,10 +34,6 @@
3334
*/
3435
final class ApiLoader extends Loader
3536
{
36-
/**
37-
* @deprecated since version 2.1, to be removed in 3.0. Use {@see RouteNameGenerator::ROUTE_NAME_PREFIX} instead.
38-
*/
39-
public const ROUTE_NAME_PREFIX = 'api_';
4037
public const DEFAULT_ACTION_PATTERN = 'api_platform.action.';
4138

4239
private $fileLoader;
@@ -50,7 +47,6 @@ final class ApiLoader extends Loader
5047
private $graphQlPlaygroundEnabled;
5148
private $entrypointEnabled;
5249
private $docsEnabled;
53-
private $identifiersExtractor;
5450

5551
public function __construct(KernelInterface $kernel, ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory, ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory, ContainerInterface $container, array $formats, array $resourceClassDirectories = [], bool $graphqlEnabled = false, bool $entrypointEnabled = true, bool $docsEnabled = true, bool $graphiQlEnabled = false, bool $graphQlPlaygroundEnabled = false)
5652
{
@@ -87,31 +83,6 @@ public function load($data, $type = null): RouteCollection
8783
continue;
8884
}
8985

90-
// $legacyDefaults = [];
91-
92-
// if ($operation->getExtraProperties()['is_legacy_subresource'] ?? false) {
93-
// $legacyDefaults['_api_subresource_operation_name'] = $operationName;
94-
// $legacyDefaults['_api_subresource_context'] = [
95-
// 'property' => $operation->getExtraProperties()['legacy_subresource_property'],
96-
// 'identifiers' => $operation->getExtraProperties()['legacy_subresource_identifiers'],
97-
// 'collection' => $operation instanceof CollectionOperationInterface,
98-
// 'operationId' => $operation->getExtraProperties()['legacy_subresource_operation_name'] ?? null,
99-
// ];
100-
// $legacyDefaults['_api_identifiers'] = $operation->getExtraProperties()['legacy_subresource_identifiers'];
101-
// } elseif ($operation->getExtraProperties()['is_legacy_resource_metadata'] ?? false) {
102-
// $legacyDefaults[sprintf('_api_%s_operation_name', $operation instanceof CollectionOperationInterface ? OperationType::COLLECTION : OperationType::ITEM)] = $operationName;
103-
// $legacyDefaults['_api_identifiers'] = [];
104-
// // Legacy identifiers
105-
// $hasCompositeIdentifier = false;
106-
// foreach ($operation->getUriVariables() ?? [] as $parameterName => $identifiedBy) {
107-
// $hasCompositeIdentifier = $identifiedBy->getCompositeIdentifier();
108-
// foreach ($identifiedBy->getIdentifiers() ?? [] as $identifier) {
109-
// $legacyDefaults['_api_identifiers'][] = $identifier;
110-
// }
111-
// }
112-
// $legacyDefaults['_api_has_composite_identifier'] = $hasCompositeIdentifier;
113-
// }
114-
11586
$path = ($operation->getRoutePrefix() ?? '').$operation->getUriTemplate();
11687
foreach ($operation->getUriVariables() ?? [] as $parameterName => $link) {
11788
if (!$expandedValue = $link->getExpandedValue()) {
@@ -120,11 +91,17 @@ public function load($data, $type = null): RouteCollection
12091

12192
$path = str_replace(sprintf('{%s}', $parameterName), $expandedValue, $path);
12293
}
94+
95+
if ($controller = $operation->getController()) {
96+
if (!$this->container->has($controller)) {
97+
throw new RuntimeException(sprintf('There is no builtin action for the "%s" operation. You need to define the controller yourself.', $operationName));
98+
}
99+
}
123100

124101
$route = new Route(
125102
$path,
126103
[
127-
'_controller' => $operation->getController() ?? 'api_platform.action.placeholder',
104+
'_controller' => $controller ?? 'api_platform.action.placeholder',
128105
'_format' => null,
129106
'_stateless' => $operation->getStateless(),
130107
'_api_resource_class' => $resourceClass,

tests/Symfony/Routing/ApiLoaderTest.php

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@
1414
namespace ApiPlatform\Tests\Symfony\Routing;
1515

1616
use ApiPlatform\Core\Api\IdentifiersExtractorInterface;
17-
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
18-
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
19-
use ApiPlatform\Core\Metadata\Property\PropertyNameCollection;
20-
use ApiPlatform\Core\Operation\UnderscorePathSegmentNameGenerator;
17+
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
18+
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
19+
use ApiPlatform\Metadata\Property\PropertyNameCollection;
2120
use ApiPlatform\Metadata\ApiProperty;
2221
use ApiPlatform\Metadata\ApiResource;
2322
use ApiPlatform\Metadata\Delete;
@@ -30,8 +29,6 @@
3029
use ApiPlatform\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
3130
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
3231
use ApiPlatform\Metadata\Resource\ResourceNameCollection;
33-
use ApiPlatform\PathResolver\CustomOperationPathResolver;
34-
use ApiPlatform\PathResolver\OperationPathResolver;
3532
use ApiPlatform\Symfony\Routing\ApiLoader;
3633
use ApiPlatform\Tests\Fixtures\DummyEntity;
3734
use ApiPlatform\Tests\Fixtures\RelatedDummyEntity;
@@ -46,9 +43,6 @@
4643
/**
4744
* @author Antoine Bluchet <[email protected]>
4845
* @author Amrouche Hamza <[email protected]>
49-
*
50-
* TODO: in 3.0 just remove the IdentifiersExtractor
51-
* @group legacy
5246
*/
5347
class ApiLoaderTest extends TestCase
5448
{
@@ -255,6 +249,7 @@ private function getApiLoaderWithResourceMetadataCollection(ResourceMetadataColl
255249
$kernelProphecy = $this->prophesize(KernelInterface::class);
256250
$kernelProphecy->locateResource(Argument::any())->willReturn($routingConfig);
257251
$possibleArguments = [
252+
'some.service.name',
258253
'api_platform.action.get_collection',
259254
'api_platform.action.post_collection',
260255
'api_platform.action.get_item',
@@ -284,15 +279,9 @@ private function getApiLoaderWithResourceMetadataCollection(ResourceMetadataColl
284279
$propertyMetadataFactoryProphecy->create(RelatedDummyEntity::class, 'id')->willReturn(new ApiProperty());
285280
$propertyMetadataFactoryProphecy->create(DummyEntity::class, 'id')->willReturn(new ApiProperty());
286281

287-
$operationPathResolver = new CustomOperationPathResolver(new OperationPathResolver(new UnderscorePathSegmentNameGenerator()));
288-
289282
$resourceMetadataFactory = $resourceMetadataFactoryProphecy->reveal();
290283

291-
$identifiersExtractorProphecy = $this->prophesize(IdentifiersExtractorInterface::class);
292-
$identifiersExtractorProphecy->getIdentifiersFromResourceClass(Argument::type('string'))->willReturn(['id']);
293-
$identifiersExtractor = $identifiersExtractorProphecy->reveal();
294-
295-
return new ApiLoader($kernelProphecy->reveal(), $resourceNameCollectionFactoryProphecy->reveal(), $resourceMetadataFactory, $operationPathResolver, $containerProphecy->reveal(), ['jsonld' => ['application/ld+json']], [], null, false, true, true, false, false, $identifiersExtractor);
284+
return new ApiLoader($kernelProphecy->reveal(), $resourceNameCollectionFactoryProphecy->reveal(), $resourceMetadataFactory, $containerProphecy->reveal(), ['jsonld' => ['application/ld+json']], [], false, true, true, false, false);
296285
}
297286

298287
private function getRoute(string $path, string $controller, ?bool $stateless, string $resourceClass, array $identifiers, string $operationName, array $extraDefaults = [], array $methods = [], array $requirements = [], array $options = [], string $host = '', array $schemes = [], string $condition = ''): Route

0 commit comments

Comments
 (0)