Skip to content

Commit ee9bd7f

Browse files
author
abluchet
committed
Rename path name generator + test all the things
1 parent 8f8e3af commit ee9bd7f

23 files changed

+407
-68
lines changed

src/Bridge/Symfony/Bundle/DependencyInjection/Configuration.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,15 @@ public function getConfigTreeBuilder()
4343
->scalarNode('title')->defaultValue('')->info('The title of the API.')->end()
4444
->scalarNode('description')->defaultValue('')->info('The description of the API.')->end()
4545
->scalarNode('version')->defaultValue('0.0.0')->info('The version of the API.')->end()
46-
->scalarNode('default_operation_path_resolver')->defaultValue('api_platform.operation_path_resolver.underscore')->info('[Deprecated] Specify the default operation path resolver to use for generating resources operations path.')->end()
46+
->scalarNode('default_operation_path_resolver')
47+
->beforeNormalization()->always(function ($v) {
48+
if (isset($v['default_operation_path_resolver'])) {
49+
@trigger_error('The use of the `default_operation_path_resolver` has been deprecated in 2.1 and will be removed in 3.0. Use `path_segment_name_generator` instead.', E_USER_DEPRECATED);
50+
}
51+
})->end()
52+
->defaultValue('api_platform.operation_path_resolver.underscore')->info('Specify the default operation path resolver to use for generating resources operations path.')->end()
4753
->scalarNode('name_converter')->defaultNull()->info('Specify a name converter to use.')->end()
48-
->scalarNode('path_name_generator')->defaultValue('api_platform.path_name_generator.underscore')->info('Specify a path name generator to use.')->end()
54+
->scalarNode('path_segment_name_generator')->defaultValue('api_platform.path_segment_name_generator.underscore')->info('Specify a path name generator to use.')->end()
4955
->scalarNode('api_resources_directory')->defaultValue('Entity')->info('The name of the directory within the bundles that contains the api resources.')->end()
5056
->arrayNode('eager_loading')
5157
->canBeDisabled()

src/Bridge/Symfony/Bundle/Resources/config/api.xml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,27 +94,27 @@
9494
</service>
9595

9696
<service id="api_platform.operation_path_resolver.custom" class="ApiPlatform\Core\PathResolver\CustomOperationPathResolver" public="false">
97-
<!-- @TODO should be "api_platform.operation_path_resolver.generator" when the default is removed -->
97+
<!-- should be "api_platform.operation_path_resolver.generator" when the default is removed -->
9898
<argument type="service" id="api_platform.operation_path_resolver.default" />
9999
</service>
100100

101101
<service id="api_platform.operation_path_resolver.generator" class="ApiPlatform\Core\PathResolver\OperationPathResolver" public="false">
102-
<argument type="service" id="api_platform.path_name_generator" />
102+
<argument type="service" id="api_platform.path_segment_name_generator" />
103103
</service>
104104

105105
<service id="api_platform.operation_path_resolver.underscore" class="ApiPlatform\Core\PathResolver\UnderscoreOperationPathResolver" public="false">
106-
<deprecated>The "%service_id%" service is deprecated since ApiPlatform 2.1 and will be removed in 3.0. Use PathNameGenerator instead.</deprecated>
106+
<deprecated>The "%service_id%" service is deprecated since ApiPlatform 2.1 and will be removed in 3.0. Use PathSegmentNameGenerator instead.</deprecated>
107107
</service>
108108

109109
<service id="api_platform.operation_path_resolver.dash" class="ApiPlatform\Core\PathResolver\DashOperationPathResolver" public="false">
110-
<deprecated>The "%service_id%" service is deprecated since ApiPlatform 2.1 and will be removed in 3.0. Use PathNameGenerator instead.</deprecated>
110+
<deprecated>The "%service_id%" service is deprecated since ApiPlatform 2.1 and will be removed in 3.0. Use PathSegmentNameGenerator instead.</deprecated>
111111
</service>
112112

113113
<!-- Path name generator -->
114114

115-
<service id="api_platform.path_name_generator" alias="api_platform.path_name_generator.underscore" public="false" />
116-
<service id="api_platform.path_name_generator.underscore" class="ApiPlatform\Core\Operation\UnderscorePathNameGenerator" public="false" />
117-
<service id="api_platform.path_name_generator.dash" class="ApiPlatform\Core\Operation\DashPathNameGenerator" public="false" />
115+
<service id="api_platform.path_segment_name_generator" alias="api_platform.path_segment_name_generator.underscore" public="false" />
116+
<service id="api_platform.path_segment_name_generator.underscore" class="ApiPlatform\Core\Operation\UnderscorePathSegmentNameGenerator" public="false" />
117+
<service id="api_platform.path_segment_name_generator.dash" class="ApiPlatform\Core\Operation\DashPathSegmentNameGenerator" public="false" />
118118

119119
<!-- Event listeners -->
120120

@@ -216,7 +216,7 @@
216216
<argument type="service" id="api_platform.metadata.resource.metadata_factory" />
217217
<argument type="service" id="api_platform.metadata.property.name_collection_factory" />
218218
<argument type="service" id="api_platform.metadata.property.metadata_factory" />
219-
<argument type="service" id="api_platform.path_name_generator" />
219+
<argument type="service" id="api_platform.path_segment_name_generator" />
220220
</service>
221221

222222
<service id="api_platform.subresource_operation_factory.cached" class="ApiPlatform\Core\Operation\Factory\CachedSubresourceOperationFactory" decorates="api_platform.subresource_operation_factory" decoration-priority="-10" public="false">

src/Bridge/Symfony/Routing/ApiLoader.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ final class ApiLoader extends Loader
5151
private $container;
5252
private $formats;
5353
private $resourceClassDirectories;
54+
private $subresourceOperationFactory;
5455

5556
public function __construct(KernelInterface $kernel, ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory, ResourceMetadataFactoryInterface $resourceMetadataFactory, OperationPathResolverInterface $operationPathResolver, ContainerInterface $container, array $formats, array $resourceClassDirectories = [], SubresourceOperationFactoryInterface $subresourceOperationFactory = null)
5657
{

src/Bridge/Symfony/Routing/RouteNameGenerator.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
*
2626
* @author Baptiste Meyer <[email protected]>
2727
*/
28-
class RouteNameGenerator
28+
final class RouteNameGenerator
2929
{
3030
const ROUTE_NAME_PREFIX = 'api_';
3131

@@ -39,7 +39,6 @@ private function __construct()
3939
* @param string $operationName
4040
* @param string $resourceShortName
4141
* @param string|bool $operationType
42-
* @param array $subresourceContext
4342
*
4443
* @throws InvalidArgumentException
4544
*

src/Bridge/Symfony/Routing/RouterOperationPathResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function resolveOperationPath(string $resourceShortName, array $operation
5656
throw new InvalidArgumentException('Subresource operations are not supported by the RouterOperationPathResolver.');
5757
} else {
5858
if (null !== $operationName) {
59-
$routeName = RouteNameGenerator::generate($operationName, $resourceShortName, $operationType, $operation);
59+
$routeName = RouteNameGenerator::generate($operationName, $resourceShortName, $operationType);
6060
} else {
6161
return $this->deferred->resolveOperationPath($resourceShortName, $operation, OperationTypeDeprecationHelper::getOperationType($operationType), $operationName);
6262
}

src/Operation/DashPathNameGenerator.php renamed to src/Operation/DashPathSegmentNameGenerator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020
*
2121
* @author Antoine Bluchet <[email protected]>
2222
*/
23-
class DashPathNameGenerator implements PathNameGeneratorInterface
23+
final class DashPathSegmentNameGenerator implements PathSegmentNameGeneratorInterface
2424
{
2525
/**
2626
* {@inheritdoc}
2727
*/
28-
public function getPathName(string $name, bool $pluralize = true): string
28+
public function getSegmentName(string $name, bool $pluralize = true): string
2929
{
3030
$name = $this->dashize($name);
3131

src/Operation/Factory/CachedSubresourceOperationFactory.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function __construct(CacheItemPoolInterface $cacheItemPool, SubresourceOp
3737
*/
3838
public function create(string $resourceClass): array
3939
{
40-
$cacheKey = self::CACHE_KEY_PREFIX.md5(serialize([$resourceClass]));
40+
$cacheKey = self::CACHE_KEY_PREFIX.str_replace('\\', '', $resourceClass);
4141

4242
try {
4343
$cacheItem = $this->cacheItemPool->getItem($cacheKey);
@@ -46,15 +46,11 @@ public function create(string $resourceClass): array
4646
return $cacheItem->get();
4747
}
4848
} catch (CacheException $e) {
49-
// do nothing
49+
return $this->decorated->create($resourceClass);
5050
}
5151

5252
$subresourceOperations = $this->decorated->create($resourceClass);
5353

54-
if (!isset($cacheItem)) {
55-
return $subresourceOperations;
56-
}
57-
5854
$cacheItem->set($subresourceOperations);
5955
$this->cacheItemPool->save($cacheItem);
6056

src/Operation/Factory/SubresourceOperationFactory.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
1818
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
1919
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
20-
use ApiPlatform\Core\Operation\PathNameGeneratorInterface;
20+
use ApiPlatform\Core\Operation\PathSegmentNameGeneratorInterface;
2121

2222
/**
2323
* @internal
@@ -27,12 +27,17 @@ final class SubresourceOperationFactory implements SubresourceOperationFactoryIn
2727
const SUBRESOURCE_SUFFIX = '_subresource';
2828
const FORMAT_SUFFIX = '.{_format}';
2929

30-
public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, PathNameGeneratorInterface $pathNameGenerator)
30+
private $resourceMetadataFactory;
31+
private $propertyNameCollectionFactory;
32+
private $propertyMetadataFactory;
33+
private $pathSegmentNameGenerator;
34+
35+
public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, PathSegmentNameGeneratorInterface $pathSegmentNameGenerator)
3136
{
3237
$this->resourceMetadataFactory = $resourceMetadataFactory;
3338
$this->propertyNameCollectionFactory = $propertyNameCollectionFactory;
3439
$this->propertyMetadataFactory = $propertyMetadataFactory;
35-
$this->pathNameGenerator = $pathNameGenerator;
40+
$this->pathSegmentNameGenerator = $pathSegmentNameGenerator;
3641
}
3742

3843
/**
@@ -78,7 +83,7 @@ private function computeSubresourceOperations(string $resourceClass, array &$tre
7883
$visiting = "{$parentOperation['property']}-{$parentOperation['resource_class']}-$property-$subresourceClass";
7984

8085
foreach ($parentOperation['identifiers'] as $key => list($param, $class)) {
81-
$prefix .= 0 === $key ? "$class" : "-$param-$class";
86+
$prefix .= 0 === $key ? $class : "-$param-$class";
8287
}
8388

8489
if (false !== strpos($prefix, $visiting)) {
@@ -111,8 +116,8 @@ private function computeSubresourceOperations(string $resourceClass, array &$tre
111116

112117
$operation['path'] = sprintf(
113118
'/%s/{id}/%s%s',
114-
$this->pathNameGenerator->getPathName($rootShortname, true),
115-
$this->pathNameGenerator->getPathName($operation['property'], $operation['collection']),
119+
$this->pathSegmentNameGenerator->getSegmentName($rootShortname, true),
120+
$this->pathSegmentNameGenerator->getSegmentName($operation['property'], $operation['collection']),
116121
self::FORMAT_SUFFIX
117122
);
118123

@@ -126,7 +131,7 @@ private function computeSubresourceOperations(string $resourceClass, array &$tre
126131

127132
$operation['path'] = str_replace(self::FORMAT_SUFFIX, '', $parentOperation['path']);
128133
list($key) = end($operation['identifiers']);
129-
$operation['path'] .= sprintf('/{%s}/%s%s', $key, $this->pathNameGenerator->getPathName($property, $operation['collection']), self::FORMAT_SUFFIX);
134+
$operation['path'] .= sprintf('/{%s}/%s%s', $key, $this->pathSegmentNameGenerator->getSegmentName($property, $operation['collection']), self::FORMAT_SUFFIX);
130135
}
131136

132137
$tree[$visiting] = $operation;

src/Operation/PathNameGeneratorInterface.php renamed to src/Operation/PathSegmentNameGeneratorInterface.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,19 @@
1414
namespace ApiPlatform\Core\Operation;
1515

1616
/**
17-
* Generate a path name according to a string and whether it needs pluralization.
17+
* Generates a path name according to a string and whether it needs pluralization.
1818
*
1919
* @author Antoine Bluchet <[email protected]>
2020
*/
21-
interface PathNameGeneratorInterface
21+
interface PathSegmentNameGeneratorInterface
2222
{
2323
/**
24-
* Transforms a given string to a tableized, pluralized string.
24+
* Transforms a given string to a valid path name which can be pluralized (eg. for collections).
2525
*
26-
* @param string $name usually a ResourceMetadata shortname
26+
* @param string $name usually a ResourceMetadata shortname
27+
* @param bool $pluralize
2728
*
2829
* @return string A string that is a part of the route name
2930
*/
30-
public function getPathName(string $name, bool $pluralize = true): string;
31+
public function getSegmentName(string $name, bool $pluralize = true): string;
3132
}

src/Operation/UnderscorePathNameGenerator.php renamed to src/Operation/UnderscorePathSegmentNameGenerator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020
*
2121
* @author Antoine Bluchet <[email protected]>
2222
*/
23-
class UnderscorePathNameGenerator implements PathNameGeneratorInterface
23+
final class UnderscorePathSegmentNameGenerator implements PathSegmentNameGeneratorInterface
2424
{
2525
/**
2626
* {@inheritdoc}
2727
*/
28-
public function getPathName(string $name, bool $pluralize = true): string
28+
public function getSegmentName(string $name, bool $pluralize = true): string
2929
{
3030
$name = Inflector::tableize($name);
3131

src/PathResolver/DashOperationPathResolver.php

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

1414
namespace ApiPlatform\Core\PathResolver;
1515

16-
use ApiPlatform\Core\PathResolver\OperationPathResolver;
17-
use ApiPlatform\Core\Operation\DashPathNameGenerator;
16+
use ApiPlatform\Core\Operation\DashPathSegmentNameGenerator;
1817

1918
/**
2019
* Generates a path with words separated by underscores.
@@ -28,16 +27,14 @@ final class DashOperationPathResolver implements OperationPathResolverInterface
2827
*/
2928
public function resolveOperationPath(string $resourceShortName, array $operation, $operationType/*, string $operationName = null*/): string
3029
{
31-
@trigger_error(sprintf('The use of %s is deprecated since 2.1. Please use PathNameGenerator instead.', __CLASS__), E_USER_DEPRECATED);
30+
@trigger_error(sprintf('The use of %s is deprecated since 2.1. Please use PathSegmentNameGenerator instead.', __CLASS__), E_USER_DEPRECATED);
3231

3332
if (func_num_args() >= 4) {
3433
$operationName = func_get_arg(3);
3534
} else {
36-
@trigger_error(sprintf('Method %s() will have a 4th `string $operationName` argument in version 3.0. Not defining it is deprecated since 2.1.', __METHOD__), E_USER_DEPRECATED);
37-
3835
$operationName = null;
3936
}
4037

41-
return (new OperationPathResolver(new DashPathNameGenerator()))->resolveOperationPath($resourceShortName, $operation, $operationType, $operationName);
38+
return (new OperationPathResolver(new DashPathSegmentNameGenerator()))->resolveOperationPath($resourceShortName, $operation, $operationType, $operationName);
4239
}
4340
}

src/PathResolver/OperationPathResolver.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use ApiPlatform\Core\Api\OperationType;
1717
use ApiPlatform\Core\Api\OperationTypeDeprecationHelper;
1818
use ApiPlatform\Core\Exception\InvalidArgumentException;
19-
use ApiPlatform\Core\Operation\PathNameGeneratorInterface;
19+
use ApiPlatform\Core\Operation\PathSegmentNameGeneratorInterface;
2020

2121
/**
2222
* Generates an operation path.
@@ -25,11 +25,11 @@
2525
*/
2626
final class OperationPathResolver implements OperationPathResolverInterface
2727
{
28-
private $pathNameGenerator;
28+
private $pathSegmentNameGenerator;
2929

30-
public function __construct(PathNameGeneratorInterface $pathNameGenerator)
30+
public function __construct(PathSegmentNameGeneratorInterface $pathSegmentNameGenerator)
3131
{
32-
$this->pathNameGenerator = $pathNameGenerator;
32+
$this->pathSegmentNameGenerator = $pathSegmentNameGenerator;
3333
}
3434

3535
/**
@@ -47,7 +47,7 @@ public function resolveOperationPath(string $resourceShortName, array $operation
4747
throw new InvalidArgumentException('Subresource operations are not supported by the OperationPathResolver.');
4848
}
4949

50-
$path = '/'.$this->pathNameGenerator->getPathName($resourceShortName, true);
50+
$path = '/'.$this->pathSegmentNameGenerator->getSegmentName($resourceShortName, true);
5151

5252
if ($operationType === OperationType::ITEM) {
5353
$path .= '/{id}';

src/PathResolver/UnderscoreOperationPathResolver.php

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

1414
namespace ApiPlatform\Core\PathResolver;
1515

16-
use ApiPlatform\Core\PathResolver\OperationPathResolver;
17-
use ApiPlatform\Core\Operation\UnderscorePathNameGenerator;
16+
use ApiPlatform\Core\Operation\UnderscorePathSegmentNameGenerator;
1817

1918
/**
2019
* Generates a path with words separated by underscores.
@@ -28,16 +27,14 @@ final class UnderscoreOperationPathResolver implements OperationPathResolverInte
2827
*/
2928
public function resolveOperationPath(string $resourceShortName, array $operation, $operationType/*, string $operationName = null*/): string
3029
{
31-
@trigger_error(sprintf('The use of %s is deprecated since 2.1. Please use PathNameGenerator instead.', __CLASS__), E_USER_DEPRECATED);
30+
@trigger_error(sprintf('The use of %s is deprecated since 2.1. Please use PathSegmentNameGenerator instead.', __CLASS__), E_USER_DEPRECATED);
3231

3332
if (func_num_args() >= 4) {
3433
$operationName = func_get_arg(3);
3534
} else {
36-
@trigger_error(sprintf('Method %s() will have a 4th `string $operationName` argument in version 3.0. Not defining it is deprecated since 2.1.', __METHOD__), E_USER_DEPRECATED);
37-
3835
$operationName = null;
3936
}
4037

41-
return (new OperationPathResolver(new UnderscorePathNameGenerator()))->resolveOperationPath($resourceShortName, $operation, $operationType, $operationName);
38+
return (new OperationPathResolver(new UnderscorePathSegmentNameGenerator()))->resolveOperationPath($resourceShortName, $operation, $operationType, $operationName);
4239
}
4340
}

tests/Bridge/Symfony/Bundle/Command/SwaggerCommandTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
/**
2121
* @author Amrouche Hamza <[email protected]>
22+
* @group legacy
2223
*/
2324
class SwaggerCommandTest extends KernelTestCase
2425
{

tests/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtensionTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -441,8 +441,8 @@ private function getContainerBuilderProphecy()
441441
'api_platform.operation_path_resolver.router',
442442
'api_platform.operation_path_resolver.generator',
443443
'api_platform.operation_path_resolver.underscore',
444-
'api_platform.path_name_generator.underscore',
445-
'api_platform.path_name_generator.dash',
444+
'api_platform.path_segment_name_generator.underscore',
445+
'api_platform.path_segment_name_generator.dash',
446446
'api_platform.problem.encoder',
447447
'api_platform.problem.normalizer.constraint_violation_list',
448448
'api_platform.problem.normalizer.error',
@@ -486,7 +486,7 @@ private function getContainerBuilderProphecy()
486486
'api_platform.metadata.resource.name_collection_factory' => 'api_platform.metadata.resource.name_collection_factory.xml',
487487
'api_platform.operation_path_resolver' => 'api_platform.operation_path_resolver.router',
488488
'api_platform.operation_path_resolver.default' => 'api_platform.operation_path_resolver.underscore',
489-
'api_platform.path_name_generator' => 'api_platform.path_name_generator.underscore',
489+
'api_platform.path_segment_name_generator' => 'api_platform.path_segment_name_generator.underscore',
490490
'api_platform.property_accessor' => 'property_accessor',
491491
'api_platform.property_info' => 'property_info',
492492
'api_platform.serializer' => 'serializer',

0 commit comments

Comments
 (0)