vendor/twig/twig/src/Error/Error.php line 58

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) Fabien Potencier
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Twig\Error;
  11. use Twig\Source;
  12. use Twig\Template;
  13. /**
  14. * Twig base exception.
  15. *
  16. * This exception class and its children must only be used when
  17. * an error occurs during the loading of a template, when a syntax error
  18. * is detected in a template, or when rendering a template. Other
  19. * errors must use regular PHP exception classes (like when the template
  20. * cache directory is not writable for instance).
  21. *
  22. * To help debugging template issues, this class tracks the original template
  23. * name and line where the error occurred.
  24. *
  25. * Whenever possible, you must set these information (original template name
  26. * and line number) yourself by passing them to the constructor. If some or all
  27. * these information are not available from where you throw the exception, then
  28. * this class will guess them automatically (when the line number is set to -1
  29. * and/or the name is set to null). As this is a costly operation, this
  30. * can be disabled by passing false for both the name and the line number
  31. * when creating a new instance of this class.
  32. *
  33. * @author Fabien Potencier <fabien@symfony.com>
  34. */
  35. class Error extends \Exception
  36. {
  37. private $lineno;
  38. private $name;
  39. private $rawMessage;
  40. private $sourcePath;
  41. private $sourceCode;
  42. /**
  43. * Constructor.
  44. *
  45. * Set the line number to -1 to enable its automatic guessing.
  46. * Set the name to null to enable its automatic guessing.
  47. *
  48. * @param string $message The error message
  49. * @param int $lineno The template line where the error occurred
  50. * @param Source|string|null $source The source context where the error occurred
  51. * @param \Exception $previous The previous exception
  52. */
  53. public function __construct(string $message, int $lineno = -1, $source = null, \Exception $previous = null)
  54. {
  55. parent::__construct('', 0, $previous);
  56. if (null === $source) {
  57. $name = null;
  58. } elseif (!$source instanceof Source && !$source instanceof \Twig_Source) {
  59. @trigger_error(sprintf('Passing a string as a source to %s is deprecated since Twig 2.6.1; pass a Twig\Source instance instead.', __CLASS__), \E_USER_DEPRECATED);
  60. $name = $source;
  61. } else {
  62. $name = $source->getName();
  63. $this->sourceCode = $source->getCode();
  64. $this->sourcePath = $source->getPath();
  65. }
  66. $this->lineno = $lineno;
  67. $this->name = $name;
  68. $this->rawMessage = $message;
  69. $this->updateRepr();
  70. }
  71. /**
  72. * Gets the raw message.
  73. *
  74. * @return string The raw message
  75. */
  76. public function getRawMessage()
  77. {
  78. return $this->rawMessage;
  79. }
  80. /**
  81. * Gets the template line where the error occurred.
  82. *
  83. * @return int The template line
  84. */
  85. public function getTemplateLine()
  86. {
  87. return $this->lineno;
  88. }
  89. /**
  90. * Sets the template line where the error occurred.
  91. *
  92. * @param int $lineno The template line
  93. */
  94. public function setTemplateLine($lineno)
  95. {
  96. $this->lineno = $lineno;
  97. $this->updateRepr();
  98. }
  99. /**
  100. * Gets the source context of the Twig template where the error occurred.
  101. *
  102. * @return Source|null
  103. */
  104. public function getSourceContext()
  105. {
  106. return $this->name ? new Source($this->sourceCode, $this->name, $this->sourcePath) : null;
  107. }
  108. /**
  109. * Sets the source context of the Twig template where the error occurred.
  110. */
  111. public function setSourceContext(Source $source = null)
  112. {
  113. if (null === $source) {
  114. $this->sourceCode = $this->name = $this->sourcePath = null;
  115. } else {
  116. $this->sourceCode = $source->getCode();
  117. $this->name = $source->getName();
  118. $this->sourcePath = $source->getPath();
  119. }
  120. $this->updateRepr();
  121. }
  122. public function guess()
  123. {
  124. $this->guessTemplateInfo();
  125. $this->updateRepr();
  126. }
  127. public function appendMessage($rawMessage)
  128. {
  129. $this->rawMessage .= $rawMessage;
  130. $this->updateRepr();
  131. }
  132. private function updateRepr()
  133. {
  134. $this->message = $this->rawMessage;
  135. if ($this->sourcePath && $this->lineno > 0) {
  136. $this->file = $this->sourcePath;
  137. $this->line = $this->lineno;
  138. return;
  139. }
  140. $dot = false;
  141. if ('.' === substr($this->message, -1)) {
  142. $this->message = substr($this->message, 0, -1);
  143. $dot = true;
  144. }
  145. $questionMark = false;
  146. if ('?' === substr($this->message, -1)) {
  147. $this->message = substr($this->message, 0, -1);
  148. $questionMark = true;
  149. }
  150. if ($this->name) {
  151. if (\is_string($this->name) || (\is_object($this->name) && method_exists($this->name, '__toString'))) {
  152. $name = sprintf('"%s"', $this->name);
  153. } else {
  154. $name = json_encode($this->name);
  155. }
  156. $this->message .= sprintf(' in %s', $name);
  157. }
  158. if ($this->lineno && $this->lineno >= 0) {
  159. $this->message .= sprintf(' at line %d', $this->lineno);
  160. }
  161. if ($dot) {
  162. $this->message .= '.';
  163. }
  164. if ($questionMark) {
  165. $this->message .= '?';
  166. }
  167. }
  168. private function guessTemplateInfo()
  169. {
  170. $template = null;
  171. $templateClass = null;
  172. $backtrace = debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS | \DEBUG_BACKTRACE_PROVIDE_OBJECT);
  173. foreach ($backtrace as $trace) {
  174. if (isset($trace['object']) && $trace['object'] instanceof Template && 'Twig\Template' !== \get_class($trace['object'])) {
  175. $currentClass = \get_class($trace['object']);
  176. $isEmbedContainer = null === $templateClass ? false : 0 === strpos($templateClass, $currentClass);
  177. if (null === $this->name || ($this->name == $trace['object']->getTemplateName() && !$isEmbedContainer)) {
  178. $template = $trace['object'];
  179. $templateClass = \get_class($trace['object']);
  180. }
  181. }
  182. }
  183. // update template name
  184. if (null !== $template && null === $this->name) {
  185. $this->name = $template->getTemplateName();
  186. }
  187. // update template path if any
  188. if (null !== $template && null === $this->sourcePath) {
  189. $src = $template->getSourceContext();
  190. $this->sourceCode = $src->getCode();
  191. $this->sourcePath = $src->getPath();
  192. }
  193. if (null === $template || $this->lineno > -1) {
  194. return;
  195. }
  196. $r = new \ReflectionObject($template);
  197. $file = $r->getFileName();
  198. $exceptions = [$e = $this];
  199. while ($e = $e->getPrevious()) {
  200. $exceptions[] = $e;
  201. }
  202. while ($e = array_pop($exceptions)) {
  203. $traces = $e->getTrace();
  204. array_unshift($traces, ['file' => $e->getFile(), 'line' => $e->getLine()]);
  205. while ($trace = array_shift($traces)) {
  206. if (!isset($trace['file']) || !isset($trace['line']) || $file != $trace['file']) {
  207. continue;
  208. }
  209. foreach ($template->getDebugInfo() as $codeLine => $templateLine) {
  210. if ($codeLine <= $trace['line']) {
  211. // update template line
  212. $this->lineno = $templateLine;
  213. return;
  214. }
  215. }
  216. }
  217. }
  218. }
  219. }
  220. class_alias('Twig\Error\Error', 'Twig_Error');