4 * This file is part of Twig.
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
13 * Twig base exception.
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).
21 * To help debugging template issues, this class tracks the original template
22 * name and line where the error occurred.
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.
32 * @author Fabien Potencier <fabien@symfony.com>
34 class Twig_Error extends Exception
37 // to be renamed to name in 2.0
39 protected $rawMessage;
48 * Set both the line number and the name to false to
49 * disable automatic guessing of the original template name
52 * Set the line number to -1 to enable its automatic guessing.
53 * Set the name to null to enable its automatic guessing.
55 * By default, automatic guessing is enabled.
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
62 public function __construct($message, $lineno = -1, $source = null, Exception $previous = null)
64 if (null === $source) {
66 } elseif (!$source instanceof Twig_Source) {
67 // for compat with the Twig C ext., passing the template name as string is accepted
70 $name = $source->getName();
71 $this->sourceCode = $source->getCode();
72 $this->sourcePath = $source->getPath();
74 if (PHP_VERSION_ID < 50300) {
75 $this->previous = $previous;
76 parent::__construct('');
78 parent::__construct('', 0, $previous);
81 $this->lineno = $lineno;
82 $this->filename = $name;
84 if (-1 === $lineno || null === $name || null === $this->sourcePath) {
85 $this->guessTemplateInfo();
88 $this->rawMessage = $message;
94 * Gets the raw message.
96 * @return string The raw message
98 public function getRawMessage()
100 return $this->rawMessage;
104 * Gets the logical name where the error occurred.
106 * @return string The name
108 * @deprecated since 1.27 (to be removed in 2.0). Use getSourceContext() instead.
110 public function getTemplateFile()
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);
114 return $this->filename;
118 * Sets the logical name where the error occurred.
120 * @param string $name The name
122 * @deprecated since 1.27 (to be removed in 2.0). Use setSourceContext() instead.
124 public function setTemplateFile($name)
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);
128 $this->filename = $name;
134 * Gets the logical name where the error occurred.
136 * @return string The name
138 * @deprecated since 1.29 (to be removed in 2.0). Use getSourceContext() instead.
140 public function getTemplateName()
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);
144 return $this->filename;
148 * Sets the logical name where the error occurred.
150 * @param string $name The name
152 * @deprecated since 1.29 (to be removed in 2.0). Use setSourceContext() instead.
154 public function setTemplateName($name)
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);
158 $this->filename = $name;
159 $this->sourceCode = $this->sourcePath = null;
165 * Gets the template line where the error occurred.
167 * @return int The template line
169 public function getTemplateLine()
171 return $this->lineno;
175 * Sets the template line where the error occurred.
177 * @param int $lineno The template line
179 public function setTemplateLine($lineno)
181 $this->lineno = $lineno;
187 * Gets the source context of the Twig template where the error occurred.
189 * @return Twig_Source|null
191 public function getSourceContext()
193 return $this->filename ? new Twig_Source($this->sourceCode, $this->filename, $this->sourcePath) : null;
197 * Sets the source context of the Twig template where the error occurred.
199 public function setSourceContext(Twig_Source $source = null)
201 if (null === $source) {
202 $this->sourceCode = $this->filename = $this->sourcePath = null;
204 $this->sourceCode = $source->getCode();
205 $this->filename = $source->getName();
206 $this->sourcePath = $source->getPath();
212 public function guess()
214 $this->guessTemplateInfo();
219 * For PHP < 5.3.0, provides access to the getPrevious() method.
221 * @param string $method The method name
222 * @param array $arguments The parameters to be passed to the method
224 * @return Exception The previous exception or null
226 * @throws BadMethodCallException
228 public function __call($method, $arguments)
230 if ('getprevious' == strtolower($method)) {
231 return $this->previous;
234 throw new BadMethodCallException(sprintf('Method "Twig_Error::%s()" does not exist.', $method));
237 public function appendMessage($rawMessage)
239 $this->rawMessage .= $rawMessage;
246 protected function updateRepr()
248 $this->message = $this->rawMessage;
250 if ($this->sourcePath && $this->lineno > 0) {
251 $this->file = $this->sourcePath;
252 $this->line = $this->lineno;
258 if ('.' === substr($this->message, -1)) {
259 $this->message = substr($this->message, 0, -1);
263 $questionMark = false;
264 if ('?' === substr($this->message, -1)) {
265 $this->message = substr($this->message, 0, -1);
266 $questionMark = true;
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);
273 $name = json_encode($this->filename);
275 $this->message .= sprintf(' in %s', $name);
278 if ($this->lineno && $this->lineno >= 0) {
279 $this->message .= sprintf(' at line %d', $this->lineno);
283 $this->message .= '.';
287 $this->message .= '?';
294 protected function guessTemplateInfo()
297 $templateClass = null;
299 if (PHP_VERSION_ID >= 50306) {
300 $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS | DEBUG_BACKTRACE_PROVIDE_OBJECT);
302 $backtrace = debug_backtrace();
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']);
316 // update template name
317 if (null !== $template && null === $this->filename) {
318 $this->filename = $template->getTemplateName();
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();
328 if (null === $template || $this->lineno > -1) {
332 $r = new ReflectionObject($template);
333 $file = $r->getFileName();
335 $exceptions = array($e = $this);
336 while (($e instanceof self || method_exists($e, 'getPrevious')) && $e = $e->getPrevious()) {
340 while ($e = array_pop($exceptions)) {
341 $traces = $e->getTrace();
342 array_unshift($traces, array('file' => $e->getFile(), 'line' => $e->getLine()));
344 while ($trace = array_shift($traces)) {
345 if (!isset($trace['file']) || !isset($trace['line']) || $file != $trace['file']) {
349 foreach ($template->getDebugInfo() as $codeLine => $templateLine) {
350 if ($codeLine <= $trace['line']) {
351 // update template line
352 $this->lineno = $templateLine;
362 class_alias('Twig_Error', 'Twig\Error\Error', false);
363 class_exists('Twig_Source');