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