Skip to content

Commit 8f8e3af

Browse files
author
abluchet
committed
Deprecate all the things
1 parent 5f4f2bb commit 8f8e3af

File tree

10 files changed

+40
-77
lines changed

10 files changed

+40
-77
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ 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.generator')->info('[Deprecated] Specify the default operation path resolver to use for generating resources operations path.')->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()
4747
->scalarNode('name_converter')->defaultNull()->info('Specify a name converter to use.')->end()
4848
->scalarNode('path_name_generator')->defaultValue('api_platform.path_name_generator.underscore')->info('Specify a path name generator to use.')->end()
4949
->scalarNode('api_resources_directory')->defaultValue('Entity')->info('The name of the directory within the bundles that contains the api resources.')->end()

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,6 @@
8585

8686
<!-- Resources Operations path resolver -->
8787

88-
<service id="api_platform.path_name_generator" alias="api_platform.path_name_generator.underscore" public="false" />
89-
90-
<service id="api_platform.path_name_generator.underscore" class="ApiPlatform\Core\Operation\UnderscorePathNameGenerator" public="false" />
91-
<service id="api_platform.path_name_generator.dash" class="ApiPlatform\Core\Operation\DashPathNameGenerator" public="false" />
92-
9388
<service id="api_platform.operation_path_resolver" alias="api_platform.operation_path_resolver.router" public="false" />
9489

9590
<service id="api_platform.operation_path_resolver.router" class="ApiPlatform\Core\Bridge\Symfony\Routing\RouterOperationPathResolver" public="false">
@@ -99,6 +94,7 @@
9994
</service>
10095

10196
<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 -->
10298
<argument type="service" id="api_platform.operation_path_resolver.default" />
10399
</service>
104100

@@ -114,6 +110,12 @@
114110
<deprecated>The "%service_id%" service is deprecated since ApiPlatform 2.1 and will be removed in 3.0. Use PathNameGenerator instead.</deprecated>
115111
</service>
116112

113+
<!-- Path name generator -->
114+
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" />
118+
117119
<!-- Event listeners -->
118120

119121
<!-- kernel.request priority must be < 8 to be executed after the Firewall -->

src/Bridge/Symfony/Routing/RouterOperationPathResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function resolveOperationPath(string $resourceShortName, array $operation
5353
if (isset($operation['route_name'])) {
5454
$routeName = $operation['route_name'];
5555
} elseif (OperationType::SUBRESOURCE === $operationType) {
56-
throw new InvalidArgumentException('Subresource operations are not supported by the OperationPathResolver.');
56+
throw new InvalidArgumentException('Subresource operations are not supported by the RouterOperationPathResolver.');
5757
} else {
5858
if (null !== $operationName) {
5959
$routeName = RouteNameGenerator::generate($operationName, $resourceShortName, $operationType, $operation);

src/PathResolver/DashOperationPathResolver.php

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@
1313

1414
namespace ApiPlatform\Core\PathResolver;
1515

16-
use ApiPlatform\Core\Api\OperationType;
17-
use ApiPlatform\Core\Api\OperationTypeDeprecationHelper;
18-
use Doctrine\Common\Inflector\Inflector;
16+
use ApiPlatform\Core\PathResolver\OperationPathResolver;
17+
use ApiPlatform\Core\Operation\DashPathNameGenerator;
1918

2019
/**
21-
* Generates a path with words separated by dashes.
20+
* Generates a path with words separated by underscores.
2221
*
2322
* @author Paul Le Corre <[email protected]>
2423
*/
@@ -29,35 +28,16 @@ final class DashOperationPathResolver implements OperationPathResolverInterface
2928
*/
3029
public function resolveOperationPath(string $resourceShortName, array $operation, $operationType/*, string $operationName = null*/): string
3130
{
32-
if (func_num_args() < 4) {
33-
@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);
34-
}
35-
36-
$operationType = OperationTypeDeprecationHelper::getOperationType($operationType);
31+
@trigger_error(sprintf('The use of %s is deprecated since 2.1. Please use PathNameGenerator instead.', __CLASS__), E_USER_DEPRECATED);
3732

38-
if ($operationType === OperationType::SUBRESOURCE && 1 < count($operation['identifiers'])) {
39-
$path = str_replace('.{_format}', '', $resourceShortName);
33+
if (func_num_args() >= 4) {
34+
$operationName = func_get_arg(3);
4035
} else {
41-
$path = '/'.Inflector::pluralize($this->dashize($resourceShortName));
42-
}
43-
44-
if ($operationType === OperationType::ITEM) {
45-
$path .= '/{id}';
46-
}
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);
4737

48-
if ($operationType === OperationType::SUBRESOURCE) {
49-
list($key) = end($operation['identifiers']);
50-
$property = true === $operation['collection'] ? Inflector::pluralize($this->dashize($operation['property'])) : $this->dashize($operation['property']);
51-
$path .= sprintf('/{%s}/%s', $key, $property);
38+
$operationName = null;
5239
}
5340

54-
$path .= '.{_format}';
55-
56-
return $path;
57-
}
58-
59-
private function dashize(string $string): string
60-
{
61-
return strtolower(preg_replace('~(?<=\\w)([A-Z])~', '-$1', $string));
41+
return (new OperationPathResolver(new DashPathNameGenerator()))->resolveOperationPath($resourceShortName, $operation, $operationType, $operationName);
6242
}
6343
}

src/PathResolver/UnderscoreOperationPathResolver.php

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@
1313

1414
namespace ApiPlatform\Core\PathResolver;
1515

16-
use ApiPlatform\Core\Api\OperationType;
17-
use ApiPlatform\Core\Api\OperationTypeDeprecationHelper;
18-
use ApiPlatform\Core\Bridge\Symfony\Routing\RouteNameGenerator;
16+
use ApiPlatform\Core\PathResolver\OperationPathResolver;
17+
use ApiPlatform\Core\Operation\UnderscorePathNameGenerator;
1918

2019
/**
2120
* Generates a path with words separated by underscores.
@@ -29,30 +28,16 @@ final class UnderscoreOperationPathResolver implements OperationPathResolverInte
2928
*/
3029
public function resolveOperationPath(string $resourceShortName, array $operation, $operationType/*, string $operationName = null*/): string
3130
{
32-
if (func_num_args() < 4) {
33-
@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);
34-
}
31+
@trigger_error(sprintf('The use of %s is deprecated since 2.1. Please use PathNameGenerator instead.', __CLASS__), E_USER_DEPRECATED);
3532

36-
$operationType = OperationTypeDeprecationHelper::getOperationType($operationType);
37-
38-
if ($operationType === OperationType::SUBRESOURCE && 1 < count($operation['identifiers'])) {
39-
$path = str_replace('.{_format}', '', $resourceShortName);
33+
if (func_num_args() >= 4) {
34+
$operationName = func_get_arg(3);
4035
} else {
41-
$path = '/'.RouteNameGenerator::inflector($resourceShortName, true);
42-
}
43-
44-
if ($operationType === OperationType::ITEM) {
45-
$path .= '/{id}';
46-
}
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);
4737

48-
if ($operationType === OperationType::SUBRESOURCE) {
49-
list($key) = end($operation['identifiers']);
50-
$property = RouteNameGenerator::inflector($operation['property'], $operation['collection']);
51-
$path .= sprintf('/{%s}/%s', $key, $property);
38+
$operationName = null;
5239
}
5340

54-
$path .= '.{_format}';
55-
56-
return $path;
41+
return (new OperationPathResolver(new UnderscorePathNameGenerator()))->resolveOperationPath($resourceShortName, $operation, $operationType, $operationName);
5742
}
5843
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ private function getContainerBuilderProphecy()
485485
'api_platform.metadata.resource.metadata_factory' => 'api_platform.metadata.resource.metadata_factory.xml',
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',
488-
'api_platform.operation_path_resolver.default' => 'api_platform.operation_path_resolver.generator',
488+
'api_platform.operation_path_resolver.default' => 'api_platform.operation_path_resolver.underscore',
489489
'api_platform.path_name_generator' => 'api_platform.path_name_generator.underscore',
490490
'api_platform.property_accessor' => 'property_accessor',
491491
'api_platform.property_info' => 'property_info',

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ public function testDefaultConfig()
6767
ExceptionInterface::class => Response::HTTP_BAD_REQUEST,
6868
InvalidArgumentException::class => Response::HTTP_BAD_REQUEST,
6969
],
70-
'default_operation_path_resolver' => 'api_platform.operation_path_resolver.generator',
71-
'name_converter' => null,
70+
'default_operation_path_resolver' => 'api_platform.operation_path_resolver.underscore',
7271
'path_name_generator' => 'api_platform.path_name_generator.underscore',
72+
'name_converter' => null,
7373
'enable_fos_user' => false,
7474
'enable_nelmio_api_doc' => false,
7575
'enable_swagger' => true,

tests/Bridge/Symfony/Routing/RouterOperationPathResolverTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function testResolveOperationPath()
4242

4343
/**
4444
* @expectedException \ApiPlatform\Core\Exception\InvalidArgumentException
45-
* @expectedMessage Subresource operations are not supported by the RouteNameGenerator.
45+
* @expectedMessage Subresource operations are not supported by the RouterOperationPathResolver.
4646
*/
4747
public function testResolveOperationPathWithSubresource()
4848
{

tests/PathResolver/DashOperationPathResolverTest.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,15 @@ public function testResolveItemOperationPath()
3535
$this->assertSame('/short-names/{id}.{_format}', $dashOperationPathResolver->resolveOperationPath('ShortName', [], OperationType::ITEM, 'get'));
3636
}
3737

38+
/**
39+
* @expectedException \ApiPlatform\Core\Exception\InvalidArgumentException
40+
* @expectedMessage Subresource operations are not supported by the OperationPathResolver.
41+
*/
3842
public function testResolveSubresourceOperationPath()
3943
{
4044
$dashOperationPathResolver = new DashOperationPathResolver();
4145

42-
$path = $dashOperationPathResolver->resolveOperationPath('ShortName', ['property' => 'relatedFoo', 'identifiers' => [['id', 'class']], 'collection' => true], OperationType::SUBRESOURCE, 'get');
43-
44-
$this->assertSame('/short-names/{id}/related-foos.{_format}', $path);
45-
46-
$next = $dashOperationPathResolver->resolveOperationPath($path, ['property' => 'bar', 'identifiers' => [['id', 'class'], ['relatedId', 'class']], 'collection' => false], OperationType::SUBRESOURCE, 'get');
47-
48-
$this->assertSame('/short-names/{id}/related-foos/{relatedId}/bar.{_format}', $next);
46+
$dashOperationPathResolver->resolveOperationPath('ShortName', ['property' => 'bar', 'identifiers' => [['id', 'class'], ['relatedId', 'class']], 'collection' => false], OperationType::SUBRESOURCE, 'get');
4947
}
5048

5149
/**

tests/PathResolver/UnderscoreOperationPathResolverTest.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,15 @@ public function testResolveItemOperationPath()
3535
$this->assertSame('/short_names/{id}.{_format}', $underscoreOperationPathResolver->resolveOperationPath('ShortName', [], OperationType::ITEM, 'get'));
3636
}
3737

38+
/**
39+
* @expectedException \ApiPlatform\Core\Exception\InvalidArgumentException
40+
* @expectedMessage Subresource operations are not supported by the OperationPathResolver.
41+
*/
3842
public function testResolveSubresourceOperationPath()
3943
{
4044
$dashOperationPathResolver = new UnderscoreOperationPathResolver();
4145

42-
$path = $dashOperationPathResolver->resolveOperationPath('ShortName', ['property' => 'relatedFoo', 'identifiers' => [['id', 'class']], 'collection' => true], OperationType::SUBRESOURCE, 'get');
43-
44-
$this->assertSame('/short_names/{id}/related_foos.{_format}', $path);
45-
46-
$next = $dashOperationPathResolver->resolveOperationPath($path, ['property' => 'bar', 'identifiers' => [['id', 'class'], ['relatedId', 'class']], 'collection' => false], OperationType::SUBRESOURCE, 'get');
47-
48-
$this->assertSame('/short_names/{id}/related_foos/{relatedId}/bar.{_format}', $next);
46+
$dashOperationPathResolver->resolveOperationPath('ShortName', ['property' => 'relatedFoo', 'identifiers' => [['id', 'class']], 'collection' => true], OperationType::SUBRESOURCE, 'get');
4947
}
5048

5149
/**

0 commit comments

Comments
 (0)