/** * Runs the Application for a defined (or new) HTTP request * * @param Request $request The HTTP request (optional) * * @return mixed */ public function run(Request $request = null) { $this->notify(new BootEvent($this)); if (null === $request) { $request = Request::createFromGlobals(); } $context = new Context($request); try { $this->notify(new RequestEvent($request, $this, $context)); if (!$context->isReady()) { $this->notify(new DispatchEvent($this, $context)); } if (!$context->isReady()) { if (null === $this->defaultAction || $context->isError()) { throw new Exception('No action found'); } $context->setActionName($this->defaultAction); } $result = $this->runAction($context); if (!$context->isDone()) { if ($result instanceof Response) { $context->setResponse($result); } elseif (is_string($result)) { $context->setResponse(new Response($result)); } } if ($context->getResponse() instanceof Response) { $this->notify(new ResponseEvent($context->getResponse(), $this, $context)); } } catch (\Exception $exp) { $event = new ErrorEvent($exp, $this, $context); $this->notify($event); if (!$event->isStopped()) { throw $exp; } else { return null; } } $endEvent = new EndEvent($this, $context); $this->notify($endEvent); if ($endEvent->isStopped()) { return null; } if ($context->getResponse() instanceof Response) { return $context->getResponse(); } return $context->getResult(); }