Skip to content

Commit 3573613

Browse files
committed
Rename Path Generator to Resource Path Naming Strategy
1 parent dc227ec commit 3573613

13 files changed

+52
-38
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function load(array $configs, ContainerBuilder $container)
5959
}
6060
}
6161

62-
$container->setAlias('api_platform.routing.resource_path_generator', $config['routing']['resource_path_generator']);
62+
$container->setAlias('api_platform.naming.resource_path_naming_strategy', $config['naming']['resource_path_naming_strategy']);
6363

6464
if ($config['name_converter']) {
6565
$container->setAlias('api_platform.name_converter', $config['name_converter']);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ public function getConfigTreeBuilder()
5858
->end()
5959
->end()
6060
->end()
61-
->arrayNode('routing')
61+
->arrayNode('naming')
6262
->addDefaultsIfNotSet()
6363
->children()
64-
->scalarNode('resource_path_generator')->defaultValue('api_platform.routing.resource_path_generator.underscore')->info('Specify the strategy to use for generating resource paths.')->end()
64+
->scalarNode('resource_path_naming_strategy')->defaultValue('api_platform.naming.resource_path_naming_strategy.underscore')->info('Specify the strategy to use for generating resource paths.')->end()
6565
->end()
6666
->end()
6767
->scalarNode('name_converter')->defaultNull()->info('Specify a name converter to use.')->end()

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
<argument type="service" id="kernel" />
2525
<argument type="service" id="api_platform.metadata.resource.name_collection_factory" />
2626
<argument type="service" id="api_platform.metadata.resource.metadata_factory" />
27-
<argument type="service" id="api_platform.routing.resource_path_generator" />
27+
<argument type="service" id="api_platform.naming.resource_path_naming_strategy" />
2828
<argument type="service" id="service_container" />
2929
<tag name="routing.loader" />
3030
</service>
@@ -49,11 +49,10 @@
4949
<argument type="service" id="api_platform.metadata.resource.metadata_factory" />
5050
</service>
5151

52-
<!-- Resource path generators -->
52+
<!-- Resource path naming strategies -->
5353

54-
<service id="api_platform.routing.resource_path_generator.underscore" class="ApiPlatform\Core\Routing\UnderscoreResourcePathGenerator" public="false" />
55-
56-
<service id="api_platform.routing.resource_path_generator.dash" class="ApiPlatform\Core\Routing\DashResourcePathGenerator" public="false" />
54+
<service id="api_platform.naming.resource_path_naming_strategy.underscore" class="ApiPlatform\Core\Naming\UnderscoreResourcePathNamingStrategy" public="false" />
55+
<service id="api_platform.naming.resource_path_naming_strategy.dash" class="ApiPlatform\Core\Routing\DashResourcePathNamingStrategy" public="false" />
5756

5857
<!-- Event listeners -->
5958

src/Bridge/Symfony/Routing/ApiLoader.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use ApiPlatform\Core\Exception\RuntimeException;
1616
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
1717
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
18-
use ApiPlatform\Core\Routing\ResourcePathGeneratorInterface;
18+
use ApiPlatform\Core\Naming\ResourcePathNamingStrategyInterface;
1919
use Doctrine\Common\Inflector\Inflector;
2020
use Symfony\Component\Config\FileLocator;
2121
use Symfony\Component\Config\Loader\Loader;
@@ -41,7 +41,7 @@ final class ApiLoader extends Loader
4141
private $resourcePathGenerator;
4242
private $container;
4343

44-
public function __construct(KernelInterface $kernel, ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory, ResourceMetadataFactoryInterface $resourceMetadataFactory, ResourcePathGeneratorInterface $resourcePathGenerator, ContainerInterface $container)
44+
public function __construct(KernelInterface $kernel, ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory, ResourceMetadataFactoryInterface $resourceMetadataFactory, ResourcePathNamingStrategyInterface $resourcePathGenerator, ContainerInterface $container)
4545
{
4646
$this->fileLoader = new XmlFileLoader(new FileLocator($kernel->locateResource('@ApiPlatformBundle/Resources/config/routing')));
4747
$this->resourceNameCollectionFactory = $resourceNameCollectionFactory;

src/Routing/UnderscoreResourcePathGenerator.php renamed to src/Naming/DashResourcePathNamingStrategy.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,22 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace ApiPlatform\Core\Routing;
12+
namespace ApiPlatform\Core\Naming;
1313

1414
use Doctrine\Common\Inflector\Inflector;
1515

1616
/**
17+
* Generates a path with words separated by dashes.
18+
*
1719
* @author Paul Le Corre <[email protected]>
1820
*/
19-
class UnderscoreResourcePathGenerator implements ResourcePathGeneratorInterface
21+
final class DashResourcePathNamingStrategy implements ResourcePathNamingStrategyInterface
2022
{
23+
/**
24+
* {@inheritdoc}
25+
*/
2126
public function generateResourceBasePath(string $resourceShortName) : string
2227
{
23-
$pathName = Inflector::tableize($resourceShortName);
24-
25-
return Inflector::pluralize($pathName);
28+
return Inflector::pluralize(strtolower(preg_replace('~(?<=\\w)([A-Z])~', '-$1', $resourceShortName)));
2629
}
2730
}

src/Routing/ResourcePathGeneratorInterface.php renamed to src/Naming/ResourcePathNamingStrategyInterface.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,21 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace ApiPlatform\Core\Routing;
12+
namespace ApiPlatform\Core\Naming;
1313

1414
/**
15+
* Generates a path from a resource name.
16+
*
1517
* @author Paul Le Corre <[email protected]>
1618
*/
17-
interface ResourcePathGeneratorInterface
19+
interface ResourcePathNamingStrategyInterface
1820
{
21+
/**
22+
* Generates the base path.
23+
*
24+
* @param string $resourceShortName
25+
*
26+
* @return string
27+
*/
1928
public function generateResourceBasePath(string $resourceShortName) : string;
2029
}

src/Routing/DashResourcePathGenerator.php renamed to src/Naming/UnderscoreResourcePathNamingStrategy.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,22 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace ApiPlatform\Core\Routing;
12+
namespace ApiPlatform\Core\Naming;
1313

1414
use Doctrine\Common\Inflector\Inflector;
1515

1616
/**
17+
* Generates a path with words separated by underscores.
18+
*
1719
* @author Paul Le Corre <[email protected]>
1820
*/
19-
class DashResourcePathGenerator implements ResourcePathGeneratorInterface
21+
final class UnderscoreResourcePathNamingStrategy implements ResourcePathNamingStrategyInterface
2022
{
23+
/**
24+
* {@inheritdoc}
25+
*/
2126
public function generateResourceBasePath(string $resourceShortName) : string
2227
{
23-
$pathName = strtolower(preg_replace('~(?<=\\w)([A-Z])~', '-$1', $resourceShortName));
24-
25-
return Inflector::pluralize($pathName);
28+
return Inflector::pluralize(Inflector::tableize($resourceShortName));
2629
}
2730
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,8 @@ private function getContainerBuilderProphecy()
260260
'api_platform.route_loader',
261261
'api_platform.router',
262262
'api_platform.iri_converter',
263-
'api_platform.routing.resource_path_generator.underscore',
264-
'api_platform.routing.resource_path_generator.dash',
263+
'api_platform.naming.resource_path_naming_strategy.underscore',
264+
'api_platform.naming.resource_path_naming_strategy.dash',
265265
'api_platform.listener.request.format',
266266
'api_platform.listener.view.serializer',
267267
'api_platform.listener.view.deserializer',
@@ -317,7 +317,7 @@ private function getContainerBuilderProphecy()
317317
}
318318

319319
$aliases = [
320-
'api_platform.routing.resource_path_generator' => 'api_platform.routing.resource_path_generator.underscore',
320+
'api_platform.naming.resource_path_naming_strategy' => 'api_platform.naming.resource_path_naming_strategy.underscore',
321321
'api_platform.metadata.resource.name_collection_factory' => 'api_platform.metadata.resource.name_collection_factory.annotation',
322322
];
323323

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ public function testDefaultConfig()
3434
'title' => 'title',
3535
'description' => 'description',
3636
'formats' => ['jsonld' => ['mime_types' => ['application/ld+json']]],
37-
'routing' => [
38-
'resource_path_generator' => 'api_platform.routing.resource_path_generator.underscore',
37+
'naming' => [
38+
'resource_path_naming_strategy' => 'api_platform.naming.resource_path_naming_strategy.underscore',
3939
],
4040
'name_converter' => null,
4141
'enable_fos_user' => false,

tests/Bridge/Symfony/Routing/ApiLoaderTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
1717
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
1818
use ApiPlatform\Core\Metadata\Resource\ResourceNameCollection;
19-
use ApiPlatform\Core\Routing\ResourcePathGeneratorInterface;
19+
use ApiPlatform\Core\Naming\ResourcePathNamingStrategyInterface;
2020
use ApiPlatform\Core\Tests\Fixtures\DummyEntity;
2121
use Prophecy\Argument;
2222
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -156,7 +156,7 @@ private function getApiLoaderWithResourceMetadata(ResourceMetadata $resourceMeta
156156
$resourceNameCollectionFactoryProphecy = $this->prophesize(ResourceNameCollectionFactoryInterface::class);
157157
$resourceNameCollectionFactoryProphecy->create()->willReturn(new ResourceNameCollection([DummyEntity::class]));
158158

159-
$resourcePathGeneratorProphecy = $this->prophesize(ResourcePathGeneratorInterface::class);
159+
$resourcePathGeneratorProphecy = $this->prophesize(ResourcePathNamingStrategyInterface::class);
160160
$resourcePathGeneratorProphecy->generateResourceBasePath('dummy')->willReturn('dummies');
161161

162162
$apiLoader = new ApiLoader($kernelProphecy->reveal(), $resourceNameCollectionFactoryProphecy->reveal(), $resourceMetadataFactoryProphecy->reveal(), $resourcePathGeneratorProphecy->reveal(), $containerProphecy->reveal());

0 commit comments

Comments
 (0)