Пример #1
0
 public static function handle($e, $send_response = true)
 {
     try {
         Hook::triggerAction('exception.handle', array(&$e));
         $log_message = sprintf('%-4s %s', "({$e->getCode()})", $e->getMessage());
         // get the channel manually so the introspection works properly.
         $level = Log::ERROR;
         if ($e instanceof ErrorException && isset(self::$levels[$e->getSeverity()])) {
             $level = self::$levels[$e->getSeverity()];
         }
         Log::getChannel('exception')->addRecord($level, $log_message, array('exception' => $e));
         $request = static::$request;
         if ($request === null) {
             $request = Request::createFromGlobals();
         }
         $action = Action::getDefaultAction(self::$_controller);
         $action->setRespondsWith(array('*'), false);
         $response = Controller::invoke($action, $request, array('exception' => $e));
         $response->prepare($request);
         if ($send_response) {
             $response->send();
         }
         return $response;
     } catch (\Exception $_e) {
         $response = new Response();
         $response->setStatusCode(500);
         if (Core::$_MODE === Core::MODE_PRODUCTION) {
             $response->setContent("Internal server error");
         } else {
             $response->setContent($e->__toString() . "\n\n\nAdditionally, the following exception occurred while trying to handle the error:\n\n" . $_e->__toString());
         }
         return $response;
     }
 }
Пример #2
0
 public function render($template, $data = array())
 {
     // locate the template file
     $template .= Config::get('wave')->view->extension;
     Hook::triggerAction('view.before_load_template', array(&$this, &$template));
     $loaded_template = $this->twig->loadTemplate($template);
     Hook::triggerAction('view.before_render', array(&$this, &$data));
     $html = $loaded_template->render($data);
     Hook::triggerAction('view.after_render', array(&$this, &$html));
     return $html;
 }
Пример #3
0
 /**
  * @param $namespace
  * @param $database
  *
  * @return DB
  * @throws DB\Exception
  */
 private static function init($namespace, $database)
 {
     $installed_drivers = DB\Connection::getAvailableDrivers();
     /** @var DB\Driver\DriverInterface $driver_class */
     $driver_class = self::getDriverClass($database->driver);
     //Check PDO driver is installed on system
     if (!in_array($driver_class::getDriverName(), $installed_drivers)) {
         throw new DBException(sprintf('PDO::%s driver not installed for %s.', $driver_class::getDriverName(), $driver_class));
     }
     self::$num_databases++;
     $instance = new self($namespace, $database);
     Hook::triggerAction('db.after_init', array(&$instance));
     return $instance;
 }
Пример #4
0
 /**
  * @param Request $request
  *
  * @throws \LogicException
  * @throws Http\Exception\NotFoundException
  * @return Response
  */
 public function route(Request $request = null)
 {
     if (null === $request) {
         $request = Request::createFromGlobals();
     }
     $this->request = $request;
     $this->request_uri = $request->getPath();
     if (strrpos($this->request_uri, $request->getFormat()) !== false) {
         $this->request_uri = substr($this->request_uri, 0, -(strlen($request->getFormat()) + 1));
     }
     $this->request_method = $request->getMethod();
     Hook::triggerAction('router.before_routing', array(&$this));
     $url = $this->request_method . $this->request_uri;
     $node = $this->getRootNode()->findChild($url, $this->request);
     /** @var \Wave\Router\Action $action */
     if ($node instanceof Router\Node && ($action = $node->getAction())) {
         Hook::triggerAction('router.before_invoke', array(&$action, &$this));
         $this->request->setAction($action);
         $this->response = Controller::invoke($action, $this->request);
         Hook::triggerAction('router.before_response', array(&$action, &$this));
         if (!$this->response instanceof Response) {
             throw new \LogicException("Action {$action->getAction()} should return a \\Wave\\Http\\Response object", 500);
         } else {
             return $this->response->prepare($this->request);
         }
     } else {
         throw new NotFoundException('The requested URL ' . $url . ' does not exist', $this->request);
     }
 }
Пример #5
0
 protected function respondJSON($payload = null)
 {
     if (!isset($this->_status)) {
         $this->_status = Response::STATUS_OK;
     }
     if (!isset($this->_message)) {
         $this->_message = Response::getMessageForCode($this->_status);
     }
     Hook::triggerAction('controller.before_build_json', array(&$this));
     $payload = $this->_buildPayload($this->_status, $this->_message, $payload);
     Hook::triggerAction('controller.before_build_json', array(&$this));
     return new JsonResponse($payload, $this->_status);
 }
Пример #6
0
 public function send()
 {
     Hook::triggerAction('response.before_send', array(&$this));
     $this->sendHeaders();
     $this->sendContent();
     if (function_exists('fastcgi_finish_request')) {
         fastcgi_finish_request();
     } elseif ('cli' !== PHP_SAPI) {
         // ob_get_level() never returns 0 on some Windows configurations, so if
         // the level is the same two times in a row, the loop should be stopped.
         $previous = null;
         $obStatus = ob_get_status(1);
         while (($level = ob_get_level()) > 0 && $level !== $previous) {
             $previous = $level;
             if ($obStatus[$level - 1]) {
                 if (version_compare(PHP_VERSION, '5.4', '>=')) {
                     if (isset($obStatus[$level - 1]['flags']) && $obStatus[$level - 1]['flags'] & PHP_OUTPUT_HANDLER_REMOVABLE) {
                         ob_end_flush();
                     }
                 } else {
                     if (isset($obStatus[$level - 1]['del']) && $obStatus[$level - 1]['del']) {
                         ob_end_flush();
                     }
                 }
             }
         }
         flush();
     }
     Hook::triggerAction('response.after_send', array(&$this));
     return $this;
 }