TwigRenderer   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 6
dl 0
loc 123
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A addExtension() 0 6 1
A addFilter() 0 6 1
A template() 0 4 1
A renderBlock() 0 4 1
A render() 0 6 1
A getLoader() 0 4 1
A getEnvironment() 0 4 1
A createLoader() 0 15 3
1
<?php
2
3
namespace Resilient;
4
5
use \Psr\Http\Message\ResponseInterface;
6
7
/**
8
 * TwigRenderer class.
9
 *
10
 * Provide implementation for abstract renderer, specifically made with twig engine
11
 *
12
 * @extends AbstractRenderer
13
 */
14
class TwigRenderer extends AbstractRenderer
15
{
16
    protected $loader;
17
    protected $environment;
18
19
    /**
20
     * __construct function.
21
     *
22
     * @access public
23
     * @param string|array $path
24
     * @param array $options (default: [])
25
     */
26
    public function __construct($path, array $options = [])
27
    {
28
        $this->loader = $this->createLoader(is_array($path) ? $path : [$path]);
29
        $this->environment = new \Twig_Environment($this->loader, $options);
30
    }
31
32
    /**
33
     * addExtension function.
34
     *
35
     * @access public
36
     * @param \Twig_ExtensionInterface $extension
37
     * @return $this
38
     */
39
    public function addExtension(\Twig_ExtensionInterface $extension)
40
    {
41
        $this->environment->addExtension($extension);
42
43
        return $this;
44
    }
45
46
    /**
47
     * addFilter function.
48
     *
49
     * @access public
50
     * @param \Twig_Filter $filter
51
     * @return $this
52
     */
53
    public function addFilter(\Twig_Filter $filter)
54
    {
55
        $this->environment->addFilter($filter);
56
57
        return $this;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    protected function template($template)
64
    {
65
        return $this->environment->load($template);
66
    }
67
68
    /**
69
     * Twig renderBlock function.
70
     *
71
     * @access public
72
     * @param mixed $template
73
     * @param mixed $blockName
74
     * @param mixed $data (default: [])
75
     * @return string rendered block
76
     */
77
    public function renderBlock($template, $blockName, $data = [])
78
    {
79
        return $this->template($template)->renderBlock($blockName, $this->mergeData($data));
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function render(ResponseInterface $response, $template, $data = [])
86
    {
87
        $response->getBody()->write($this->template($template)->render($this->mergeData($data)));
88
89
        return $response;
90
    }
91
92
    /**
93
     * getLoader function.
94
     *
95
     * @access public
96
     * @return Twig_Loader_Filesystem
97
     */
98
    public function getLoader()
99
    {
100
        return $this->loader;
101
    }
102
103
    /**
104
     * getEnvironment function.
105
     *
106
     * @access public
107
     * @return Twig_Environment
108
     */
109
    public function getEnvironment()
110
    {
111
        return $this->environment;
112
    }
113
114
    /**
115
     * createLoader function.
116
     *
117
     * @access protected
118
     * @param array $paths
119
     * @return Twig_Loader_Filesystem
120
     */
121
    protected function createLoader(array $paths)
122
    {
123
        $loader = new \Twig_Loader_Filesystem();
124
125
        foreach ($paths as $namespace => $path) {
126
            if (is_string($namespace)) {
127
                $loader->setPaths($path, $namespace);
128
                continue;
129
            }
130
131
            $loader->addPath($path);
132
        }
133
134
        return $loader;
135
    }
136
}
137