public function dispatch(Command $command) { //If beforeCommand fails abort if ($this->_eventsManager->fire('command:beforeCommand', $command) === false) { return false; } //If run the commands fails abort too if ($command->run($command->getParameters()) === false) { return false; } $this->_eventsManager->fire('command:afterCommand', $command); }
public function testListenDispatchEvents() { $dispatcherMock = $this->getMockBuilder(Dispatcher::class)->disableOriginalConstructor()->getMock(); $listener = $this->getMockBuilder(ListenerDispatch::class)->getMock(); $listener->expects($this->once())->method('beforeDispatchLoop'); $listener->expects($this->once())->method('beforeExecuteRoute'); // $listener->expects($this->once()) // ->method('afterExecuteRoute'); // $listener->expects($this->once()) // ->method('beforeNotFoundAction'); // $listener->expects($this->once()) // ->method('beforeException'); // $listener->expects($this->once()) // ->method('afterDispatchLoop'); $this->listener->listenDispatchEvents($listener); $this->em->fire('dispatch:beforeDispatchLoop', $dispatcherMock, null); $this->em->fire('dispatch:beforeExecuteRoute', $dispatcherMock, null); // $this->em->fire('dispatch:afterExecuteRoute', $dispatcherMock, null); // $this->em->fire('dispatch:beforeNotFoundAction', $dispatcherMock, null); // $this->em->fire('dispatch:beforeException', $dispatcherMock, null); // $this->em->fire('dispatch:afterDispatchLoop', $dispatcherMock, null); }
/** * Renders output file using renderer object * * @throws InvalidRendererException * @return string */ public function render() { if ($this->eventsManager instanceof ManagerInterface) { $this->eventsManager->fire('generator:beforeRender', $this); } if (!$this->renderer instanceof RendererInterface) { throw new InvalidRendererException(); } $output = $this->renderer->render(); if ($this->eventsManager instanceof ManagerInterface) { $this->eventsManager->fire('generator:afterRender', $this); } return $output; }
public static function fire($eventType, $source, $data = null, $cancelable = true) { static::$event or static::$event = static::$di->getShared('eventsManager'); return static::$event->fire($eventType, $source, $data, $cancelable); }
<?php use Phalcon\Events\Manager as EventsManager; $evManager = new EventsManager(); //Set up the events manager to collect responses $evManager->collectResponses(true); //Attach a listener $evManager->attach('custom:custom', function () { return 'first response'; }); //Attach a listener $evManager->attach('custom:custom', function () { return 'second response'; }); //Fire the event $evManager->fire('custom:custom', null); //Get all the collected responses print_r($evManager->getResponses());