Completed
Pull Request — master (#396)
by Elan
01:15
created

AbstractController::config()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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