} } class String { public $value; } class Slugify extends DefaultCommand { public $string; } class Lower extends DefaultCommand { public $string; } class Ucfirst extends DefaultCommand { public $string; } class RemoveNonAscii extends DefaultCommand { public $string; } $commandBus = new DirectCommandBus(); $stringService = new StringManipulation($commandBus); $commandBus->register('MyApp\\Slugify', $stringService); $commandBus->register('MyApp\\Lower', $stringService); $commandBus->register('MyApp\\Ucfirst', $stringService); $commandBus->register('MyApp\\RemoveNonAscii', $stringService); $string = new String(); $string->value = isset($argv[1]) ? $argv[1] : "Hello World!"; $commandBus->handle(new Slugify(array("string" => $string)));
} private function findUserById($id) { if (!isset($this->users[$id])) { // here would normally be a database call or something $this->users[$id] = new User(); $this->map->add($this->users[$id]); } return $this->users[$id]; } } class MyEventHandler { public function onChangeEmail(DomainObjectChanged $event) { echo "E-Mail changed from " . $event->oldEmail . " to " . $event->email . "\n"; } } // 1. Setup the Library with InMemory Handlers $messageBus = new InMemoryEventMessageBus(); $identityMap = new SimpleIdentityMap(); $queue = new EventProviderQueue($identityMap); $commandBus = new DirectCommandBus(array(new EventMessageHandlerFactory($messageBus, $queue))); // 2. Register a command service and an event handler $userService = new UserService($identityMap, $messageBus); $someEventHandler = new MyEventHandler(); $commandBus->register('MyApp\\ChangeEmailCommand', $userService); $messageBus->register($someEventHandler); // 3. Invoke command! $commandBus->handle(new ChangeEmailCommand(array('id' => 1234, 'email' => '*****@*****.**'))); $commandBus->handle(new ChangeEmailCommand(array('id' => 1234, 'email' => '*****@*****.**')));
} class FieldMarked extends DefaultDomainEvent { public $field; public $player; } class MarkField extends DefaultCommand { public $field; public $player; } // 1. Setup the Library with InMemory Handlers $messageBus = new InMemoryEventMessageBus(); $identityMap = new SimpleIdentityMap(); $queue = new EventProviderQueue($identityMap); $commandBus = new DirectCommandBus(array(new EventMessageHandlerFactory($messageBus, $queue))); // 2. register $boardService = new BoardService($identityMap); $humanService = new HumanPlayerService($commandBus); $aiService = new ComputerPlayerService($commandBus); $uiService = new DisplayService(); $commandBus->register("TicTacToe\\MarkField", $boardService); $messageBus->register($aiService); $messageBus->register($uiService); $messageBus->register($humanService); while (!$humanService->gameOver()) { try { $humanService->ask(); } catch (CommandFailedStackException $e) { } }