Skip to content

Commit

Permalink
Merge pull request #2 from rezzza/feature/override-debug-event-dispat…
Browse files Browse the repository at this point in the history
…cher

Add CLI like `app/console debug:event-dispatcher` for our event-dispatcher
  • Loading branch information
tyx committed Mar 17, 2016
2 parents 3ab641d + 47de79e commit 37677b9
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,6 @@ $changeTracker = new ChangeTracker(
)
);
```

# EventDispatcher debug
To debug your own event dispatcher with Symfony, we add a CLI for you. You should register it as a service and use the `--service-id` option.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"suggest": {
"doctrine/common": "To use ORMRepository",
"symfony/event-dispatcher": "To use SymfonyEventBus",
"symfony/framework-bundle": "To use EventDispatcherDebugCommand",
"ext-redis": "To use Redis capabilities"
}
}
47 changes: 47 additions & 0 deletions src/UI/CLI/EventDispatcherDebugCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Rezzza\DomainEvent\UI\CLI;

use Symfony\Bundle\FrameworkBundle\Command\EventDispatcherDebugCommand as BaseEventDispatcherDebugCommand;
use Symfony\Bundle\FrameworkBundle\Console\Helper\DescriptorHelper;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

class EventDispatcherDebugCommand extends BaseEventDispatcherDebugCommand
{
protected function configure()
{
parent::configure();
$this->addOption('service-id', null, InputOption::VALUE_REQUIRED, 'The id of the service you want debug', 'event_dispatcher');
}

/**
* {@inheritdoc}
*
* @throws \LogicException
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$output = new SymfonyStyle($input, $output);
$dispatcher = $this->getContainer()->get($input->getOption('service-id'));

$options = array();
if ($event = $input->getArgument('event')) {
if (!$dispatcher->hasListeners($event)) {
$output->warning(sprintf('The event "%s" does not have any registered listeners.', $event));

return;
}

$options = array('event' => $event);
}

$helper = new DescriptorHelper();
$options['format'] = $input->getOption('format');
$options['raw_text'] = $input->getOption('raw');
$options['output'] = $output;
$helper->describe($output, $dispatcher, $options);
}
}

0 comments on commit 37677b9

Please sign in to comment.