예제 #1
0
파일: Seed.php 프로젝트: rakorium/okapi
 /**
  * @param Resources $context
  * @return Closure
  */
 public static function redirector(Resources $context)
 {
     $redirector = function ($target, $status = 301) {
         $response = new Message\Response();
         $response->setStatusCode($status)->getHeaders()->set('Location', $target);
         return $response;
     };
     return $redirector;
 }
예제 #2
0
 /**
  * @param Message\Request $request
  * @param Message\Response $response
  * @return static
  */
 public function register(Message\Request $request, Message\Response $response)
 {
     $type = $this->mime;
     $mtime = $this->statValue('mtime', time());
     $etag = $this->calcEtag();
     $fresh = $this->freshResponse($request, $mtime, $etag);
     $dispo = $this->disposition;
     $length = strlen($this->data);
     // Add common headers
     $response->getHeaders()->add('Etag', $etag)->add('Last-Modified', Message\Headers::date($mtime));
     if ($fresh) {
         // 200 OK
         $response->getHeaders()->add('Accept-Ranges', 'none')->add('Content-Type', $type)->add('Content-Length', $fresh ? $length : 0)->add('Content-Disposition', $dispo);
         $response->setBody($this->sendHandler());
     } else {
         // 304 Not Modified
         $response->setStatus(304)->setBody(null);
     }
     return $this;
 }
예제 #3
0
파일: Renderer.php 프로젝트: rakorium/okapi
 /**
  * @param ViewModel $model
  * @param Response $response
  * @return string
  */
 public function render(ViewModel $model, Response $response = null)
 {
     $body = '';
     $helpers = $this->getHelpers();
     $suffix = $this->getScriptSuffix();
     // (0) Create the ROOT node
     $data = ViewData::cast($model->getData(), ['escape' => $helpers->get('escape')]);
     $this->injectHelpers($data);
     // (1) Render the view script
     $view_name = $model->getTemplate();
     if ($view_name) {
         $view_script = $view_name . $suffix;
         $view_path = $this->templatePath($view_script);
         if (file_exists($view_path)) {
             $body = $this->renderScript($view_path, $data);
         } else {
             throw new Exception\RuntimeException("Script file ({$view_path}) not found");
         }
     }
     // (2) Render the layout
     $layout_name = $this->modelLayout($model);
     if ($layout_name) {
         $layout_script = $layout_name . $suffix;
         $layout_path = $this->templatePath($layout_script);
         if (file_exists($layout_path)) {
             $data['content'] = $body;
             $body = $this->renderScript($layout_path, $data);
         } else {
             throw new Exception\RuntimeException("Layout file ({$layout_path}) not found");
         }
     }
     // (3) Push
     if ($response) {
         $response->setBody($body);
     }
     return $body;
 }
예제 #4
0
파일: Renderer.php 프로젝트: rakorium/okapi
 /**
  *
  */
 public function render(ViewModel $model, Response $response)
 {
     $body = '';
     $engine = $this->getEngine();
     // Create the ROOT node
     $data = $this->dataNode($model->getData(), false);
     // Get the layout name
     $layout_name = $model->getLayout();
     $engine->defAlias('layout', $layout_name);
     // (1) Render the view script
     $view_name = $model->getTemplate();
     if ($view_name) {
         $body = $engine->execute($view_name, $data, null);
         if ($body === false) {
             $error = $engine->getFirstError();
             $this->logSubError($error);
             $info = isset($error['message']) ? '(' . $error['message'] . ')' : '';
             throw new Exception\RuntimeException("View script [{$view_name}] could not be loaded {$info}");
         }
     }
     // Push view errors (DEBUG)
     $data['view_errors'] = $engine->getErrors();
     // (2) Render the layout
     if ($layout_name) {
         $data['content'] = $body;
         $layout = $engine->execute($layout_name, $data, null);
         if ($layout === false) {
             $error = $engine->getFirstError();
             $this->logSubError($error);
             $info = isset($error['message']) ? '(' . $error['message'] . ')' : '';
             throw new Exception\RuntimeException("Layout script [{$layout_name}] could not be loaded {$info}");
         }
         $body = $layout;
     }
     $response->setBody($body);
 }
예제 #5
0
 /**
  * @param Message\Request $request
  * @param Message\Response $response
  * @return static
  */
 public function register(Message\Request $request, Message\Response $response)
 {
     $info = pathinfo($this->filename);
     $mtime = $this->statValue('mtime', time());
     $etag = $this->fileEtag($this->filename);
     $fresh = $this->freshResponse($request, $mtime, $etag);
     $length = $this->statValue('size');
     // Add common headers
     $response->getHeaders()->add('Etag', $etag)->add('Last-Modified', Message\Headers::date($mtime));
     if ($fresh) {
         // 200 OK
         $headers = $response->getHeaders();
         $headers->add('Accept-Ranges', 'none')->add('Content-Type', $this->contentType($info))->add('Content-Disposition', $this->getDispoString());
         if ($fresh) {
             if ($length !== null) {
                 $headers->add('Content-Length', $length);
             }
         } else {
             $headers->add('Content-Length', 0);
         }
         $response->setBody($this->sendHandler());
         $this->autoclose = false;
     } else {
         // 304 Not Modified
         $response->setStatus(304)->setBody(null);
     }
     return $this;
 }
예제 #6
0
파일: Emitter.php 프로젝트: rakorium/okapi
 /**
  *
  * @param Message\Response $response
  */
 protected function emitBody(Message\Response $response)
 {
     $handled = true;
     $body = $response->getBody();
     if (is_string($body)) {
         echo $body;
     } elseif (is_resource($body)) {
         while (!feof($body)) {
             $data = fread($body, 8192);
             echo $data;
         }
         fclose($body);
     } elseif (is_callable($body)) {
         $body();
     } else {
         $handled = false;
     }
     return $handled;
 }