Symfony Example =============== This document shows an example of how to implement [example1.php](https://github.com/beberlei/litecqrs-php/blob/master/example/example1.php) as part of a Symfony project. Service Definition ------------------ ```xml Acme\DemoBundle\EventHandlers\MyEventHandler Acme\DemoBundle\Services\UserService ``` User.php -------- ```php raise(new DomainObjectChanged("ChangeEmail", array("email" => $email, "oldEmail" => $this->email))); $this->email = $email; } } ``` ChangeEmailCommand.php ---------------------- ```php map = $map; } public function changeEmail(ChangeEmailCommand $command) { $user = $this->findUserById($command->id); $user->changeEmail($command->email); } 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]; } } ``` MyEventHandler.php ------------------ ```php oldEmail . " to " . $event->email . "\n"; } } ``` Usage Example, via a Symfony Command ------------------------------------ Defined commands and events can be displayed: ```bash $ php app/console lite-cqrs:debug COMMANDS ======== Command-Handler Service Command Class test.command.user_service_commands ChangeEmailCommand Acme\DemoBundle\Model\Command\ChangeEmailCommand EVENTS ====== Event-Handler Service Event Class test.command.event_handler ChangeEmail Acme\DemoBundle\EventHandlers\MyEventHandler ``` Create a Symfony command to execute your ChangeEmailCommand ```php setName('acme:demo:change-email') ->setDescription('Change Email') ->addArgument('new_email', InputArgument::REQUIRED, 'Change the email to what?') ; } protected function execute(InputInterface $input, OutputInterface $output) { $commandBus = $this->getContainer()->get('command_bus'); $commandBus->handle(new ChangeEmailCommand(array('id' => 1234, 'email' => $input->getArgument('new_email')))); } } ``` Run the Symfony command: ```bash $ php app/console acme:demo:change-email info@beberlei.de E-Mail changed from old@example.com to info@beberlei.de ```