Overview

Namespaces

  • Baguette
    • Application
    • Response
    • Serializer
    • Session
  • Overview
  • Namespace
  • Class
 1:  2:  3:  4:  5:  6:  7:  8:  9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 
<?php

namespace Baguette\Response;

/**
 * HTML Template Response
 *
 * @author    USAMI Kenta <tadsan@zonu.me>
 * @copyright 2016 Baguette HQ
 * @license   http://www.apache.org/licenses/LICENSE-2.0 Apache-2.0
 */
class TwigResponse implements ResponseInterface
{
    const CONTENT_TYPE_HTML  = 'text/html; charset=utf-8';

    /** @var \Twig_Environment */
    protected static $twig;
    /** @var string */
    public $tpl_name;
    /** @var array */
    public $params;
    /** @var string|null */
    public $content_type;
    /** @var int */
    public $status_code;

    /**
     * @param string $tpl_name
     * @param array  $params
     * @param int    $status_code
     */
    public function __construct($tpl_name, array $params = [], $status_code = 200)
    {
        $this->tpl_name = $tpl_name;
        $this->params   = $params;
        $this->status_code = $status_code;
    }

    /**
     * @return array[]
     */
    public function getResponseHeaders()
    {
        return [
            'content-type' => [self::CONTENT_TYPE_HTML],
        ];
    }

    /**
     * @param  \Baguette\Application $app
     * @return string
     */
    public function render(\Baguette\Application $app)
    {
        $params = $this->params + [
            'server' => $app->server,
            'cookie' => $app->cookie,
            'get'    => $app->get,
            'post'   => $app->post,
            'now'    => $app->now,
        ];

        return self::$twig->render($this->tpl_name, $params);
    }

    /**
     * @return int
     */
    public function getHttpStatusCode()
    {
        return $this->status_code;
    }

    /**
     * @param  \Twig_Environment
     */
    public static function setTwigEnvironment(\Twig_Environment $twig)
    {
        self::$twig = $twig;
    }
}
ξ ^ω^)ξ < Baguette Not Framework API documentation generated by ApiGen