Example #1
0
 /**
  * Run the application. If there is an exception thrown send an 500.
  *
  * @param Request $request
  */
 public function run(Request $request)
 {
     try {
         $this->handleRequest($request);
     } catch (Exception $e) {
         $logger = $this->masterFactory->getDebugFactory()->getErrorLogger();
         $logger->error($e->getMessage() . PHP_EOL . $e->getTraceAsString());
         header(sprintf('Status: %s', Response::getMessageForCode(500)));
         if (Settings::isDevMode()) {
             echo $e->getMessage() . PHP_EOL;
             echo '<pre>';
             echo $e->getTraceAsString();
             echo '</pre>';
         }
     }
 }
Example #2
0
 /**
  * @param string $action
  *
  * @throws NotExistsException
  *
  * @return Response
  */
 public function handle($action)
 {
     if (!method_exists($this, $action)) {
         throw new NotExistsException('Method ' . $action . ' does not exist');
     }
     if ((int) substr($this->getStatus(), 0, 1) === 2 || (int) substr($this->getStatus(), 0, 1) === 3) {
         // Call hooks
         if ($this->hasAccess()) {
             $this->preAction();
             call_user_func([$this, $action]);
             $this->postAction();
         }
         if (!$this->isHttpMethodAllowed()) {
             $this->setStatus(405);
             $body = Response::getMessageForCode($this->getStatus());
         } else {
             $this->response->setHeader('Content-Type', $this->getView()->getContentType());
             $body = $this->getView()->render();
         }
     } else {
         $body = Response::getMessageForCode($this->getStatus());
     }
     $this->response->setBody($body)->setStatus($this->getStatus());
     return $this->response;
 }