Skip to content

Commit

Permalink
Merge pull request #53 from ruudk/auto-register-all-public-methods
Browse files Browse the repository at this point in the history
AutoRegister all public methods
  • Loading branch information
cmodijk authored Nov 9, 2017
2 parents e46f51e + 11334c2 commit c46a5cb
Show file tree
Hide file tree
Showing 14 changed files with 189 additions and 44 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ phpunit.xml
composer.lock
.couscous/
/build/
/coverage.xml
56 changes: 41 additions & 15 deletions src/DependencyInjection/Compiler/AutoRegister.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,35 +23,61 @@ public function process(ContainerBuilder $container)
{
foreach ($container->findTaggedServiceIds($this->tagName) as $serviceId => $tags) {
foreach ($tags as $tagAttributes) {

// if tag attributes are set, skip
// if tag attribute is set, skip
if (isset($tagAttributes[$this->tagAttribute])) {
continue;
}

$registerPublicMethods = false;
if (isset($tagAttributes['register_public_methods']) && true === $tagAttributes['register_public_methods']) {
$registerPublicMethods = true;
}

$definition = $container->getDefinition($serviceId);

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

// if no __invoke method, skip
if (!$reflectionClass->hasMethod('__invoke')) {
continue;
}
$methods = $reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC);

$tagAttributes = [];
foreach ($methods as $method) {
if (true === $method->isConstructor()) {
continue;
}

$invokeParameters = $reflectionClass->getMethod('__invoke')->getParameters();
if (true === $method->isDestructor()) {
continue;
}

// if no param or optional param, skip
if (count($invokeParameters) !== 1 || $invokeParameters[0]->isOptional()) {
return;
if (false === $registerPublicMethods && '__invoke' !== $method->getName()) {
continue;
}

$parameters = $method->getParameters();

// if no param or optional param, skip
if (count($parameters) !== 1 || $parameters[0]->isOptional()) {
continue;
}

// get the class name
$handles = $parameters[0]->getClass()->getName();

$tagAttributes[] = [
$this->tagAttribute => $handles,
'method' => $method->getName()
];
}

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

// auto handle
$definition->clearTag($this->tagName);
$definition->addTag($this->tagName, [$this->tagAttribute => $handles]);
foreach ($tagAttributes as $attributes) {
$definition->addTag($this->tagName, $attributes);
}
}
}
}
}
Expand Down
57 changes: 49 additions & 8 deletions tests/Functional/SmokeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Tools\SchemaTool;
use SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Auto\AutoCommand;
use SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Auto\AutoEvent;
use SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Auto\AutoCommand1;
use SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Auto\AutoCommand2;
use SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Auto\AutoEvent1;
use SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Auto\AutoEvent2;
use SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Auto\AutoEvent3;
use SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\TestCommand;
use SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\TestKernel;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
Expand Down Expand Up @@ -52,13 +55,13 @@ public function it_handles_a_command_then_dispatches_events_for_all_modified_ent
/**
* @test
*/
public function it_can_auto_register_event_subscribers()
public function it_can_auto_register_event_subscribers_using_invoke()
{
self::bootKernel(['environment' => 'config2']);
$container = self::$kernel->getContainer();

$subscriber = $container->get('auto_event_subscriber');
$event = new AutoEvent();
$subscriber = $container->get('auto_event_subscriber_using_invoke');
$event = new AutoEvent1();

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

Expand All @@ -70,13 +73,51 @@ public function it_can_auto_register_event_subscribers()
/**
* @test
*/
public function it_can_auto_register_command_handlers()
public function it_can_auto_register_event_subscribers_using_public_method()
{
self::bootKernel(['environment' => 'config2']);
$container = self::$kernel->getContainer();

$handler = $container->get('auto_command_handler');
$command = new AutoCommand();
$subscriber = $container->get('auto_event_subscriber_using_public_method');
$event2 = new AutoEvent2();
$event3 = new AutoEvent3();

$this->assertEmpty($subscriber->handled);

$container->get('event_bus')->handle($event2);
$container->get('event_bus')->handle($event3);

$this->assertSame([$event2, $event3], $subscriber->handled);
}

/**
* @test
*/
public function it_can_auto_register_command_handlers_using_invoke()
{
self::bootKernel(['environment' => 'config2']);
$container = self::$kernel->getContainer();

$handler = $container->get('auto_command_handler_using_invoke');
$command = new AutoCommand1();

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

$container->get('command_bus')->handle($command);

$this->assertSame($command, $handler->handled);
}

/**
* @test
*/
public function it_can_auto_register_command_handlers_using_public_method()
{
self::bootKernel(['environment' => 'config2']);
$container = self::$kernel->getContainer();

$handler = $container->get('auto_command_handler_using_public_method');
$command = new AutoCommand2();

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

Expand Down
7 changes: 7 additions & 0 deletions tests/Functional/SmokeTest/Auto/AutoCommand1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

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

final class AutoCommand1
{
}
7 changes: 7 additions & 0 deletions tests/Functional/SmokeTest/Auto/AutoCommand2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

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

final class AutoCommand2
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

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

final class AutoCommandHandler
final class AutoCommandHandlerUsingInvoke
{
public $handled;

public function __invoke(AutoCommand $command)
public function __invoke(AutoCommand1 $command)
{
$this->handled = $command;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

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

final class AutoCommandHandlerUsingPublicMethod
{
public $handled;

public function someHandleMethod(AutoCommand2 $command)
{
$this->handled = $command;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

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

final class AutoEvent
final class AutoEvent1
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

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

final class AutoCommand
final class AutoEvent2
{
}
7 changes: 7 additions & 0 deletions tests/Functional/SmokeTest/Auto/AutoEvent3.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

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

final class AutoEvent3
{
}
13 changes: 0 additions & 13 deletions tests/Functional/SmokeTest/Auto/AutoEventSubscriber.php

This file was deleted.

18 changes: 18 additions & 0 deletions tests/Functional/SmokeTest/Auto/AutoEventSubscriberUsingInvoke.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

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

final class AutoEventSubscriberUsingInvoke
{
public $handled;

public function __invoke(AutoEvent1 $event)
{
$this->handled = $event;
}

public function randomPublicMethod($value)
{

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

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

final class AutoEventSubscriberUsingPublicMethod
{
public $handled = [];

public function __construct()
{

}

public function __destruct()
{

}

public function someEventHandler(AutoEvent2 $event)
{
$this->handled[] = $event;
}

public function someOtherEventHandler(AutoEvent3 $event)
{
$this->handled[] = $event;
}
}
18 changes: 14 additions & 4 deletions tests/Functional/SmokeTest/config2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,22 @@ imports:
- { resource: config.yml }

services:
auto_command_handler:
class: SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Auto\AutoCommandHandler
auto_command_handler_using_invoke:
class: SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Auto\AutoCommandHandlerUsingInvoke
tags:
- { name: command_handler }

auto_event_subscriber:
class: SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Auto\AutoEventSubscriber
auto_command_handler_using_public_method:
class: SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Auto\AutoCommandHandlerUsingPublicMethod
tags:
- { name: command_handler, register_public_methods: true }

auto_event_subscriber_using_invoke:
class: SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Auto\AutoEventSubscriberUsingInvoke
tags:
- { name: event_subscriber }

auto_event_subscriber_using_public_method:
class: SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Auto\AutoEventSubscriberUsingPublicMethod
tags:
- { name: event_subscriber, register_public_methods: true }

0 comments on commit c46a5cb

Please sign in to comment.