ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Twig_Error Class Reference

Twig base exception. More...

+ Inheritance diagram for Twig_Error:
+ Collaboration diagram for Twig_Error:

Public Member Functions

 __construct ($message, $lineno=-1, $source=null, Exception $previous=null)
 Constructor. More...
 
 getRawMessage ()
 Gets the raw message. More...
 
 getTemplateFile ()
 Gets the logical name where the error occurred. More...
 
 setTemplateFile ($name)
 Sets the logical name where the error occurred. More...
 
 getTemplateName ()
 Gets the logical name where the error occurred. More...
 
 setTemplateName ($name)
 Sets the logical name where the error occurred. More...
 
 getTemplateLine ()
 Gets the template line where the error occurred. More...
 
 setTemplateLine ($lineno)
 Sets the template line where the error occurred. More...
 
 getSourceContext ()
 Gets the source context of the Twig template where the error occurred. More...
 
 setSourceContext (Twig_Source $source=null)
 Sets the source context of the Twig template where the error occurred. More...
 
 guess ()
 
 __call ($method, $arguments)
 For PHP < 5.3.0, provides access to the getPrevious() method. More...
 
 appendMessage ($rawMessage)
 

Protected Member Functions

 updateRepr ()
 
 guessTemplateInfo ()
 

Protected Attributes

 $lineno
 
 $filename
 
 $rawMessage
 
 $previous
 

Private Attributes

 $sourcePath
 
 $sourceCode
 

Detailed Description

Twig base exception.

This exception class and its children must only be used when an error occurs during the loading of a template, when a syntax error is detected in a template, or when rendering a template. Other errors must use regular PHP exception classes (like when the template cache directory is not writable for instance).

To help debugging template issues, this class tracks the original template name and line where the error occurred.

Whenever possible, you must set these information (original template name and line number) yourself by passing them to the constructor. If some or all these information are not available from where you throw the exception, then this class will guess them automatically (when the line number is set to -1 and/or the name is set to null). As this is a costly operation, this can be disabled by passing false for both the name and the line number when creating a new instance of this class.

Author
Fabien Potencier fabie.nosp@m.n@sy.nosp@m.mfony.nosp@m..com

Definition at line 34 of file Error.php.

Constructor & Destructor Documentation

◆ __construct()

Twig_Error::__construct (   $message,
  $lineno = -1,
  $source = null,
Exception  $previous = null 
)

Constructor.

Set both the line number and the name to false to disable automatic guessing of the original template name and line number.

Set the line number to -1 to enable its automatic guessing. Set the name to null to enable its automatic guessing.

By default, automatic guessing is enabled.

Parameters
string$messageThe error message
int$linenoThe template line where the error occurred
Twig_Source | string | null$sourceThe source context where the error occurred
Exception$previousThe previous exception

Definition at line 62 of file Error.php.

References $lineno, $message, $name, $previous, $source, guessTemplateInfo(), and updateRepr().

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  }
updateRepr()
Definition: Error.php:246
catch(Exception $e) $message
guessTemplateInfo()
Definition: Error.php:294
Holds information about a non-compiled Twig template.
Definition: Source.php:19
$source
Definition: linkback.php:22
+ Here is the call graph for this function:

Member Function Documentation

◆ __call()

Twig_Error::__call (   $method,
  $arguments 
)

For PHP < 5.3.0, provides access to the getPrevious() method.

Parameters
string$methodThe method name
array$argumentsThe parameters to be passed to the method
Returns
Exception The previous exception or null
Exceptions
BadMethodCallException

Definition at line 228 of file Error.php.

References $previous.

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  }

◆ appendMessage()

Twig_Error::appendMessage (   $rawMessage)

Definition at line 237 of file Error.php.

References $rawMessage, and updateRepr().

Referenced by Twig_Error_Loader\__construct(), Twig_Error_Syntax\addSuggestions(), and Twig_Parser\subparse().

238  {
239  $this->rawMessage .= $rawMessage;
240  $this->updateRepr();
241  }
updateRepr()
Definition: Error.php:246
$rawMessage
Definition: Error.php:39
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getRawMessage()

Twig_Error::getRawMessage ( )

Gets the raw message.

Returns
string The raw message

Definition at line 98 of file Error.php.

References $rawMessage.

99  {
100  return $this->rawMessage;
101  }
$rawMessage
Definition: Error.php:39

◆ getSourceContext()

Twig_Error::getSourceContext ( )

Gets the source context of the Twig template where the error occurred.

Returns
Twig_Source|null

Definition at line 191 of file Error.php.

Referenced by Twig_Template\displayBlock(), Twig_Template\displayWithErrorHandling(), Twig_Template\loadTemplate(), Twig_Parser\parse(), Twig_Tests_ErrorTest\testTwigExceptionAddsFileAndLine(), Twig_Tests_ErrorTest\testTwigExceptionGuessWithExceptionAndArrayLoader(), Twig_Tests_ErrorTest\testTwigExceptionGuessWithExceptionAndFilesystemLoader(), Twig_Tests_ErrorTest\testTwigExceptionGuessWithMissingVarAndArrayLoader(), and Twig_Tests_ErrorTest\testTwigExceptionGuessWithMissingVarAndFilesystemLoader().

192  {
193  return $this->filename ? new Twig_Source($this->sourceCode, $this->filename, $this->sourcePath) : null;
194  }
Holds information about a non-compiled Twig template.
Definition: Source.php:19
+ Here is the caller graph for this function:

◆ getTemplateFile()

Twig_Error::getTemplateFile ( )

Gets the logical name where the error occurred.

Returns
string The name
Deprecated:
since 1.27 (to be removed in 2.0). Use getSourceContext() instead.

Definition at line 110 of file Error.php.

References $filename.

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  }

◆ getTemplateLine()

◆ getTemplateName()

Twig_Error::getTemplateName ( )

Gets the logical name where the error occurred.

Returns
string The name
Deprecated:
since 1.29 (to be removed in 2.0). Use getSourceContext() instead.

Definition at line 140 of file Error.php.

References $filename.

Referenced by guessTemplateInfo().

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  }
+ Here is the caller graph for this function:

◆ guess()

Twig_Error::guess ( )

Definition at line 212 of file Error.php.

References guessTemplateInfo(), and updateRepr().

Referenced by Twig_Template\displayBlock(), Twig_Template\displayWithErrorHandling(), Twig_Template\getParent(), and Twig_Template\loadTemplate().

213  {
214  $this->guessTemplateInfo();
215  $this->updateRepr();
216  }
updateRepr()
Definition: Error.php:246
guessTemplateInfo()
Definition: Error.php:294
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ guessTemplateInfo()

Twig_Error::guessTemplateInfo ( )
protected

Definition at line 294 of file Error.php.

References $exceptions, $r, $template, and getTemplateName().

Referenced by __construct(), and guess().

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  }
$template
$r
Definition: example_031.php:79
Default base class for compiled templates.
Definition: Template.php:24
getTemplateName()
Gets the logical name where the error occurred.
Definition: Error.php:140
$exceptions
Definition: Utf8Test.php:67
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setSourceContext()

Twig_Error::setSourceContext ( Twig_Source  $source = null)

Sets the source context of the Twig template where the error occurred.

Definition at line 199 of file Error.php.

References $source, and updateRepr().

Referenced by Twig_Environment\compileSource(), Twig_Template\displayBlock(), Twig_Template\displayWithErrorHandling(), Twig_Template\getParent(), Twig_Template\loadTemplate(), and Twig_Parser\parse().

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  }
updateRepr()
Definition: Error.php:246
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setTemplateFile()

Twig_Error::setTemplateFile (   $name)

Sets the logical name where the error occurred.

Parameters
string$nameThe name
Deprecated:
since 1.27 (to be removed in 2.0). Use setSourceContext() instead.

Definition at line 124 of file Error.php.

References $name, and updateRepr().

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  }
updateRepr()
Definition: Error.php:246
+ Here is the call graph for this function:

◆ setTemplateLine()

Twig_Error::setTemplateLine (   $lineno)

Sets the template line where the error occurred.

Parameters
int$linenoThe template line

Definition at line 179 of file Error.php.

References $lineno, and updateRepr().

Referenced by Twig_Error_Loader\__construct(), Twig_Template\displayBlock(), Twig_Template\displayWithErrorHandling(), Twig_Template\loadTemplate(), and Twig_Parser\parse().

180  {
181  $this->lineno = $lineno;
182 
183  $this->updateRepr();
184  }
updateRepr()
Definition: Error.php:246
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setTemplateName()

Twig_Error::setTemplateName (   $name)

Sets the logical name where the error occurred.

Parameters
string$nameThe name
Deprecated:
since 1.29 (to be removed in 2.0). Use setSourceContext() instead.

Definition at line 154 of file Error.php.

References $name, and updateRepr().

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  }
updateRepr()
Definition: Error.php:246
+ Here is the call graph for this function:

◆ updateRepr()

Twig_Error::updateRepr ( )
protected

Definition at line 246 of file Error.php.

References $lineno, $name, $rawMessage, and $sourcePath.

Referenced by __construct(), appendMessage(), guess(), setSourceContext(), setTemplateFile(), setTemplateLine(), and setTemplateName().

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  }
$sourcePath
Definition: Error.php:42
$rawMessage
Definition: Error.php:39
+ Here is the caller graph for this function:

Field Documentation

◆ $filename

◆ $lineno

◆ $previous

◆ $rawMessage

Twig_Error::$rawMessage
protected

Definition at line 39 of file Error.php.

Referenced by appendMessage(), getRawMessage(), and updateRepr().

◆ $sourceCode

Twig_Error::$sourceCode
private

Definition at line 43 of file Error.php.

◆ $sourcePath

Twig_Error::$sourcePath
private

Definition at line 42 of file Error.php.

Referenced by updateRepr().


The documentation for this class was generated from the following file: