<?php require_once __DIR__ . '/../bootstrap.php'; $eventDispatcher = new Broadway\EventDispatcher\EventDispatcher(); // You can register any callable $eventDispatcher->addListener('my_event', function ($arg1, $arg2) { echo "Arg1: {$arg1}\n"; echo "Arg2: {$arg2}\n"; }); // Dispatch with an array of arguments $eventDispatcher->dispatch('my_event', array('one', 'two'));
$udb2RepositoryDecorator->syncBackOn(); } return $udb2RepositoryDecorator; }); $app['execution_context_metadata_enricher'] = $app->share(function ($app) { return new \CultuurNet\UDB3\EventSourcing\ExecutionContextMetadataEnricher(); }); $app['event_stream_metadata_enricher'] = $app->share(function ($app) { $eventStreamDecorator = new \Broadway\EventSourcing\MetadataEnrichment\MetadataEnrichingEventStreamDecorator(); $eventStreamDecorator->registerEnricher($app['execution_context_metadata_enricher']); return $eventStreamDecorator; }); $app['command_bus_event_dispatcher'] = $app->share(function ($app) { $dispatcher = new \Broadway\EventDispatcher\EventDispatcher(); $dispatcher->addListener(\CultuurNet\UDB3\CommandHandling\ResqueCommandBus::EVENT_COMMAND_CONTEXT_SET, function ($context) use($app) { $app['execution_context_metadata_enricher']->setContext($context); }); return $dispatcher; }); $app['logger.command_bus'] = $app->share(function ($app) { $logger = new \Monolog\Logger('command_bus'); $handlers = $app['config']['log.command_bus']; foreach ($handlers as $handler_config) { switch ($handler_config['type']) { case 'hipchat': $handler = new \Monolog\Handler\HipChatHandler($handler_config['token'], $handler_config['room']); break; case 'file': $handler = new \Monolog\Handler\StreamHandler(__DIR__ . '/web/' . $handler_config['path']); break; case 'socketioemitter':
} class ExampleFailureCommand extends BaseCommand { } // Setup the system to handle commands $commandHandler = new ExampleCommandHandler(); $eventDispatcher = new Broadway\EventDispatcher\EventDispatcher(); $simpleCommandBus = new Broadway\CommandHandling\SimpleCommandBus(); $commandBus = new Broadway\CommandHandling\EventDispatchingCommandBus($simpleCommandBus, $eventDispatcher); $commandBus->subscribe($commandHandler); // Dependencies of auditing logger $logger = new StdoutLogger(); $commandSerializer = new Broadway\Auditing\CommandSerializer(); /* * The actual example! */ // setup the command logger $commandAuditLogger = new Broadway\Auditing\CommandLogger($logger, $commandSerializer); // register the command logger with the event dispatcher of the command bus $eventDispatcher->addListener("broadway.command_handling.command_success", array($commandAuditLogger, "onCommandHandlingSuccess")); $eventDispatcher->addListener("broadway.command_handling.command_failure", array($commandAuditLogger, "onCommandHandlingFailure")); echo "Dispatching the command that will succeed.\n"; $command = new ExampleCommand('Hi from command!'); $commandBus->dispatch($command); try { echo "Dispatching the command that will fail.\n"; $command = new ExampleFailureCommand('Hi from failure command!'); $commandBus->dispatch($command); } catch (Exception $e) { echo "See? It failed.\n"; }