/** * Dispatch a request * * @events dispatch.pre, dispatch.post * @param Request $request * @param null|Response $response * @param null|Event $e * @return Response|mixed */ public function dispatch(Request $request, Response $response = null, Event $e = null) { $this->request = $request; if (!$response) { $response = new HttpResponse(); } $this->response = $response; if ($e instanceof Event && !$e instanceof MvcEvent) { $eventParams = $e->getParams(); $e = new MvcEvent(); $e->setParams($eventParams); unset($eventParams); } if (null === $e) { $e = new MvcEvent(); } $e->setRequest($request) ->setResponse($response) ->setTarget($this); $this->event = $e; $result = $this->events()->trigger('dispatch', $e, function($test) { return ($test instanceof Response); }); if ($result->stopped()) { return $result->last(); } return $e->getResult(); }
/** * Set an event to use during dispatch * * By default, will re-cast to MvcEvent if another event type is provided. * * @param Event $e * @return void */ public function setEvent(Event $e) { if ($e instanceof Event && !$e instanceof MvcEvent) { $eventParams = $e->getParams(); $e = new MvcEvent(); $e->setParams($eventParams); unset($eventParams); } $this->event = $e; }