Completed
Pull Request — master (#132)
by
unknown
14:07
created

Template::cleanOutputBuffer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace League\Plates\Template;
4
5
use League\Plates\Engine;
6
use LogicException;
7
8
/**
9
 * Container which holds template data and provides access to template functions.
10
 */
11
class Template
12
{
13
    /**
14
     * Instance of the template engine.
15
     * @var Engine
16
     */
17
    protected $engine;
18
19
    /**
20
     * The name of the template.
21
     * @var Name
22
     */
23
    protected $name;
24
25
    /**
26
     * The data assigned to the template.
27
     * @var array
28
     */
29
    protected $data = array();
30
31
    /**
32
     * An array of section content.
33
     * @var array
34
     */
35
    protected $sections = array();
36
37
    /**
38
     * The name of the template layout.
39
     * @var string
40
     */
41
    protected $layoutName;
42
43
    /**
44
     * The data assigned to the template layout.
45
     * @var array
46
     */
47
    protected $layoutData;
48
49
    /**
50
     * Create new Template instance.
51
     * @param Engine $engine
52
     * @param string $name
53
     */
54 80
    public function __construct(Engine $engine, $name)
55
    {
56 80
        $this->engine = $engine;
57 80
        $this->name = new Name($engine, $name);
58
59 80
        $this->data($this->engine->getData($name));
60 80
    }
61
62
    /**
63
     * Magic method used to call extension functions.
64
     * @param  string $name
65
     * @param  array  $arguments
66
     * @return mixed
67
     */
68 8
    public function __call($name, $arguments)
69
    {
70 8
        return $this->engine->getFunction($name)->call($this, $arguments);
71
    }
72
73
    /**
74
     * Assign data to template object.
75
     * @param  array $data
76
     * @return null
77
     */
78 80
    public function data(array $data)
79
    {
80 80
        $this->data = array_merge($this->data, $data);
81 80
    }
82
83
    /**
84
     * Check if the template exists.
85
     * @return boolean
86
     */
87 68
    public function exists()
88
    {
89 68
        return $this->name->doesPathExist();
90
    }
91
92
    /**
93
     * Get the template path.
94
     * @return string
95
     */
96 64
    public function path()
97
    {
98 64
        return $this->name->getPath();
99
    }
100
101
    /**
102
     * Render the template and layout.
103
     * @param  array  $data
104
     * @throws \Throwable
105
     * @throws \Exception
106 60
     * @return string
107
     */
108
    public function render(array $data = array())
109 60
    {
110
        try {
111 60
            $this->data($data);
112
113 60
            unset($data);
114
115 60
            extract($this->data);
116
117 60
            ob_start();
118
119
            if (!$this->exists()) {
120
                throw new LogicException(
121
                    'The template "' . $this->name->getName() . '" could not be found at "' . $this->path() . '".'
122
                );
123 60
            }
124
125 60
            include $this->path();
126
127 60
            $content = ob_get_clean();
128 12
129 12
            if (isset($this->layoutName)) {
130 12
                $layout = $this->engine->make($this->layoutName);
131 9
                $layout->sections = array_merge($this->sections, array('content' => $content));
132
                $content = $layout->render($this->layoutData);
133 60
            }
134
135
            return $content;
136
        } catch (\Throwable $e) {
0 ignored issues
show
Bug introduced by
The class Throwable does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
137
            $this->cleanOutputBuffer($e);
138
        } catch (\Exception $e) {
139
            $this->cleanOutputBuffer($e);
140
        }
141
    }
142
143
    /**
144
     * @param \Throwable|\Exception $e
145
     * @throws \Throwable
146
     * @throws \Exception
147
     */
148
    private function cleanOutputBuffer($e) {
149 12
        ob_end_clean();
150
        throw $e;
151 12
    }
152 12
153 12
    /**
154
     * Set the template's layout.
155
     * @param  string $name
156
     * @param  array  $data
157
     * @return null
158
     */
159
    protected function layout($name, array $data = array())
160 4
    {
161
        $this->layoutName = $name;
162 4
        $this->layoutData = $data;
163
    }
164
165
    /**
166
     * Start a new section block.
167
     * @param  string $name
168 4
     * @return null
169
     */
170 4
    protected function start($name)
171 4
    {
172
        if ($name === 'content') {
173
            throw new LogicException(
174
                'The section name "content" is reserved.'
175
            );
176
        }
177 4
178
        $this->sections[$name] = '';
179 4
180
        ob_start();
181
    }
182
183
    /**
184
     * Stop the current section block.
185 4
     * @return null
186
     */
187 4
    protected function stop()
188 4
    {
189
        if (empty($this->sections)) {
190
            throw new LogicException(
191
                'You must start a section before you can stop it.'
192
            );
193
        }
194
195
        end($this->sections);
196 12
197
        $this->sections[key($this->sections)] = ob_get_clean();
198 12
    }
199 8
200
    /**
201
     * Returns the content for a section block.
202 4
     * @param  string      $name    Section name
203
     * @param  string      $default Default section content
204
     * @return string|null
205
     */
206
    protected function section($name, $default = null)
207
    {
208
        if (!isset($this->sections[$name])) {
209
            return $default;
210
        }
211 4
212
        return $this->sections[$name];
213 4
    }
214
215
    /**
216
     * Fetch a rendered template.
217
     * @param  string $name
218
     * @param  array  $data
219
     * @return string
220
     */
221
    protected function fetch($name, array $data = array())
222 4
    {
223
        return $this->engine->render($name, $data);
224 4
    }
225 4
226
    /**
227
     * Output a rendered template.
228
     * @param  string $name
229
     * @param  array  $data
230
     * @return null
231
     */
232
    protected function insert($name, array $data = array())
233 8
    {
234
        echo $this->engine->render($name, $data);
235 8
    }
236 8
237 4
    /**
238 8
     * Apply multiple functions to variable.
239 8
     * @param  mixed  $var
240 6
     * @param  string $functions
241
     * @return mixed
242 2
     */
243
    protected function batch($var, $functions)
244
    {
245 6
        foreach (explode('|', $functions) as $function) {
246
            if ($this->engine->doesFunctionExist($function)) {
247 8
                $var = call_user_func(array($this, $function), $var);
248
            } elseif (is_callable($function)) {
249
                $var = call_user_func($function, $var);
250
            } else {
251
                throw new LogicException(
252
                    'The batch function could not find the "' . $function . '" function.'
253
                );
254
            }
255
        }
256 12
257
        return $var;
258 12
    }
259
260 12
    /**
261 4
     * Escape string.
262 3
     * @param  string      $string
263
     * @param  null|string $functions
264 12
     * @return string
265 4
     */
266 3
    protected function escape($string, $functions = null)
267
    {
268 12
        static $flags;
269
270
        if (!isset($flags)) {
271
            $flags = ENT_QUOTES | (defined('ENT_SUBSTITUTE') ? ENT_SUBSTITUTE : 0);
272
        }
273
274
        if ($functions) {
275
            $string = $this->batch($string, $functions);
276
        }
277 4
278
        return htmlspecialchars($string, $flags, 'UTF-8');
279 4
    }
280
281
    /**
282
     * Alias to escape function.
283
     * @param  string      $string
284
     * @param  null|string $functions
285
     * @return string
286
     */
287
    protected function e($string, $functions = null)
288
    {
289
        return $this->escape($string, $functions);
290
    }
291
}
292