Error.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) 2009 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. * @package twig
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. class Twig_Error extends Exception
  17. {
  18. protected $lineno;
  19. protected $filename;
  20. protected $rawMessage;
  21. protected $previous;
  22. /**
  23. * Constructor.
  24. *
  25. * @param string $message The error message
  26. * @param integer $lineno The template line where the error occurred
  27. * @param string $filename The template file name where the error occurred
  28. * @param Exception $previous The previous exception
  29. */
  30. public function __construct($message, $lineno = -1, $filename = null, Exception $previous = null)
  31. {
  32. if (-1 === $lineno || null === $filename) {
  33. if ($trace = $this->getTemplateTrace()) {
  34. if (-1 === $lineno) {
  35. $lineno = $this->guessTemplateLine($trace);
  36. }
  37. if (null === $filename) {
  38. $filename = $trace['object']->getTemplateName();
  39. }
  40. }
  41. }
  42. $this->lineno = $lineno;
  43. $this->filename = $filename;
  44. $this->rawMessage = $message;
  45. $this->updateRepr();
  46. if (version_compare(PHP_VERSION, '5.3.0', '<')) {
  47. $this->previous = $previous;
  48. parent::__construct($this->message);
  49. } else {
  50. parent::__construct($this->message, 0, $previous);
  51. }
  52. }
  53. /**
  54. * Gets the raw message.
  55. *
  56. * @return string The raw message
  57. */
  58. public function getRawMessage()
  59. {
  60. return $this->rawMessage;
  61. }
  62. /**
  63. * Gets the filename where the error occurred.
  64. *
  65. * @return string The filename
  66. */
  67. public function getTemplateFile()
  68. {
  69. return $this->filename;
  70. }
  71. /**
  72. * Sets the filename where the error occurred.
  73. *
  74. * @param string $filename The filename
  75. */
  76. public function setTemplateFile($filename)
  77. {
  78. $this->filename = $filename;
  79. $this->updateRepr();
  80. }
  81. /**
  82. * Gets the template line where the error occurred.
  83. *
  84. * @return integer The template line
  85. */
  86. public function getTemplateLine()
  87. {
  88. return $this->lineno;
  89. }
  90. /**
  91. * Sets the template line where the error occurred.
  92. *
  93. * @param integer $lineno The template line
  94. */
  95. public function setTemplateLine($lineno)
  96. {
  97. $this->lineno = $lineno;
  98. $this->updateRepr();
  99. }
  100. /**
  101. * For PHP < 5.3.0, provides access to the getPrevious() method.
  102. *
  103. * @param string $method The method name
  104. * @param array $arguments The parameters to be passed to the method
  105. *
  106. * @return Exception The previous exception or null
  107. */
  108. public function __call($method, $arguments)
  109. {
  110. if ('getprevious' == strtolower($method)) {
  111. return $this->previous;
  112. }
  113. throw new BadMethodCallException(sprintf('Method "Twig_Error::%s()" does not exist.', $method));
  114. }
  115. protected function updateRepr()
  116. {
  117. $this->message = $this->rawMessage;
  118. $dot = false;
  119. if ('.' === substr($this->message, -1)) {
  120. $this->message = substr($this->message, 0, -1);
  121. $dot = true;
  122. }
  123. if (null !== $this->filename) {
  124. $this->message .= sprintf(' in %s', is_string($this->filename) ? '"'.$this->filename.'"' : json_encode($this->filename));
  125. }
  126. if ($this->lineno >= 0) {
  127. $this->message .= sprintf(' at line %d', $this->lineno);
  128. }
  129. if ($dot) {
  130. $this->message .= '.';
  131. }
  132. }
  133. protected function getTemplateTrace()
  134. {
  135. foreach (debug_backtrace() as $trace) {
  136. if (isset($trace['object']) && $trace['object'] instanceof Twig_Template) {
  137. return $trace;
  138. }
  139. }
  140. }
  141. protected function guessTemplateLine($trace)
  142. {
  143. if (isset($trace['line'])) {
  144. foreach ($trace['object']->getDebugInfo() as $codeLine => $templateLine) {
  145. if ($codeLine <= $trace['line']) {
  146. return $templateLine;
  147. }
  148. }
  149. }
  150. return -1;
  151. }
  152. }