Completed
Push — master ( 180c3e...9faef8 )
by Jonathan
02:00
created

src/Template/Template.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 104
    public function __construct(Engine $engine, $name)
57
    {
58 104
        $this->engine = $engine;
59 104
        $this->name = new Name($engine, $name);
60
61 104
        $this->data($this->engine->getData($name));
62 104
    }
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
     * @param  array  $data
0 ignored issues
show
There is no parameter named $data. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
78
     * @throws \Throwable
79
     * @throws \Exception
80
     * @return string
81
     */
82 4
    public function __toString()
83
    {
84 4
        return $this->render();
85
    }
86
87
    /**
88
     * Assign data to template object.
89
     * @param  array $data
90
     * @return null
91
     */
92 104
    public function data(array $data)
93
    {
94 104
        $this->data = array_merge($this->data, $data);
95 104
    }
96
97
    /**
98
     * Check if the template exists.
99
     * @return boolean
100
     */
101 92
    public function exists()
102
    {
103 92
        return $this->name->doesPathExist();
104
    }
105
106
    /**
107
     * Get the template path.
108
     * @return string
109
     */
110 88
    public function path()
111
    {
112 88
        return $this->name->getPath();
113
    }
114
115
    /**
116
     * Render the template and layout.
117
     * @param  array  $data
118
     * @throws \Throwable
119
     * @throws \Exception
120
     * @return string
121
     */
122 84
    public function render(array $data = array())
123
    {
124
        try {
125 84
            $this->data($data);
126
127 84
            unset($data);
128
129 84
            extract($this->data);
130
131 84
            ob_start();
132
133 84
            if (!$this->exists()) {
134 4
                throw new LogicException(
135 4
                    'The template "' . $this->name->getName() . '" could not be found at "' . $this->path() . '".'
136 3
                );
137
            }
138
139 80
            include $this->path();
140
141 64
            $content = ob_get_clean();
142
143 64
            if (isset($this->layoutName)) {
144 12
                $layout = $this->engine->make($this->layoutName);
145 12
                $layout->sections = array_merge($this->sections, array('content' => $content));
146 12
                $content = $layout->render($this->layoutData);
147 9
            }
148
149 64
            return $content;
150 20
        } catch (Throwable $e) {
0 ignored issues
show
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...
151 5
            ob_end_clean();
152 5
            throw $e;
153 15
        } catch (Exception $e) {
154 15
            ob_end_clean();
155 15
            throw $e;
156
        }
157
    }
158
159
    /**
160
     * Set the template's layout.
161
     * @param  string $name
162
     * @param  array  $data
163
     * @return null
164
     */
165 12
    public function layout($name, array $data = array())
166
    {
167 12
        $this->layoutName = $name;
168 12
        $this->layoutData = $data;
169 12
    }
170
171
    /**
172
     * Start a new section block.
173
     * @param  string $name
174
     * @return null
175
     */
176 8
    public function start($name)
177
    {
178 8
        if ($name === 'content') {
179 4
            throw new LogicException(
180 1
                'The section name "content" is reserved.'
181 3
            );
182
        }
183
184 4
        $this->sections[$name] = '';
185
186 4
        ob_start();
187 4
    }
188
189
    /**
190
     * Stop the current section block.
191
     * @return null
192
     */
193 8
    public function stop()
194
    {
195 8
        if (empty($this->sections)) {
196 4
            throw new LogicException(
197 1
                'You must start a section before you can stop it.'
198 3
            );
199
        }
200
201 4
        end($this->sections);
202
203 4
        $this->sections[key($this->sections)] = ob_get_clean();
204 4
    }
205
206
    /**
207
     * Returns the content for a section block.
208
     * @param  string      $name    Section name
209
     * @param  string      $default Default section content
210
     * @return string|null
211
     */
212 12
    public function section($name, $default = null)
213
    {
214 12
        if (!isset($this->sections[$name])) {
215 8
            return $default;
216
        }
217
218 4
        return $this->sections[$name];
219
    }
220
221
    /**
222
     * Fetch a rendered template.
223
     * @param  string $name
224
     * @param  array  $data
225
     * @return string
226
     */
227 4
    public function fetch($name, array $data = array())
228
    {
229 4
        return $this->engine->render($name, $data);
230
    }
231
232
    /**
233
     * Output a rendered template.
234
     * @param  string $name
235
     * @param  array  $data
236
     * @return null
237
     */
238 4
    public function insert($name, array $data = array())
239
    {
240 4
        echo $this->engine->render($name, $data);
241 4
    }
242
243
    /**
244
     * Apply multiple functions to variable.
245
     * @param  mixed  $var
246
     * @param  string $functions
247
     * @return mixed
248
     */
249 12
    public function batch($var, $functions)
250
    {
251 12
        foreach (explode('|', $functions) as $function) {
252 12
            if ($this->engine->doesFunctionExist($function)) {
253 4
                $var = call_user_func(array($this, $function), $var);
254 12
            } elseif (is_callable($function)) {
255 8
                $var = call_user_func($function, $var);
256 6
            } else {
257 4
                throw new LogicException(
258 6
                    'The batch function could not find the "' . $function . '" function.'
259 3
                );
260
            }
261 6
        }
262
263 8
        return $var;
264
    }
265
266
    /**
267
     * Escape string.
268
     * @param  string      $string
269
     * @param  null|string $functions
270
     * @return string
271
     */
272 12
    public function escape($string, $functions = null)
273
    {
274 12
        static $flags;
275
276 12
        if (!isset($flags)) {
277 4
            $flags = ENT_QUOTES | (defined('ENT_SUBSTITUTE') ? ENT_SUBSTITUTE : 0);
278 3
        }
279
280 12
        if ($functions) {
281 4
            $string = $this->batch($string, $functions);
282 3
        }
283
284 12
        return htmlspecialchars($string, $flags, 'UTF-8');
285
    }
286
287
    /**
288
     * Alias to escape function.
289
     * @param  string      $string
290
     * @param  null|string $functions
291
     * @return string
292
     */
293 4
    public function e($string, $functions = null)
294
    {
295 4
        return $this->escape($string, $functions);
296
    }
297
}
298