/** * Constructs the application */ public function __construct() { $this->em = new EventManager(); $this->commands = array(); //handle dispatch by running the command $this->em->attach(self::EVENT_DISPATCH, function ($event) { //get the command $command = $event->getCommand(); $command->setEvent($event); //check for a command if (is_null($command)) { throw new \InvalidArgumentException('Command required'); } //attach the command to the event if ($command instanceof EventListenerInterface) { $command->attach($this->getEventManager()); } //run the command $exitCode = $command->dispatch($event); //detach the command from the event if ($command instanceof EventListenerInterface) { $command->detach($this->getEventManager()); } //set the exit code $event->setExitCode($exitCode); }); //handle exceptions by printing the messages $this->em->attach(self::EVENT_EXCEPTION, function ($event) { $this->printError($event, $event->getException()->getMessage()); }); }
/** * @inheritdoc */ public function attach(EventManager $em) { $em->attach(Application::EVENT_INTERRUPT, array($this, 'interrupt')); }