Completed
Push — master ( dac568...96128c )
by Jonathan
04:17 queued 01:47
created

Template::path()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

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 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace League\Plates\Template;
4
5
use Exception;
6
use League\Plates\Engine;
7
use LogicException;
8
use Throwable;
9
10
/**
11
 * Container which holds template data and provides access to template functions.
12
 */
13
class Template
14
{
15
    /**
16
     * Instance of the template engine.
17
     * @var Engine
18
     */
19
    protected $engine;
20
21
    /**
22
     * The name of the template.
23
     * @var Name
24
     */
25
    protected $name;
26
27
    /**
28
     * The data assigned to the template.
29
     * @var array
30
     */
31
    protected $data = array();
32
33
    /**
34
     * An array of section content.
35
     * @var array
36
     */
37
    protected $sections = array();
38
39
    /**
40
     * The name of the template layout.
41
     * @var string
42
     */
43
    protected $layoutName;
44
45
    /**
46
     * The data assigned to the template layout.
47
     * @var array
48
     */
49
    protected $layoutData;
50
51
    /**
52
     * Create new Template instance.
53
     * @param Engine $engine
54
     * @param string $name
55
     */
56 108
    public function __construct(Engine $engine, $name)
57
    {
58 108
        $this->engine = $engine;
59 108
        $this->name = new Name($engine, $name);
60
61 108
        $this->data($this->engine->getData($name));
62 108
    }
63
64
    /**
65
     * Magic method used to call extension functions.
66
     * @param  string $name
67
     * @param  array  $arguments
68
     * @return mixed
69
     */
70 8
    public function __call($name, $arguments)
71
    {
72 8
        return $this->engine->getFunction($name)->call($this, $arguments);
73
    }
74
75
    /**
76
     * Alias for render() method.
77
     * @throws \Throwable
78
     * @throws \Exception
79
     * @return string
80
     */
81 4
    public function __toString()
82
    {
83 4
        return $this->render();
84
    }
85
86
    /**
87
     * Assign data to template object.
88
     * @param  array $data
89
     * @return null
90
     */
91 108
    public function data(array $data = null)
92
    {
93 108
        if ($data === null) {
94 4
            return $this->data;
95
        }
96
97 108
        $this->data = array_merge($this->data, $data);
98 108
    }
99
100
    /**
101
     * Check if the template exists.
102
     * @return boolean
103
     */
104 92
    public function exists()
105
    {
106 92
        return $this->name->doesPathExist();
107
    }
108
109
    /**
110
     * Get the template path.
111
     * @return string
112
     */
113 88
    public function path()
114
    {
115 88
        return $this->name->getPath();
116
    }
117
118
    /**
119
     * Render the template and layout.
120
     * @param  array  $data
121
     * @throws \Throwable
122
     * @throws \Exception
123
     * @return string
124
     */
125 84
    public function render(array $data = array())
126
    {
127
        try {
128 84
            $this->data($data);
129
130 84
            unset($data);
131
132 84
            extract($this->data);
133
134 84
            ob_start();
135
136 84
            if (!$this->exists()) {
137 4
                throw new LogicException(
138 4
                    'The template "' . $this->name->getName() . '" could not be found at "' . $this->path() . '".'
139 4
                );
140
            }
141
142 80
            include $this->path();
143
144 64
            $content = ob_get_clean();
145
146 64
            if (isset($this->layoutName)) {
147 12
                $layout = $this->engine->make($this->layoutName);
148 12
                $layout->sections = array_merge($this->sections, array('content' => $content));
149 12
                $content = $layout->render($this->layoutData);
150 12
            }
151
152 64
            return $content;
153 20
        } catch (Throwable $e) {
0 ignored issues
show
Bug introduced by
The class Throwable does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
154
            ob_end_clean();
155
            throw $e;
156 20
        } catch (Exception $e) {
157 20
            ob_end_clean();
158 20
            throw $e;
159
        }
160
    }
161
162
    /**
163
     * Set the template's layout.
164
     * @param  string $name
165
     * @param  array  $data
166
     * @return null
167
     */
168 12
    public function layout($name, array $data = array())
169
    {
170 12
        $this->layoutName = $name;
171 12
        $this->layoutData = $data;
172 12
    }
173
174
    /**
175
     * Start a new section block.
176
     * @param  string $name
177
     * @return null
178
     */
179 8
    public function start($name)
180
    {
181 8
        if ($name === 'content') {
182 4
            throw new LogicException(
183
                'The section name "content" is reserved.'
184 4
            );
185
        }
186
187 4
        $this->sections[$name] = '';
188
189 4
        ob_start();
190 4
    }
191
192
    /**
193
     * Stop the current section block.
194
     * @return null
195
     */
196 8
    public function stop()
197
    {
198 8
        if (empty($this->sections)) {
199 4
            throw new LogicException(
200
                'You must start a section before you can stop it.'
201 4
            );
202
        }
203
204 4
        end($this->sections);
205
206 4
        $this->sections[key($this->sections)] = ob_get_clean();
207 4
    }
208
209
    /**
210
     * Returns the content for a section block.
211
     * @param  string      $name    Section name
212
     * @param  string      $default Default section content
213
     * @return string|null
214
     */
215 12
    public function section($name, $default = null)
216
    {
217 12
        if (!isset($this->sections[$name])) {
218 8
            return $default;
219
        }
220
221 4
        return $this->sections[$name];
222
    }
223
224
    /**
225
     * Fetch a rendered template.
226
     * @param  string $name
227
     * @param  array  $data
228
     * @return string
229
     */
230 4
    public function fetch($name, array $data = array())
231
    {
232 4
        return $this->engine->render($name, $data);
233
    }
234
235
    /**
236
     * Output a rendered template.
237
     * @param  string $name
238
     * @param  array  $data
239
     * @return null
240
     */
241 4
    public function insert($name, array $data = array())
242
    {
243 4
        echo $this->engine->render($name, $data);
244 4
    }
245
246
    /**
247
     * Apply multiple functions to variable.
248
     * @param  mixed  $var
249
     * @param  string $functions
250
     * @return mixed
251
     */
252 12
    public function batch($var, $functions)
253
    {
254 12
        foreach (explode('|', $functions) as $function) {
255 12
            if ($this->engine->doesFunctionExist($function)) {
256 4
                $var = call_user_func(array($this, $function), $var);
257 12
            } elseif (is_callable($function)) {
258 8
                $var = call_user_func($function, $var);
259 8
            } else {
260 4
                throw new LogicException(
261 4
                    'The batch function could not find the "' . $function . '" function.'
262 4
                );
263
            }
264 8
        }
265
266 8
        return $var;
267
    }
268
269
    /**
270
     * Escape string.
271
     * @param  string      $string
272
     * @param  null|string $functions
273
     * @return string
274
     */
275 12
    public function escape($string, $functions = null)
276
    {
277 12
        static $flags;
278
279 12
        if (!isset($flags)) {
280 4
            $flags = ENT_QUOTES | (defined('ENT_SUBSTITUTE') ? ENT_SUBSTITUTE : 0);
281 4
        }
282
283 12
        if ($functions) {
284 4
            $string = $this->batch($string, $functions);
285 4
        }
286
287 12
        return htmlspecialchars($string, $flags, 'UTF-8');
288
    }
289
290
    /**
291
     * Alias to escape function.
292
     * @param  string      $string
293
     * @param  null|string $functions
294
     * @return string
295
     */
296 4
    public function e($string, $functions = null)
297
    {
298 4
        return $this->escape($string, $functions);
299
    }
300
}
301