From 3e5ef7e86cbc2fdb32b45867aad30c6cf761d345 Mon Sep 17 00:00:00 2001 From: Robin Chalas Date: Fri, 20 Oct 2017 00:57:30 +0200 Subject: [PATCH] Resolve parameters in TypeHintMapping/ClassNameMapping --- .../HandlerMapping/ClassNameMapping.php | 9 +++++---- .../HandlerMapping/TagBasedMapping.php | 8 ++++---- .../HandlerMapping/TypeHintMapping.php | 7 ++++--- .../HandlerMapping/ClassNameMappingTest.php | 13 +++++++++++++ .../HandlerMapping/TypeHintMappingTest.php | 13 +++++++++++++ 5 files changed, 39 insertions(+), 11 deletions(-) diff --git a/src/DependencyInjection/HandlerMapping/ClassNameMapping.php b/src/DependencyInjection/HandlerMapping/ClassNameMapping.php index fb3f2a9..e16ca13 100644 --- a/src/DependencyInjection/HandlerMapping/ClassNameMapping.php +++ b/src/DependencyInjection/HandlerMapping/ClassNameMapping.php @@ -3,19 +3,20 @@ namespace League\Tactician\Bundle\DependencyInjection\HandlerMapping; +use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Definition; final class ClassNameMapping extends TagBasedMapping { - protected function isSupported(Definition $definition, array $tagAttributes): bool + protected function isSupported(ContainerBuilder $container, Definition $definition, array $tagAttributes): bool { - return isset($tagAttributes['command']) && class_exists($tagAttributes['command']); + return isset($tagAttributes['command']) && class_exists($container->getParameterBag()->resolveValue($tagAttributes['command'])); } - protected function findCommandsForService(Definition $definition, array $tagAttributes): array + protected function findCommandsForService(ContainerBuilder $container, Definition $definition, array $tagAttributes): array { return [ - $tagAttributes['command'] + $container->getParameterBag()->resolveValue($tagAttributes['command']) ]; } } diff --git a/src/DependencyInjection/HandlerMapping/TagBasedMapping.php b/src/DependencyInjection/HandlerMapping/TagBasedMapping.php index 635c65a..483c137 100644 --- a/src/DependencyInjection/HandlerMapping/TagBasedMapping.php +++ b/src/DependencyInjection/HandlerMapping/TagBasedMapping.php @@ -29,11 +29,11 @@ private function mapServiceByTag(ContainerBuilder $container, Routing $routing, { $definition = $container->getDefinition($serviceId); - if (!$this->isSupported($definition, $attributes)) { + if (!$this->isSupported($container, $definition, $attributes)) { return; } - foreach ($this->findCommandsForService($definition, $attributes) as $commandClassName) { + foreach ($this->findCommandsForService($container, $definition, $attributes) as $commandClassName) { if (isset($attributes['bus'])) { $routing->routeToBus($attributes['bus'], $commandClassName, $serviceId); } else { @@ -42,7 +42,7 @@ private function mapServiceByTag(ContainerBuilder $container, Routing $routing, } } - abstract protected function isSupported(Definition $definition, array $tagAttributes): bool; + abstract protected function isSupported(ContainerBuilder $container, Definition $definition, array $tagAttributes): bool; - abstract protected function findCommandsForService(Definition $definition, array $tagAttributes): array; + abstract protected function findCommandsForService(ContainerBuilder $container, Definition $definition, array $tagAttributes): array; } diff --git a/src/DependencyInjection/HandlerMapping/TypeHintMapping.php b/src/DependencyInjection/HandlerMapping/TypeHintMapping.php index 5550ef1..452fb41 100644 --- a/src/DependencyInjection/HandlerMapping/TypeHintMapping.php +++ b/src/DependencyInjection/HandlerMapping/TypeHintMapping.php @@ -3,6 +3,7 @@ namespace League\Tactician\Bundle\DependencyInjection\HandlerMapping; +use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Definition; use ReflectionClass; @@ -28,16 +29,16 @@ */ final class TypeHintMapping extends TagBasedMapping { - protected function isSupported(Definition $definition, array $tagAttributes): bool + protected function isSupported(ContainerBuilder $container, Definition $definition, array $tagAttributes): bool { return isset($tagAttributes['typehints']) && $tagAttributes['typehints'] === true; } - protected function findCommandsForService(Definition $definition, array $tagAttributes): array + protected function findCommandsForService(ContainerBuilder $container, Definition $definition, array $tagAttributes): array { $results = []; - $reflClass = new ReflectionClass($definition->getClass()); + $reflClass = new ReflectionClass($container->getParameterBag()->resolveValue($definition->getClass())); foreach ($reflClass->getMethods() as $method) { diff --git a/tests/DependencyInjection/HandlerMapping/ClassNameMappingTest.php b/tests/DependencyInjection/HandlerMapping/ClassNameMappingTest.php index b652751..55893fc 100644 --- a/tests/DependencyInjection/HandlerMapping/ClassNameMappingTest.php +++ b/tests/DependencyInjection/HandlerMapping/ClassNameMappingTest.php @@ -27,6 +27,19 @@ public function test_will_skip_definitions_without_command_tag() $this->assertEquals([], $routing->commandToServiceMapping('default')); } + public function test_will_resolve_parameters_in_command_attribute() + { + $builder = new ContainerBuilder(); + $builder->setParameter('fake_command_class', FakeCommand::class); + $builder + ->setDefinition('some.handler', new Definition(SomeHandler::class)) + ->addTag('tactician.handler', ['command' => '%fake_command_class%']); + + $routing = (new ClassNameMapping())->build($builder, new Routing(['default'])); + + $this->assertEquals([FakeCommand::class => 'some.handler'], $routing->commandToServiceMapping('default')); + } + public function test_will_find_handler_for_defined_command() { $builder = new ContainerBuilder(); diff --git a/tests/DependencyInjection/HandlerMapping/TypeHintMappingTest.php b/tests/DependencyInjection/HandlerMapping/TypeHintMappingTest.php index fdb2eac..da304e7 100644 --- a/tests/DependencyInjection/HandlerMapping/TypeHintMappingTest.php +++ b/tests/DependencyInjection/HandlerMapping/TypeHintMappingTest.php @@ -27,6 +27,19 @@ public function test_will_skip_definitions_without_auto_tag() $this->assertEquals([], $routing->commandToServiceMapping('default')); } + public function test_will_resolve_parameters_in_handler_class() + { + $builder = new ContainerBuilder(); + $builder->setParameter('handler_class', InvokeHandler::class); + $builder + ->setDefinition('some.handler', new Definition('%handler_class%')) + ->addTag('tactician.handler', ['typehints' => true]); + + $routing = (new TypeHintMapping())->build($builder, new Routing(['default'])); + + $this->assertEquals([FakeCommand::class => 'some.handler'], $routing->commandToServiceMapping('default')); + } + /** * @dataProvider simpleTestCases */