Skip to content

Commit c46a5cb

Browse files
authored
Merge pull request #53 from ruudk/auto-register-all-public-methods
AutoRegister all public methods
2 parents e46f51e + 11334c2 commit c46a5cb

14 files changed

+189
-44
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ phpunit.xml
33
composer.lock
44
.couscous/
55
/build/
6+
/coverage.xml

src/DependencyInjection/Compiler/AutoRegister.php

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,35 +23,61 @@ public function process(ContainerBuilder $container)
2323
{
2424
foreach ($container->findTaggedServiceIds($this->tagName) as $serviceId => $tags) {
2525
foreach ($tags as $tagAttributes) {
26-
27-
// if tag attributes are set, skip
26+
// if tag attribute is set, skip
2827
if (isset($tagAttributes[$this->tagAttribute])) {
2928
continue;
3029
}
3130

31+
$registerPublicMethods = false;
32+
if (isset($tagAttributes['register_public_methods']) && true === $tagAttributes['register_public_methods']) {
33+
$registerPublicMethods = true;
34+
}
35+
3236
$definition = $container->getDefinition($serviceId);
3337

3438
// check if service id is class name
3539
$reflectionClass = new \ReflectionClass($definition->getClass() ?: $serviceId);
3640

37-
// if no __invoke method, skip
38-
if (!$reflectionClass->hasMethod('__invoke')) {
39-
continue;
40-
}
41+
$methods = $reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC);
42+
43+
$tagAttributes = [];
44+
foreach ($methods as $method) {
45+
if (true === $method->isConstructor()) {
46+
continue;
47+
}
4148

42-
$invokeParameters = $reflectionClass->getMethod('__invoke')->getParameters();
49+
if (true === $method->isDestructor()) {
50+
continue;
51+
}
4352

44-
// if no param or optional param, skip
45-
if (count($invokeParameters) !== 1 || $invokeParameters[0]->isOptional()) {
46-
return;
53+
if (false === $registerPublicMethods && '__invoke' !== $method->getName()) {
54+
continue;
55+
}
56+
57+
$parameters = $method->getParameters();
58+
59+
// if no param or optional param, skip
60+
if (count($parameters) !== 1 || $parameters[0]->isOptional()) {
61+
continue;
62+
}
63+
64+
// get the class name
65+
$handles = $parameters[0]->getClass()->getName();
66+
67+
$tagAttributes[] = [
68+
$this->tagAttribute => $handles,
69+
'method' => $method->getName()
70+
];
4771
}
4872

49-
// get the class name
50-
$handles = $invokeParameters[0]->getClass()->getName();
73+
if (count($tags) !== 0) {
74+
// auto handle
75+
$definition->clearTag($this->tagName);
5176

52-
// auto handle
53-
$definition->clearTag($this->tagName);
54-
$definition->addTag($this->tagName, [$this->tagAttribute => $handles]);
77+
foreach ($tagAttributes as $attributes) {
78+
$definition->addTag($this->tagName, $attributes);
79+
}
80+
}
5581
}
5682
}
5783
}

tests/Functional/SmokeTest.php

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44

55
use Doctrine\ORM\EntityManager;
66
use Doctrine\ORM\Tools\SchemaTool;
7-
use SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Auto\AutoCommand;
8-
use SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Auto\AutoEvent;
7+
use SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Auto\AutoCommand1;
8+
use SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Auto\AutoCommand2;
9+
use SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Auto\AutoEvent1;
10+
use SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Auto\AutoEvent2;
11+
use SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Auto\AutoEvent3;
912
use SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\TestCommand;
1013
use SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\TestKernel;
1114
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
@@ -52,13 +55,13 @@ public function it_handles_a_command_then_dispatches_events_for_all_modified_ent
5255
/**
5356
* @test
5457
*/
55-
public function it_can_auto_register_event_subscribers()
58+
public function it_can_auto_register_event_subscribers_using_invoke()
5659
{
5760
self::bootKernel(['environment' => 'config2']);
5861
$container = self::$kernel->getContainer();
5962

60-
$subscriber = $container->get('auto_event_subscriber');
61-
$event = new AutoEvent();
63+
$subscriber = $container->get('auto_event_subscriber_using_invoke');
64+
$event = new AutoEvent1();
6265

6366
$this->assertNull($subscriber->handled);
6467

@@ -70,13 +73,51 @@ public function it_can_auto_register_event_subscribers()
7073
/**
7174
* @test
7275
*/
73-
public function it_can_auto_register_command_handlers()
76+
public function it_can_auto_register_event_subscribers_using_public_method()
7477
{
7578
self::bootKernel(['environment' => 'config2']);
7679
$container = self::$kernel->getContainer();
7780

78-
$handler = $container->get('auto_command_handler');
79-
$command = new AutoCommand();
81+
$subscriber = $container->get('auto_event_subscriber_using_public_method');
82+
$event2 = new AutoEvent2();
83+
$event3 = new AutoEvent3();
84+
85+
$this->assertEmpty($subscriber->handled);
86+
87+
$container->get('event_bus')->handle($event2);
88+
$container->get('event_bus')->handle($event3);
89+
90+
$this->assertSame([$event2, $event3], $subscriber->handled);
91+
}
92+
93+
/**
94+
* @test
95+
*/
96+
public function it_can_auto_register_command_handlers_using_invoke()
97+
{
98+
self::bootKernel(['environment' => 'config2']);
99+
$container = self::$kernel->getContainer();
100+
101+
$handler = $container->get('auto_command_handler_using_invoke');
102+
$command = new AutoCommand1();
103+
104+
$this->assertNull($handler->handled);
105+
106+
$container->get('command_bus')->handle($command);
107+
108+
$this->assertSame($command, $handler->handled);
109+
}
110+
111+
/**
112+
* @test
113+
*/
114+
public function it_can_auto_register_command_handlers_using_public_method()
115+
{
116+
self::bootKernel(['environment' => 'config2']);
117+
$container = self::$kernel->getContainer();
118+
119+
$handler = $container->get('auto_command_handler_using_public_method');
120+
$command = new AutoCommand2();
80121

81122
$this->assertNull($handler->handled);
82123

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Auto;
4+
5+
final class AutoCommand1
6+
{
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Auto;
4+
5+
final class AutoCommand2
6+
{
7+
}

tests/Functional/SmokeTest/Auto/AutoCommandHandler.php renamed to tests/Functional/SmokeTest/Auto/AutoCommandHandlerUsingInvoke.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
namespace SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Auto;
44

5-
final class AutoCommandHandler
5+
final class AutoCommandHandlerUsingInvoke
66
{
77
public $handled;
88

9-
public function __invoke(AutoCommand $command)
9+
public function __invoke(AutoCommand1 $command)
1010
{
1111
$this->handled = $command;
1212
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Auto;
4+
5+
final class AutoCommandHandlerUsingPublicMethod
6+
{
7+
public $handled;
8+
9+
public function someHandleMethod(AutoCommand2 $command)
10+
{
11+
$this->handled = $command;
12+
}
13+
}

tests/Functional/SmokeTest/Auto/AutoEvent.php renamed to tests/Functional/SmokeTest/Auto/AutoEvent1.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
namespace SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Auto;
44

5-
final class AutoEvent
5+
final class AutoEvent1
66
{
77
}

tests/Functional/SmokeTest/Auto/AutoCommand.php renamed to tests/Functional/SmokeTest/Auto/AutoEvent2.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
namespace SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Auto;
44

5-
final class AutoCommand
5+
final class AutoEvent2
66
{
77
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Auto;
4+
5+
final class AutoEvent3
6+
{
7+
}

0 commit comments

Comments
 (0)