Example #1
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);
     }
 }
Example #2
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;
     }
 }
Example #3
0
 public function prepare(Request $request)
 {
     parent::prepare($request);
     $this->headers->set('Content-Type', 'text/html; charset=utf8');
     $this->headers->set('X-Wave-Response', 'html');
     return $this;
 }
Example #4
0
 public function setContent($data, $convert = true)
 {
     if ($convert) {
         $this->data = $data;
         $data = XML::encode($data);
     }
     parent::setContent($data);
 }
Example #5
0
 public function sendContent()
 {
     if ($this->jsonp_callback !== null) {
         echo "{$this->jsonp_callback}({$this->content});";
     } else {
         parent::sendContent();
     }
 }
Example #6
0
 public function __construct($url, $status = self::STATUS_FOUND, array $headers = array())
 {
     parent::__construct('', $status, $headers);
     $this->setTarget($url);
 }
Example #7
0
 protected function requestXML($payload = null)
 {
     if (!isset($this->_status)) {
         $this->_status = Response::STATUS_BAD_REQUEST;
     }
     if (!isset($this->_message)) {
         $this->_message = Response::getMessageForCode($this->_status);
     }
     return $this->respondXML($payload);
 }