Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Register listener events using typehints #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions src/Command/DebugCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerInterface;
use ReflectionClass;

class DebugCommand extends Command
{
Expand All @@ -28,7 +28,36 @@ public function __construct(ContainerInterface $container, array $listeners, arr
$listener = $container->get($serviceId);
$method = array_key_exists('method', $tag) ? $tag['method'] : '__invoke';

$events[$tag['event']][] = get_class($listener) . '::' . $method;
if (isset($tag['event'])) {
$events[$tag['event']][] = get_class($listener) . '::' . $method;
}

if (isset($tag['typehints'])) {
$reflClass = new ReflectionClass($container->getParameterBag()->resolveValue($listener));

foreach ($reflClass->getMethods() as $method) {
if (!$method->isPublic()
|| $method->isConstructor()
|| $method->isStatic()
|| $method->isAbstract()
|| $method->isVariadic()
|| $method->getNumberOfParameters() !== 1
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we handle methods like __get() here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I would not know how to do it. I did a tribute (copy + paste + adapt) to the typehint function that uses the tactician-bundle https://github.com/thephpleague/tactician-bundle/blob/bf10a37e27e4306bb9de5d909dba36226aa8e3c4/src/DependencyInjection/HandlerMapping/TypeHintMapping.php

) {
continue;
}
$parameter = $method->getParameters()[0];
if (!$parameter->hasType()
|| $parameter->getType()->isBuiltin()
|| $parameter->getClass()->isInterface()
) {
continue;
}

$event = (string)$parameter->getType();

$events[$event][] = get_class($listener) . '::' . $method->getName();
}
}
}
}

Expand Down
101 changes: 84 additions & 17 deletions src/DependencyInjection/TacticianDomainEventExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace BornFree\TacticianDomainEventBundle\DependencyInjection;

use ReflectionClass;
use BornFree\TacticianDoctrineDomainEvent\EventListener\CollectsEventsFromAllEntitiesManagedByUnitOfWork;
use BornFree\TacticianDoctrineDomainEvent\EventListener\CollectsEventsFromEntities;
use Symfony\Component\DependencyInjection\Definition;
Expand Down Expand Up @@ -40,32 +41,98 @@ private function addListeners(ContainerBuilder $container)
$definition = $container->getDefinition('tactician_domain_events.dispatcher');
$taggedServices = $container->findTaggedServiceIds('tactician.event_listener');

$listeners = array_merge(
$this->findListenersForEvent($taggedServices),
$this->findListenersForTypeHints($container, $taggedServices)
);

foreach($listeners as $item) {
if (!class_exists($item['event'])) {
throw new \Exception(
sprintf(
'Class %s registered as an event class in %s does not exist',
ravedok marked this conversation as resolved.
Show resolved Hide resolved
$item['event'],
$item['id']
)
);
}

$listener = $item['method']
? [new Reference($item['id']), $item['method']]
: new Reference($item['id']);

$definition->addMethodCall('addListener', [
$item['event'],
$listener
]);

}
}

private function findListenersForEvent($taggedServices): array
{
$listeners = [];

foreach ($taggedServices as $id => $tags) {
foreach ($tags as $attributes) {
if (!isset($attributes['event'])) {
throw new \Exception('The tactician.event_listener tag must always have an event attribute');
continue;
}

if (!class_exists($attributes['event'])) {
throw new \Exception(
sprintf(
'Class %s registered as an event class in %s does not exist',
$attributes['event'],
$id
)
);
}
$listeners[] = [
'id' => $id,
'event' => $attributes['event'],
'method' => $attributes['method'] ?? null
];
}
}

return $listeners;
}

$listener = array_key_exists('method', $attributes)
? [new Reference($id), $attributes['method']]
: new Reference($id);
private function findListenersForTypeHints(ContainerBuilder $container, $taggedServices): array
{
$listeners = [];

$definition->addMethodCall('addListener', [
$attributes['event'],
$listener
]);
foreach ($taggedServices as $id => $tags) {
foreach ($tags as $attributes) {
if (!isset($attributes['typehints']) || $attributes['typehints'] !== true) {
continue;
}

$reflClass = new ReflectionClass($container->getParameterBag()->resolveValue($container->getDefinition($id)->getClass()));

foreach ($reflClass->getMethods() as $method) {
if (!$method->isPublic()
|| $method->isConstructor()
|| $method->isStatic()
|| $method->isAbstract()
|| $method->isVariadic()
|| $method->getNumberOfParameters() !== 1
) {
continue;
}
$parameter = $method->getParameters()[0];
// dump($parameter);
ravedok marked this conversation as resolved.
Show resolved Hide resolved
if (!$parameter->hasType()
|| $parameter->getType()->isBuiltin()
|| $parameter->getClass()->isInterface()
) {
continue;
}

$event = (string)$parameter->getType();

$listeners[] = [
'id' => $id,
'event' => $event,
'method' => $method->getName()
];
}
}
}

return $listeners;
}

private function addSubscribers(ContainerBuilder $container)
Expand Down