Example #1
0
 /**
  * {@inheritdoc}
  *
  * @return int Exit status (AbstractApplication::STATUS_... constants)
  */
 public function run() : int
 {
     try {
         $this->executeHook(self::BEFORE_FIND_ROUTE);
         $this->loadRouter();
         $this->loadRequest();
         $this->loadResponse();
         $this->route = $this->router->findRoute($this->request);
         $this->executeHook(self::BEFORE_CALL_ACTION);
         $this->callAction($this->route['controller'], $this->route['action']);
         $exitStatus = self::STATUS_SUCCESS;
     } catch (RouteNotFoundException $exception) {
         $this->route['controller'] = $this->router->normalizeController($this->config['not_found_route']['controller']);
         $this->route['action'] = $this->router->normalizeAction($this->config['not_found_route']['action']);
         $this->route['params']['exception'] = $exception;
         $this->response->withStatus(404);
         if (isset($this->config['exception_handler'])) {
             call_user_func($this->config['exception_handler'], $exception, $this->response);
         }
         $this->callAction($this->route['controller'], $this->route['action']);
         $exitStatus = self::STATUS_ACTION_NOT_FOUND;
     } catch (\Throwable $exception) {
         $this->route['controller'] = $this->router->normalizeController($this->config['error_route']['controller']);
         $this->route['action'] = $this->router->normalizeAction($this->config['error_route']['action']);
         $this->route['params']['exception'] = $exception;
         $this->route['params']['show_errors'] = $this->config['show_errors'];
         $this->response->withStatus(503);
         if (isset($this->config['exception_handler'])) {
             call_user_func($this->config['exception_handler'], $exception, $this->response);
         }
         $this->callAction($this->route['controller'], $this->route['action']);
         $exitStatus = self::STATUS_ERROR;
     } finally {
         $this->finishTime = microtime(true);
     }
     $this->executeHook(self::BEFORE_SEND_RESPONSE);
     //TODO debug
     $this->response->getBody()->write(sprintf('<p>Time: %0.7f</p>', $this->finishTime - $_SERVER['REQUEST_TIME_FLOAT']));
     $this->response->getBody()->write(sprintf('<p>Memory: %0.2fM</p>', memory_get_peak_usage(true) / (1024 * 1024)));
     $this->response->getBody()->write(sprintf('<p>Included files: %d</p>', count(get_included_files())));
     $this->response->getBody()->write(sprintf('<p>Included files:</p><pre>%s</pre>', var_export(get_included_files(), true)));
     $this->sendResponse();
     $this->executeHook(self::AFTER_SEND_RESPONSE);
     return $exitStatus;
 }