Security update to Drupal 8.4.6
[yaffs-website] / vendor / twig / twig / lib / Twig / Error.php
1 <?php
2
3 /*
4  * This file is part of Twig.
5  *
6  * (c) Fabien Potencier
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 /**
13  * Twig base exception.
14  *
15  * This exception class and its children must only be used when
16  * an error occurs during the loading of a template, when a syntax error
17  * is detected in a template, or when rendering a template. Other
18  * errors must use regular PHP exception classes (like when the template
19  * cache directory is not writable for instance).
20  *
21  * To help debugging template issues, this class tracks the original template
22  * name and line where the error occurred.
23  *
24  * Whenever possible, you must set these information (original template name
25  * and line number) yourself by passing them to the constructor. If some or all
26  * these information are not available from where you throw the exception, then
27  * this class will guess them automatically (when the line number is set to -1
28  * and/or the name is set to null). As this is a costly operation, this
29  * can be disabled by passing false for both the name and the line number
30  * when creating a new instance of this class.
31  *
32  * @author Fabien Potencier <fabien@symfony.com>
33  */
34 class Twig_Error extends Exception
35 {
36     protected $lineno;
37     // to be renamed to name in 2.0
38     protected $filename;
39     protected $rawMessage;
40     protected $previous;
41
42     private $sourcePath;
43     private $sourceCode;
44
45     /**
46      * Constructor.
47      *
48      * Set both the line number and the name to false to
49      * disable automatic guessing of the original template name
50      * and line number.
51      *
52      * Set the line number to -1 to enable its automatic guessing.
53      * Set the name to null to enable its automatic guessing.
54      *
55      * By default, automatic guessing is enabled.
56      *
57      * @param string                  $message  The error message
58      * @param int                     $lineno   The template line where the error occurred
59      * @param Twig_Source|string|null $source   The source context where the error occurred
60      * @param Exception               $previous The previous exception
61      */
62     public function __construct($message, $lineno = -1, $source = null, Exception $previous = null)
63     {
64         if (null === $source) {
65             $name = null;
66         } elseif (!$source instanceof Twig_Source) {
67             // for compat with the Twig C ext., passing the template name as string is accepted
68             $name = $source;
69         } else {
70             $name = $source->getName();
71             $this->sourceCode = $source->getCode();
72             $this->sourcePath = $source->getPath();
73         }
74         if (PHP_VERSION_ID < 50300) {
75             $this->previous = $previous;
76             parent::__construct('');
77         } else {
78             parent::__construct('', 0, $previous);
79         }
80
81         $this->lineno = $lineno;
82         $this->filename = $name;
83
84         if (-1 === $lineno || null === $name || null === $this->sourcePath) {
85             $this->guessTemplateInfo();
86         }
87
88         $this->rawMessage = $message;
89
90         $this->updateRepr();
91     }
92
93     /**
94      * Gets the raw message.
95      *
96      * @return string The raw message
97      */
98     public function getRawMessage()
99     {
100         return $this->rawMessage;
101     }
102
103     /**
104      * Gets the logical name where the error occurred.
105      *
106      * @return string The name
107      *
108      * @deprecated since 1.27 (to be removed in 2.0). Use getSourceContext() instead.
109      */
110     public function getTemplateFile()
111     {
112         @trigger_error(sprintf('The "%s" method is deprecated since version 1.27 and will be removed in 2.0. Use getSourceContext() instead.', __METHOD__), E_USER_DEPRECATED);
113
114         return $this->filename;
115     }
116
117     /**
118      * Sets the logical name where the error occurred.
119      *
120      * @param string $name The name
121      *
122      * @deprecated since 1.27 (to be removed in 2.0). Use setSourceContext() instead.
123      */
124     public function setTemplateFile($name)
125     {
126         @trigger_error(sprintf('The "%s" method is deprecated since version 1.27 and will be removed in 2.0. Use setSourceContext() instead.', __METHOD__), E_USER_DEPRECATED);
127
128         $this->filename = $name;
129
130         $this->updateRepr();
131     }
132
133     /**
134      * Gets the logical name where the error occurred.
135      *
136      * @return string The name
137      *
138      * @deprecated since 1.29 (to be removed in 2.0). Use getSourceContext() instead.
139      */
140     public function getTemplateName()
141     {
142         @trigger_error(sprintf('The "%s" method is deprecated since version 1.29 and will be removed in 2.0. Use getSourceContext() instead.', __METHOD__), E_USER_DEPRECATED);
143
144         return $this->filename;
145     }
146
147     /**
148      * Sets the logical name where the error occurred.
149      *
150      * @param string $name The name
151      *
152      * @deprecated since 1.29 (to be removed in 2.0). Use setSourceContext() instead.
153      */
154     public function setTemplateName($name)
155     {
156         @trigger_error(sprintf('The "%s" method is deprecated since version 1.29 and will be removed in 2.0. Use setSourceContext() instead.', __METHOD__), E_USER_DEPRECATED);
157
158         $this->filename = $name;
159         $this->sourceCode = $this->sourcePath = null;
160
161         $this->updateRepr();
162     }
163
164     /**
165      * Gets the template line where the error occurred.
166      *
167      * @return int The template line
168      */
169     public function getTemplateLine()
170     {
171         return $this->lineno;
172     }
173
174     /**
175      * Sets the template line where the error occurred.
176      *
177      * @param int $lineno The template line
178      */
179     public function setTemplateLine($lineno)
180     {
181         $this->lineno = $lineno;
182
183         $this->updateRepr();
184     }
185
186     /**
187      * Gets the source context of the Twig template where the error occurred.
188      *
189      * @return Twig_Source|null
190      */
191     public function getSourceContext()
192     {
193         return $this->filename ? new Twig_Source($this->sourceCode, $this->filename, $this->sourcePath) : null;
194     }
195
196     /**
197      * Sets the source context of the Twig template where the error occurred.
198      */
199     public function setSourceContext(Twig_Source $source = null)
200     {
201         if (null === $source) {
202             $this->sourceCode = $this->filename = $this->sourcePath = null;
203         } else {
204             $this->sourceCode = $source->getCode();
205             $this->filename = $source->getName();
206             $this->sourcePath = $source->getPath();
207         }
208
209         $this->updateRepr();
210     }
211
212     public function guess()
213     {
214         $this->guessTemplateInfo();
215         $this->updateRepr();
216     }
217
218     /**
219      * For PHP < 5.3.0, provides access to the getPrevious() method.
220      *
221      * @param string $method    The method name
222      * @param array  $arguments The parameters to be passed to the method
223      *
224      * @return Exception The previous exception or null
225      *
226      * @throws BadMethodCallException
227      */
228     public function __call($method, $arguments)
229     {
230         if ('getprevious' == strtolower($method)) {
231             return $this->previous;
232         }
233
234         throw new BadMethodCallException(sprintf('Method "Twig_Error::%s()" does not exist.', $method));
235     }
236
237     public function appendMessage($rawMessage)
238     {
239         $this->rawMessage .= $rawMessage;
240         $this->updateRepr();
241     }
242
243     /**
244      * @internal
245      */
246     protected function updateRepr()
247     {
248         $this->message = $this->rawMessage;
249
250         if ($this->sourcePath && $this->lineno > 0) {
251             $this->file = $this->sourcePath;
252             $this->line = $this->lineno;
253
254             return;
255         }
256
257         $dot = false;
258         if ('.' === substr($this->message, -1)) {
259             $this->message = substr($this->message, 0, -1);
260             $dot = true;
261         }
262
263         $questionMark = false;
264         if ('?' === substr($this->message, -1)) {
265             $this->message = substr($this->message, 0, -1);
266             $questionMark = true;
267         }
268
269         if ($this->filename) {
270             if (is_string($this->filename) || (is_object($this->filename) && method_exists($this->filename, '__toString'))) {
271                 $name = sprintf('"%s"', $this->filename);
272             } else {
273                 $name = json_encode($this->filename);
274             }
275             $this->message .= sprintf(' in %s', $name);
276         }
277
278         if ($this->lineno && $this->lineno >= 0) {
279             $this->message .= sprintf(' at line %d', $this->lineno);
280         }
281
282         if ($dot) {
283             $this->message .= '.';
284         }
285
286         if ($questionMark) {
287             $this->message .= '?';
288         }
289     }
290
291     /**
292      * @internal
293      */
294     protected function guessTemplateInfo()
295     {
296         $template = null;
297         $templateClass = null;
298
299         if (PHP_VERSION_ID >= 50306) {
300             $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS | DEBUG_BACKTRACE_PROVIDE_OBJECT);
301         } else {
302             $backtrace = debug_backtrace();
303         }
304
305         foreach ($backtrace as $trace) {
306             if (isset($trace['object']) && $trace['object'] instanceof Twig_Template && 'Twig_Template' !== get_class($trace['object'])) {
307                 $currentClass = get_class($trace['object']);
308                 $isEmbedContainer = 0 === strpos($templateClass, $currentClass);
309                 if (null === $this->filename || ($this->filename == $trace['object']->getTemplateName() && !$isEmbedContainer)) {
310                     $template = $trace['object'];
311                     $templateClass = get_class($trace['object']);
312                 }
313             }
314         }
315
316         // update template name
317         if (null !== $template && null === $this->filename) {
318             $this->filename = $template->getTemplateName();
319         }
320
321         // update template path if any
322         if (null !== $template && null === $this->sourcePath) {
323             $src = $template->getSourceContext();
324             $this->sourceCode = $src->getCode();
325             $this->sourcePath = $src->getPath();
326         }
327
328         if (null === $template || $this->lineno > -1) {
329             return;
330         }
331
332         $r = new ReflectionObject($template);
333         $file = $r->getFileName();
334
335         $exceptions = array($e = $this);
336         while (($e instanceof self || method_exists($e, 'getPrevious')) && $e = $e->getPrevious()) {
337             $exceptions[] = $e;
338         }
339
340         while ($e = array_pop($exceptions)) {
341             $traces = $e->getTrace();
342             array_unshift($traces, array('file' => $e->getFile(), 'line' => $e->getLine()));
343
344             while ($trace = array_shift($traces)) {
345                 if (!isset($trace['file']) || !isset($trace['line']) || $file != $trace['file']) {
346                     continue;
347                 }
348
349                 foreach ($template->getDebugInfo() as $codeLine => $templateLine) {
350                     if ($codeLine <= $trace['line']) {
351                         // update template line
352                         $this->lineno = $templateLine;
353
354                         return;
355                     }
356                 }
357             }
358         }
359     }
360 }
361
362 class_alias('Twig_Error', 'Twig\Error\Error', false);
363 class_exists('Twig_Source');