Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
TwigResponse
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
2 / 2
5
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
4
 render
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Miniframe\Response;
4
5use Miniframe\Core\Registry;
6use Miniframe\Core\Response;
7use Miniframe\Middleware\Twig;
8
9class TwigResponse extends Response
10{
11    /**
12     * Filename of the template
13     *
14     * @var string
15     */
16    private $template;
17
18    /**
19     * Properties sent to the template engine
20     *
21     * @var array
22     */
23    private $properties;
24
25    /**
26     * Reference to the Twig middleware
27     *
28     * @var Twig|null
29     */
30    private $twigMiddleware = null;
31
32    /**
33     * Initializes a new Twig response
34     *
35     * @param string $template   Filename of the template.
36     * @param array  $properties Properties to be sent to the template.
37     */
38    public function __construct(string $template, array $properties = array())
39    {
40        if (Registry::has('twig')) {
41            $this->twigMiddleware = Registry::get('twig');
42        } elseif (Registry::has(Twig::class)) {
43            $this->twigMiddleware = Registry::get(Twig::class);
44        }
45        if ($this->twigMiddleware === null) {
46            throw new \RuntimeException(
47                'Twig middleware not loaded. See '
48                . 'https://bitbucket.org/miniframe/twig-bundle/src/master/README.md'
49                . ' for the config.ini directives.'
50            );
51        }
52
53        $this->template = $template;
54        $this->properties = $properties;
55    }
56
57    /**
58     * Returns the rendered version of the template.
59     *
60     * @return string
61     */
62    public function render(): string
63    {
64        return $this->twigMiddleware->getEnvironment()->render($this->template, $this->properties);
65    }
66}