From 66f85124135b112dc9b056458dcc2cdd06126d5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Fr=C3=A9mont?= Date: Tue, 16 Nov 2021 10:41:07 +0100 Subject: [PATCH] Make mapping paths configurable --- .../DependencyInjection/Configuration.php | 9 ++++ .../SyliusResourceExtension.php | 1 + .../Resources/config/services/routing.xml | 2 +- src/Bundle/Routing/AttributesLoader.php | 20 ++++--- .../Tests/Configuration/ConfigurationTest.php | 42 +++++++++++++++ .../SyliusResourceExtensionTest.php | 21 ++++++++ .../spec/Routing/AttributesLoaderSpec.php | 54 ++++++++++++++++++- 7 files changed, 140 insertions(+), 9 deletions(-) diff --git a/src/Bundle/DependencyInjection/Configuration.php b/src/Bundle/DependencyInjection/Configuration.php index 0a85bce00..ca007105c 100644 --- a/src/Bundle/DependencyInjection/Configuration.php +++ b/src/Bundle/DependencyInjection/Configuration.php @@ -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() diff --git a/src/Bundle/DependencyInjection/SyliusResourceExtension.php b/src/Bundle/DependencyInjection/SyliusResourceExtension.php index 92d06bd2d..fec4372ab 100644 --- a/src/Bundle/DependencyInjection/SyliusResourceExtension.php +++ b/src/Bundle/DependencyInjection/SyliusResourceExtension.php @@ -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']); diff --git a/src/Bundle/Resources/config/services/routing.xml b/src/Bundle/Resources/config/services/routing.xml index 8e2617917..fbca04804 100644 --- a/src/Bundle/Resources/config/services/routing.xml +++ b/src/Bundle/Resources/config/services/routing.xml @@ -25,7 +25,7 @@ - %kernel.project_dir%/src/Entity + %sylius.resource.mapping% diff --git a/src/Bundle/Routing/AttributesLoader.php b/src/Bundle/Routing/AttributesLoader.php index 1d6268fe9..fd713f994 100644 --- a/src/Bundle/Routing/AttributesLoader.php +++ b/src/Bundle/Routing/AttributesLoader.php @@ -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; } @@ -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 diff --git a/src/Bundle/Tests/Configuration/ConfigurationTest.php b/src/Bundle/Tests/Configuration/ConfigurationTest.php index 22fcb3a2f..b3b819d7d 100644 --- a/src/Bundle/Tests/Configuration/ConfigurationTest.php +++ b/src/Bundle/Tests/Configuration/ConfigurationTest.php @@ -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 */ diff --git a/src/Bundle/Tests/DependencyInjection/SyliusResourceExtensionTest.php b/src/Bundle/Tests/DependencyInjection/SyliusResourceExtensionTest.php index 733d2625a..5569ce293 100644 --- a/src/Bundle/Tests/DependencyInjection/SyliusResourceExtensionTest.php +++ b/src/Bundle/Tests/DependencyInjection/SyliusResourceExtensionTest.php @@ -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 [ diff --git a/src/Bundle/spec/Routing/AttributesLoaderSpec.php b/src/Bundle/spec/Routing/AttributesLoaderSpec.php index 27ed2f930..27024fa1d 100644 --- a/src/Bundle/spec/Routing/AttributesLoaderSpec.php +++ b/src/Bundle/spec/Routing/AttributesLoaderSpec.php @@ -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()) ); } @@ -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'); @@ -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'); @@ -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'); @@ -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'); @@ -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'); @@ -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'); @@ -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'); @@ -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'); @@ -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'); @@ -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'); @@ -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'); @@ -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'); @@ -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');