Example #1
0
 /**
  * Prepares the response based on the Request object
  * This method is not essential and can be cut out of the chain.
  * However it cleans up the headers and does some other stuff under the hood.
  * @param  Request $req The request object
  * 
  * @return \Modulework\Modules\Http\Response THIS
  */
 public function prepare(Request $request)
 {
     if ($this->isInvalid()) {
         throw new RunTimeException('Response is invalid (' . $this->statusCode . ')');
     }
     if ($this->isInformational()) {
         $this->content = null;
     }
     $this->charset = $this->charset ?: 'UTF-8';
     if (!$this->headers->has('Content-Type')) {
         $this->headers->set('Content-Type', 'text/html; charset=' . $this->charset);
     }
     // This method tries may cause some issues, if 200 is REQUIRED even when it' s
     // redirect response. If you want to change the status code just call it AFTER
     // this method:
     // e. g. [...]->prepare()->setStatusCode(301)[...]
     if ($this->statusCode === 200 && $this->headers->has('Location')) {
         $this->setStatusCode('302');
     }
     if ($request->isMethod('HEAD')) {
         $this->content = null;
     }
     if ('1.0' == $this->getProtocolVersion() && 'no-cache' == $this->headers->get('Cache-Control')) {
         $this->headers->set('pragma', 'no-cache');
         $this->headers->set('expires', -1);
     }
     return $this;
 }