Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function load(array $configs, ContainerBuilder $container)
}
}

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

if ($config['name_converter']) {
$container->setAlias('api_platform.name_converter', $config['name_converter']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ public function getConfigTreeBuilder()
->end()
->end()
->end()
->arrayNode('routing')
->arrayNode('naming')
->addDefaultsIfNotSet()
->children()
->scalarNode('resource_path_generator')->defaultValue('api_platform.routing.resource_path_generator.underscore')->info('Specify the strategy to use for generating resource paths.')->end()
->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()
->end()
->end()
->scalarNode('name_converter')->defaultNull()->info('Specify a name converter to use.')->end()
Expand Down
9 changes: 4 additions & 5 deletions src/Bridge/Symfony/Bundle/Resources/config/api.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<argument type="service" id="kernel" />
<argument type="service" id="api_platform.metadata.resource.name_collection_factory" />
<argument type="service" id="api_platform.metadata.resource.metadata_factory" />
<argument type="service" id="api_platform.routing.resource_path_generator" />
<argument type="service" id="api_platform.naming.resource_path_naming_strategy" />
<argument type="service" id="service_container" />
<tag name="routing.loader" />
</service>
Expand All @@ -49,11 +49,10 @@
<argument type="service" id="api_platform.metadata.resource.metadata_factory" />
</service>

<!-- Resource path generators -->
<!-- Resource path naming strategies -->

<service id="api_platform.routing.resource_path_generator.underscore" class="ApiPlatform\Core\Routing\UnderscoreResourcePathGenerator" public="false" />

<service id="api_platform.routing.resource_path_generator.dash" class="ApiPlatform\Core\Routing\DashResourcePathGenerator" public="false" />
<service id="api_platform.naming.resource_path_naming_strategy.underscore" class="ApiPlatform\Core\Naming\UnderscoreResourcePathNamingStrategy" public="false" />
<service id="api_platform.naming.resource_path_naming_strategy.dash" class="ApiPlatform\Core\Routing\DashResourcePathNamingStrategy" public="false" />

<!-- Event listeners -->

Expand Down
4 changes: 2 additions & 2 deletions src/Bridge/Symfony/Routing/ApiLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use ApiPlatform\Core\Exception\RuntimeException;
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
use ApiPlatform\Core\Routing\ResourcePathGeneratorInterface;
use ApiPlatform\Core\Naming\ResourcePathNamingStrategyInterface;
use Doctrine\Common\Inflector\Inflector;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Loader\Loader;
Expand All @@ -41,7 +41,7 @@ final class ApiLoader extends Loader
private $resourcePathGenerator;
private $container;

public function __construct(KernelInterface $kernel, ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory, ResourceMetadataFactoryInterface $resourceMetadataFactory, ResourcePathGeneratorInterface $resourcePathGenerator, ContainerInterface $container)
public function __construct(KernelInterface $kernel, ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory, ResourceMetadataFactoryInterface $resourceMetadataFactory, ResourcePathNamingStrategyInterface $resourcePathGenerator, ContainerInterface $container)
{
$this->fileLoader = new XmlFileLoader(new FileLocator($kernel->locateResource('@ApiPlatformBundle/Resources/config/routing')));
$this->resourceNameCollectionFactory = $resourceNameCollectionFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,22 @@
* file that was distributed with this source code.
*/

namespace ApiPlatform\Core\Routing;
namespace ApiPlatform\Core\Naming;

use Doctrine\Common\Inflector\Inflector;

/**
* Generates a path with words separated by dashes.
*
* @author Paul Le Corre <paul@lecorre.me>
*/
class UnderscoreResourcePathGenerator implements ResourcePathGeneratorInterface
final class DashResourcePathNamingStrategy implements ResourcePathNamingStrategyInterface
{
/**
* {@inheritdoc}
*/
public function generateResourceBasePath(string $resourceShortName) : string
{
$pathName = Inflector::tableize($resourceShortName);

return Inflector::pluralize($pathName);
return Inflector::pluralize(strtolower(preg_replace('~(?<=\\w)([A-Z])~', '-$1', $resourceShortName)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,21 @@
* file that was distributed with this source code.
*/

namespace ApiPlatform\Core\Routing;
namespace ApiPlatform\Core\Naming;

/**
* Generates a path from a resource name.
*
* @author Paul Le Corre <paul@lecorre.me>
*/
interface ResourcePathGeneratorInterface
interface ResourcePathNamingStrategyInterface
{
/**
* Generates the base path.
*
* @param string $resourceShortName
*
* @return string
*/
public function generateResourceBasePath(string $resourceShortName) : string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,22 @@
* file that was distributed with this source code.
*/

namespace ApiPlatform\Core\Routing;
namespace ApiPlatform\Core\Naming;

use Doctrine\Common\Inflector\Inflector;

/**
* Generates a path with words separated by underscores.
*
* @author Paul Le Corre <paul@lecorre.me>
*/
class DashResourcePathGenerator implements ResourcePathGeneratorInterface
final class UnderscoreResourcePathNamingStrategy implements ResourcePathNamingStrategyInterface
{
/**
* {@inheritdoc}
*/
public function generateResourceBasePath(string $resourceShortName) : string
{
$pathName = strtolower(preg_replace('~(?<=\\w)([A-Z])~', '-$1', $resourceShortName));

return Inflector::pluralize($pathName);
return Inflector::pluralize(Inflector::tableize($resourceShortName));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,8 @@ private function getContainerBuilderProphecy()
'api_platform.route_loader',
'api_platform.router',
'api_platform.iri_converter',
'api_platform.routing.resource_path_generator.underscore',
'api_platform.routing.resource_path_generator.dash',
'api_platform.naming.resource_path_naming_strategy.underscore',
'api_platform.naming.resource_path_naming_strategy.dash',
'api_platform.listener.request.format',
'api_platform.listener.view.serializer',
'api_platform.listener.view.deserializer',
Expand Down Expand Up @@ -317,7 +317,7 @@ private function getContainerBuilderProphecy()
}

$aliases = [
'api_platform.routing.resource_path_generator' => 'api_platform.routing.resource_path_generator.underscore',
'api_platform.naming.resource_path_naming_strategy' => 'api_platform.naming.resource_path_naming_strategy.underscore',
'api_platform.metadata.resource.name_collection_factory' => 'api_platform.metadata.resource.name_collection_factory.annotation',
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public function testDefaultConfig()
'title' => 'title',
'description' => 'description',
'formats' => ['jsonld' => ['mime_types' => ['application/ld+json']]],
'routing' => [
'resource_path_generator' => 'api_platform.routing.resource_path_generator.underscore',
'naming' => [
'resource_path_naming_strategy' => 'api_platform.naming.resource_path_naming_strategy.underscore',
],
'name_converter' => null,
'enable_fos_user' => false,
Expand Down
4 changes: 2 additions & 2 deletions tests/Bridge/Symfony/Routing/ApiLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
use ApiPlatform\Core\Metadata\Resource\ResourceNameCollection;
use ApiPlatform\Core\Routing\ResourcePathGeneratorInterface;
use ApiPlatform\Core\Naming\ResourcePathNamingStrategyInterface;
use ApiPlatform\Core\Tests\Fixtures\DummyEntity;
use Prophecy\Argument;
use Symfony\Component\DependencyInjection\ContainerInterface;
Expand Down Expand Up @@ -156,7 +156,7 @@ private function getApiLoaderWithResourceMetadata(ResourceMetadata $resourceMeta
$resourceNameCollectionFactoryProphecy = $this->prophesize(ResourceNameCollectionFactoryInterface::class);
$resourceNameCollectionFactoryProphecy->create()->willReturn(new ResourceNameCollection([DummyEntity::class]));

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

$apiLoader = new ApiLoader($kernelProphecy->reveal(), $resourceNameCollectionFactoryProphecy->reveal(), $resourceMetadataFactoryProphecy->reveal(), $resourcePathGeneratorProphecy->reveal(), $containerProphecy->reveal());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
* file that was distributed with this source code.
*/

namespace ApiPlatform\Core\Tests\Routing;
namespace ApiPlatform\Core\Tests\Naming;

use ApiPlatform\Core\Routing\DashResourcePathGenerator;
use ApiPlatform\Core\Naming\DashResourcePathNamingStrategy;

class DashResourcePathGeneratorTest extends \PHPUnit_Framework_TestCase
class DashResourcePathNamingStrategyTest extends \PHPUnit_Framework_TestCase
{
public function testGenerateResourceBasePath()
{
$dashResourcePathGenerator = new DashResourcePathGenerator();
$dashResourcePathGenerator = new DashResourcePathNamingStrategy();

$this->assertSame('short-names', $dashResourcePathGenerator->generateResourceBasePath('ShortName'));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/

namespace ApiPlatform\Core\Tests\Routing;
namespace ApiPlatform\Core\Tests\Naming;

use ApiPlatform\Core\Bridge\Symfony\Routing\Router;
use Prophecy\Argument;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
* file that was distributed with this source code.
*/

namespace ApiPlatform\Core\Tests\Routing;
namespace ApiPlatform\Core\Tests\Naming;

use ApiPlatform\Core\Routing\UnderscoreResourcePathGenerator;
use ApiPlatform\Core\Naming\UnderscoreResourcePathNamingStrategy;

class UnderscoreResourcePathGeneratorTest extends \PHPUnit_Framework_TestCase
class UnderscoreResourcePathNamingStrategyTest extends \PHPUnit_Framework_TestCase
{
public function testGenerateResourceBasePath()
{
$underscoreResourcePathGenerator = new UnderscoreResourcePathGenerator();
$underscoreResourcePathGenerator = new UnderscoreResourcePathNamingStrategy();

$this->assertSame('short_names', $underscoreResourcePathGenerator->generateResourceBasePath('ShortName'));
}
Expand Down