Example #1
0
File: App.php Project: colorium/web
 /**
  * Render response
  *
  * @param Context $context
  * @return Context
  */
 protected function render(Context $context)
 {
     $this->logger->debug('kernel.process.render: render Response');
     // resolve output format
     if ($context->response->raw) {
         // template
         if ($template = $context->logic->html) {
             // render template
             $vars = $context->response->content;
             $vars = is_array($vars) ? $vars : (array) $vars;
             $content = $this->templater->render($template, $vars);
             // set html response
             $context->response = new Response\Html($content, $context->response->code, $context->response->headers);
             $this->logger->debug('kernel.process.render: template "' . $template . '" compiled');
         } elseif ($context->logic->render = 'json') {
             $context->response = new Response\Json($context->response->content, $context->response->code, $context->response->headers);
             $this->logger->debug('kernel.process.render: json response generated');
         }
         // remove auto-generated flag
         $context->response->raw = false;
     } elseif ($context->response instanceof Response\Template) {
         $context->response->content = $this->templater->render($context->response->template, $context->response->vars);
     } elseif ($context->response instanceof Response\Redirect and $context->response->uri[0] == '/') {
         $context->response->uri = $context->request->uri->make($context->response->uri);
     }
     return $context;
 }
Example #2
0
 /**
  * Handle request/response
  *
  * @param Context $context
  * @param callable $chain
  * @return Response
  */
 public function handle(Context $context, callable $chain = null)
 {
     // setup
     $context->templater = $this->templater;
     $this->templater->vars['ctx'] = $context;
     $this->templater->helpers['url'] = function (...$parts) use($context) {
         $path = ltrim(implode('/', $parts));
         return $context->request->uri->make($path);
     };
     $this->templater->helpers['call'] = function ($resource, ...$params) use($context) {
         return $context->forward($resource, ...$params);
     };
     // execute chain
     $context = $chain($context);
     // expect valid response
     if (!$context->response instanceof Response) {
         throw new \RuntimeException('Context::response must be a valid Colorium\\Http\\Response instance');
     } elseif ($context->response instanceof Response\Template) {
         $context->response->content = $this->templater->render($context->response->template, $context->response->vars);
     } elseif ($context->response instanceof Response\Redirect and $context->response->uri[0] == '/') {
         $context->response->uri = $context->request->uri->make($context->response->uri);
     } elseif ($context->response->raw and !$context->request->cli) {
         // render template
         if ($template = $context->invokable->annotation('html')) {
             $content = $this->templater->render($template, (array) $context->response->content);
             $context->response = new Response\Html($content, $context->response->code);
         } else {
             $context->response = new Response\Json($context->response->content, $context->response->code);
         }
     }
     return $context;
 }