Xhgui_Controller::render()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
use Slim\Slim;
4
5
abstract class Xhgui_Controller
6
{
7
    /**
8
     * @var array
9
     */
10
    protected $_templateVars = array();
11
12
    /**
13
     * @var string|null
14
     */
15
    protected $_template = null;
16
17
    /**
18
     * @var Slim
19
     */
20
    protected $app;
21
22
    public function __construct(Slim $app)
23
    {
24
        $this->app = $app;
25
    }
26
27
    public function set($vars)
28
    {
29
        $this->_templateVars = array_merge($this->_templateVars, $vars);
30
    }
31
32
    public function templateVars()
33
    {
34
        return $this->_templateVars;
35
    }
36
37
    public function render()
38
    {
39
        // We want to render the specified Twig template to the output buffer.
40
        // The simplest way to do that is Slim::render, but that is not allowed
41
        // in middleware, because it uses Slim\View::display which prints
42
        // directly to the native PHP output buffer.
43
        // Doing that is problematic, because the HTTP headers set via $app->response()
44
        // must be output first, which won't happen until after the middleware
45
        // is completed. Output of headers and body is done by the Slim::run entry point.
46
47
        // The below is copied from Slim::render (slim/[email protected]).
48
        // Modified to use View::fetch + Response::write, instead of View::display.
49
        $this->app->view->appendData($this->_templateVars);
50
        $body = $this->app->view->fetch($this->_template);
51
        $this->app->response->write($body);
52
    }
53
54
}
55