Skip to content

Commit

Permalink
Merge pull request #76 from chalasr/resolve-params
Browse files Browse the repository at this point in the history
Resolve parameters in TypeHintMapping/ClassNameMapping
  • Loading branch information
rosstuck authored Oct 20, 2017
2 parents 43bb25b + 3e5ef7e commit b8849df
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 11 deletions.
9 changes: 5 additions & 4 deletions src/DependencyInjection/HandlerMapping/ClassNameMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
];
}
}
8 changes: 4 additions & 4 deletions src/DependencyInjection/HandlerMapping/TagBasedMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
}
7 changes: 4 additions & 3 deletions src/DependencyInjection/HandlerMapping/TypeHintMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace League\Tactician\Bundle\DependencyInjection\HandlerMapping;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use ReflectionClass;

Expand All @@ -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) {

Expand Down
13 changes: 13 additions & 0 deletions tests/DependencyInjection/HandlerMapping/ClassNameMappingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
13 changes: 13 additions & 0 deletions tests/DependencyInjection/HandlerMapping/TypeHintMappingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit b8849df

Please sign in to comment.