PageRenderTime 36ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/twig/twig/lib/Twig/Error.php

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