Example #1
0
 /**
  * Returns a prepared response with error message and HTTP status code set.
  *
  * @param Response $response      The response instance
  * @param int      $statusCode    The status code
  * @param string   $statusMessage The status message
  * @param bool     $expose        Controls whether the real information will be send or the default HTTP one
  *
  * @author Benjamin Carl <*****@*****.**>
  *
  * @return Response The modified response instance
  */
 protected function buildErrorResponse(Response $response, $statusCode, $statusMessage, $expose = false)
 {
     // Normalize for Exceptions from other parts of the app.
     if ($statusCode < 200 || $statusCode > 510) {
         $statusCode = 500;
     }
     $body = new Doozr_Response_Body('php://memory', 'w');
     // Do not expose secret information
     if (false === $expose) {
         $statusMessage = constant('Doozr_Http::REASONPHRASE_' . $statusCode);
     }
     $body->write('<h1>' . $statusMessage . '</h1>');
     return $response->withStatus($statusCode, $statusMessage)->withBody($body);
 }
Example #2
0
 /**
  * Getter for data.
  *
  * @param string $data The data to store.
  *
  * @author Benjamin Carl <*****@*****.**>
  * @return Doozr_Response_Body Data stored
  * @access public
  */
 public function setData($data)
 {
     $body = new Doozr_Response_Body();
     $body->write($data);
     $this->setBody($body);
 }
Example #3
0
 /**
  * Dispatches the request to the backend layers. This can be "Model" "View" "Presenter".
  *
  * @author Benjamin Carl <*****@*****.**>
  *
  * @return Response
  *
  * @throws \Doozr_Route_Exception
  */
 public function run()
 {
     // The MVP process is here ...
     /* @var Psr\Http\Message\ResponseInterface $response */
     $response = $this->getResponse();
     $presenter = $this->getPresenter();
     $action = $this->getAction();
     $view = $this->getView();
     // Use inofficial standard "xAction()"
     $method = $action . 'Action';
     // Validate that request mapped to a route (foo:bar) can be executed by Doozr
     if (true === ($httpStatus = $this->validateRequest($presenter, $method))) {
         // If we have a view attach it
         if (null !== $view && $view instanceof Doozr_Base_View) {
             $presenter->attach($view);
         }
         // Call the requested Action on requested Presenter (Presenter:Action)
         $data = $presenter->{$method}();
         // Create a response body with write access
         $responseBody = new Doozr_Response_Body('php://memory', 'w');
         $responseBody->write($data[Doozr_Base_Presenter::IDENTIFIER_VIEW]);
         $response = $response->withBody($responseBody);
         $response = $response->withStatus(Doozr_Http::OK);
     } else {
         // So if the Status is not TRUE (successful) it contains an integer for HTTP Response :)
         switch ($httpStatus) {
             case Doozr_Http::BAD_REQUEST:
                 $message = sprintf('No Presenter to execute route ("%s"). Sure it exists?', $this->getRoute());
                 break;
             case Doozr_Http::NOT_FOUND:
             default:
                 $message = sprintf('Method "%s()" of class "%s" not callable. Sure it exists and it\'s public?', $method, 'Presenter_' . ucfirst($this->getClassName()));
                 break;
         }
         throw new Doozr_Route_Exception($message, $httpStatus);
     }
     // Return all data returned and fetched in data
     return $response;
 }