Skip to content

Commit

Permalink
Make mapping paths configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
loic425 committed Jan 10, 2022
1 parent 23f6301 commit 66f8512
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 9 deletions.
9 changes: 9 additions & 0 deletions src/Bundle/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ public function getConfigTreeBuilder(): TreeBuilder

$rootNode
->children()
->arrayNode('mapping')
->addDefaultsIfNotSet()
->children()
->arrayNode('paths')
->defaultValue(['%kernel.project_dir%/src/Entity'])
->prototype('scalar')->end()
->end()
->end()
->end()
->scalarNode('authorization_checker')
->defaultValue('sylius.resource_controller.authorization_checker.disabled')
->cannotBeEmpty()
Expand Down
1 change: 1 addition & 0 deletions src/Bundle/DependencyInjection/SyliusResourceExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function load(array $configs, ContainerBuilder $container): void
$container->setAlias('sylius.translation_locale_provider', $config['translation']['locale_provider'])->setPublic(true);
}

$container->setParameter('sylius.resource.mapping', $config['mapping']);
$container->setParameter('sylius.resource.settings', $config['settings']);
$container->setAlias('sylius.resource_controller.authorization_checker', $config['authorization_checker']);

Expand Down
2 changes: 1 addition & 1 deletion src/Bundle/Resources/config/services/routing.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

<service id="sylius.routing.loader.attributes" class="Sylius\Bundle\ResourceBundle\Routing\AttributesLoader">
<!-- TODO make it configurable -->
<argument>%kernel.project_dir%/src/Entity</argument>
<argument>%sylius.resource.mapping%</argument>
<argument type="service" id="sylius.routing.loader.resource" />
<tag name="routing.route_loader" />
</service>
Expand Down
20 changes: 13 additions & 7 deletions src/Bundle/Routing/AttributesLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@

final class AttributesLoader
{
private string $resourceDirectory;
private array $mapping;

private ResourceLoader $resourceLoader;

public function __construct(
string $resourceDirectory,
array $mapping,
ResourceLoader $resourceLoader
) {
$this->resourceDirectory = $resourceDirectory;
$this->mapping = $mapping;
$this->resourceLoader = $resourceLoader;
}

Expand Down Expand Up @@ -117,13 +117,19 @@ private function getClassAttributes(\ReflectionClass $reflectionClass, string $a

private function getReflectionClasses(): \Iterator
{
$resources = $this->getResourcesByPath($this->resourceDirectory);
$paths = $this->mapping['paths'] ?? [];

foreach ($resources as $className) {
$reflectionClass = new \ReflectionClass($className);
foreach ($paths as $resourceDirectory) {
$resources = $this->getResourcesByPath($resourceDirectory);

yield $className => $reflectionClass;
foreach ($resources as $className) {
$reflectionClass = new \ReflectionClass($className);

yield $className => $reflectionClass;
}
}


}

private function getResourcesByPath(string $path): array
Expand Down
42 changes: 42 additions & 0 deletions src/Bundle/Tests/Configuration/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,48 @@ public function it_does_not_break_if_not_customized()
);
}

/**
* @test
*/
public function it_has_default_mapping_paths()
{
$this->assertProcessedConfigurationEquals(
[
[],
],
[
'mapping' => [
'paths' => [
'%kernel.project_dir%/src/Entity',
],
]
],
'mapping'
);
}

/**
* @test
*/
public function its_mapping_paths_can_be_customized()
{
$this->assertProcessedConfigurationEquals(
[
['mapping' => [
'paths' => ['path/to/resources'],
]],
],
[
'mapping' => [
'paths' => [
'path/to/resources',
],
]
],
'mapping'
);
}

/**
* @test
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,27 @@ public function it_does_not_break_when_aliasing_two_resources_use_same_factory_c
$this->assertContainerBuilderHasAlias(sprintf('%s $comicBookFactory', BookFactory::class), 'app.factory.comic_book');
}

/**
* @test
*/
public function it_registers_parameter_for_paths(): void
{
$this->setParameter('kernel.bundles', []);
$this->load([
'mapping' => [
'paths' => [
'path/to/resources',
],
],
]);

$this->assertContainerBuilderHasParameter('sylius.resource.mapping', [
'paths' => [
'path/to/resources',
],
]);
}

protected function getContainerExtensions(): array
{
return [
Expand Down
54 changes: 53 additions & 1 deletion src/Bundle/spec/Routing/AttributesLoaderSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ final class AttributesLoaderSpec extends ObjectBehavior
function let(RegistryInterface $resourceRegistry, RouteFactoryInterface $routeFactory): void
{
$this->beConstructedWith(
__DIR__.'/../../test/src',
['paths' => [__DIR__.'/../../test/src']],
new ResourceLoader($resourceRegistry->getWrappedObject(), $routeFactory->getWrappedObject())
);
}
Expand All @@ -47,6 +47,10 @@ function it_generates_routes_from_resource(
Route $route,
RouteCollection $routeCollection
): void {
if (\PHP_VERSION_ID < 80000) {
return;
}

$resourceRegistry->get(Argument::cetera())->willReturn($metadata);
$metadata->getPluralName()->willReturn('books');
$metadata->getServiceId('controller')->willReturn('app.controller.book');
Expand Down Expand Up @@ -77,6 +81,10 @@ function it_generates_routes_from_resource_with_methods(
Route $route,
RouteCollection $routeCollection
): void {
if (\PHP_VERSION_ID < 80000) {
return;
}

$resourceRegistry->get(Argument::cetera())->willReturn($metadata);
$metadata->getPluralName()->willReturn('books');
$metadata->getServiceId('controller')->willReturn('app.controller.book');
Expand Down Expand Up @@ -108,6 +116,10 @@ function it_generates_routes_from_resource_with_criteria(
Route $route,
RouteCollection $routeCollection
): void {
if (\PHP_VERSION_ID < 80000) {
return;
}

$resourceRegistry->get(Argument::cetera())->willReturn($metadata);
$metadata->getPluralName()->willReturn('books');
$metadata->getServiceId('controller')->willReturn('app.controller.book');
Expand Down Expand Up @@ -142,6 +154,10 @@ function it_generates_routes_from_resource_with_template(
Route $route,
RouteCollection $routeCollection
): void {
if (\PHP_VERSION_ID < 80000) {
return;
}

$resourceRegistry->get(Argument::cetera())->willReturn($metadata);
$metadata->getPluralName()->willReturn('books');
$metadata->getServiceId('controller')->willReturn('app.controller.book');
Expand Down Expand Up @@ -174,6 +190,10 @@ function it_generates_routes_from_resource_with_repository(
Route $route,
RouteCollection $routeCollection
): void {
if (\PHP_VERSION_ID < 80000) {
return;
}

$resourceRegistry->get(Argument::cetera())->willReturn($metadata);
$metadata->getPluralName()->willReturn('books');
$metadata->getServiceId('controller')->willReturn('app.controller.book');
Expand Down Expand Up @@ -209,6 +229,10 @@ function it_generates_routes_from_resource_with_serialization_groups(
Route $route,
RouteCollection $routeCollection
): void {
if (\PHP_VERSION_ID < 80000) {
return;
}

$resourceRegistry->get(Argument::cetera())->willReturn($metadata);
$metadata->getPluralName()->willReturn('books');
$metadata->getServiceId('controller')->willReturn('app.controller.book');
Expand Down Expand Up @@ -241,6 +265,10 @@ function it_generates_routes_from_resource_with_serialization_version(
Route $route,
RouteCollection $routeCollection
): void {
if (\PHP_VERSION_ID < 80000) {
return;
}

$resourceRegistry->get(Argument::cetera())->willReturn($metadata);
$metadata->getPluralName()->willReturn('books');
$metadata->getServiceId('controller')->willReturn('app.controller.book');
Expand Down Expand Up @@ -273,6 +301,10 @@ function it_generates_routes_from_resource_with_vars(
Route $route,
RouteCollection $routeCollection
): void {
if (\PHP_VERSION_ID < 80000) {
return;
}

$resourceRegistry->get(Argument::cetera())->willReturn($metadata);
$metadata->getPluralName()->willReturn('books');
$metadata->getServiceId('controller')->willReturn('app.controller.book');
Expand Down Expand Up @@ -307,6 +339,10 @@ function it_generates_routes_from_resource_with_requirements(
Route $route,
RouteCollection $routeCollection
): void {
if (\PHP_VERSION_ID < 80000) {
return;
}

$resourceRegistry->get(Argument::cetera())->willReturn($metadata);
$metadata->getPluralName()->willReturn('books');
$metadata->getServiceId('controller')->willReturn('app.controller.book');
Expand Down Expand Up @@ -336,6 +372,10 @@ function it_generates_routes_from_resource_with_priority(
Route $route,
RouteCollection $routeCollection
): void {
if (\PHP_VERSION_ID < 80000) {
return;
}

$resourceRegistry->get(Argument::cetera())->willReturn($metadata);
$metadata->getPluralName()->willReturn('books');
$metadata->getServiceId('controller')->willReturn('app.controller.book');
Expand Down Expand Up @@ -363,6 +403,10 @@ function it_generates_routes_from_resource_with_options(
Route $route,
RouteCollection $routeCollection
): void {
if (\PHP_VERSION_ID < 80000) {
return;
}

$resourceRegistry->get(Argument::cetera())->willReturn($metadata);
$metadata->getPluralName()->willReturn('books');
$metadata->getServiceId('controller')->willReturn('app.controller.book');
Expand Down Expand Up @@ -393,6 +437,10 @@ function it_generates_routes_from_resource_with_host(
Route $route,
RouteCollection $routeCollection
): void {
if (\PHP_VERSION_ID < 80000) {
return;
}

$resourceRegistry->get(Argument::cetera())->willReturn($metadata);
$metadata->getPluralName()->willReturn('books');
$metadata->getServiceId('controller')->willReturn('app.controller.book');
Expand Down Expand Up @@ -420,6 +468,10 @@ function it_generates_routes_from_resource_with_schemes(
Route $route,
RouteCollection $routeCollection
): void {
if (\PHP_VERSION_ID < 80000) {
return;
}

$resourceRegistry->get(Argument::cetera())->willReturn($metadata);
$metadata->getPluralName()->willReturn('books');
$metadata->getServiceId('controller')->willReturn('app.controller.book');
Expand Down

0 comments on commit 66f8512

Please sign in to comment.