Skip to content

Commit

Permalink
Fix PopulateDebugCommandPass to avoid getting services from container…
Browse files Browse the repository at this point in the history
… on compile time
  • Loading branch information
thatside-zaraffa committed Jul 6, 2018
1 parent 5f0b448 commit 1a5de36
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 28 deletions.
32 changes: 30 additions & 2 deletions src/Command/DebugCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

namespace BornFree\TacticianDomainEventBundle\Command;

use BornFree\TacticianDomainEvent\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Console\Command\Command;
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;

class DebugCommand extends Command
{
Expand All @@ -14,11 +17,36 @@ class DebugCommand extends Command
*/
private $mappings;

public function __construct(array $mappings)
public function __construct(ContainerInterface $container, array $listeners, array $subscribers)
{
parent::__construct();

$this->mappings = $mappings;
$events = [];

foreach ($listeners as $serviceId => $tags) {
foreach ($tags as $tag) {
$listener = $container->get($serviceId);
$method = array_key_exists('method', $tag) ? $tag['method'] : '__invoke';

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

foreach ($subscribers as $serviceId => $tags) {
$subscriber = $container->get($serviceId);

if (!$subscriber instanceof EventSubscriberInterface) {
continue;
}

foreach ($subscriber->getSubscribedEvents() as $event => $method) {
$events[$event][] = get_class($subscriber) . '::' . $method[1];
}
}

ksort($events);

$this->mappings = $events;
}

protected function configure()
Expand Down
27 changes: 2 additions & 25 deletions src/DependencyInjection/Compiler/PopulateDebugCommandPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,7 @@ public function process(ContainerBuilder $container)
$subscribers = $container->findTaggedServiceIds('tactician.event_subscriber');
$listeners = $container->findTaggedServiceIds('tactician.event_listener');

$events = [];

foreach ($listeners as $serviceId => $tags) {
foreach ($tags as $tag) {
$listener = $container->getDefinition($serviceId);
$method = array_key_exists('method', $tag) ? $tag['method'] : '__invoke';

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

foreach ($subscribers as $serviceId => $tags) {
$subscriber = $container->get($serviceId);

if (!$subscriber instanceof EventSubscriberInterface) {
continue;
}

foreach ($subscriber->getSubscribedEvents() as $event => $method) {
$events[$event][] = get_class($subscriber) . '::' . $method[1];
}
}

ksort($events);
$command->setArgument(0, $events);
$command->setArgument(1, $listeners);
$command->setArgument(2, $subscribers);
}
}
4 changes: 3 additions & 1 deletion src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ services:
tactician_domain_events.dispatcher:
class: BornFree\TacticianDomainEvent\EventDispatcher\EventDispatcher
lazy: true

tactician_domain_events.middleware.release_recorded_events:
class: BornFree\TacticianDomainEvent\Middleware\ReleaseRecordedEventsMiddleware
arguments: ['@tactician_domain_events.doctrine.event_collector', '@tactician_domain_events.dispatcher']
lazy: true

tactician_domain_events.command.debug:
class: BornFree\TacticianDomainEventBundle\Command\DebugCommand
arguments:
- '@service_container'
tags:
- { name: console.command }

0 comments on commit 1a5de36

Please sign in to comment.