PageRenderTime 38ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/ingsol/music_sonata
PHP | 199 lines | 112 code | 30 blank | 57 comment | 30 complexity | 9f9b8e34e1861afcca867ff843a90e9a MD5 | raw file
Possible License(s): BSD-2-Clause, LGPL-2.1, Apache-2.0, JSON, LGPL-3.0, BSD-3-Clause
  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 (version_compare(PHP_VERSION, '5.3.0', '<')) {
  33. $this->previous = $previous;
  34. parent::__construct('');
  35. } else {
  36. parent::__construct('', 0, $previous);
  37. }
  38. $this->lineno = $lineno;
  39. $this->filename = $filename;
  40. if (-1 === $this->lineno || null === $this->filename) {
  41. $this->guessTemplateInfo();
  42. }
  43. $this->rawMessage = $message;
  44. $this->updateRepr();
  45. }
  46. /**
  47. * Gets the raw message.
  48. *
  49. * @return string The raw message
  50. */
  51. public function getRawMessage()
  52. {
  53. return $this->rawMessage;
  54. }
  55. /**
  56. * Gets the filename where the error occurred.
  57. *
  58. * @return string The filename
  59. */
  60. public function getTemplateFile()
  61. {
  62. return $this->filename;
  63. }
  64. /**
  65. * Sets the filename where the error occurred.
  66. *
  67. * @param string $filename The filename
  68. */
  69. public function setTemplateFile($filename)
  70. {
  71. $this->filename = $filename;
  72. $this->updateRepr();
  73. }
  74. /**
  75. * Gets the template line where the error occurred.
  76. *
  77. * @return integer The template line
  78. */
  79. public function getTemplateLine()
  80. {
  81. return $this->lineno;
  82. }
  83. /**
  84. * Sets the template line where the error occurred.
  85. *
  86. * @param integer $lineno The template line
  87. */
  88. public function setTemplateLine($lineno)
  89. {
  90. $this->lineno = $lineno;
  91. $this->updateRepr();
  92. }
  93. /**
  94. * For PHP < 5.3.0, provides access to the getPrevious() method.
  95. *
  96. * @param string $method The method name
  97. * @param array $arguments The parameters to be passed to the method
  98. *
  99. * @return Exception The previous exception or null
  100. */
  101. public function __call($method, $arguments)
  102. {
  103. if ('getprevious' == strtolower($method)) {
  104. return $this->previous;
  105. }
  106. throw new BadMethodCallException(sprintf('Method "Twig_Error::%s()" does not exist.', $method));
  107. }
  108. protected function updateRepr()
  109. {
  110. $this->message = $this->rawMessage;
  111. $dot = false;
  112. if ('.' === substr($this->message, -1)) {
  113. $this->message = substr($this->message, 0, -1);
  114. $dot = true;
  115. }
  116. if (null !== $this->filename) {
  117. if (is_string($this->filename) || (is_object($this->filename) && method_exists($this->filename, '__toString'))) {
  118. $filename = sprintf('"%s"', $this->filename);
  119. } else {
  120. $filename = json_encode($this->filename);
  121. }
  122. $this->message .= sprintf(' in %s', $filename);
  123. }
  124. if ($this->lineno >= 0) {
  125. $this->message .= sprintf(' at line %d', $this->lineno);
  126. }
  127. if ($dot) {
  128. $this->message .= '.';
  129. }
  130. }
  131. protected function guessTemplateInfo()
  132. {
  133. $template = null;
  134. foreach (debug_backtrace() as $trace) {
  135. if (isset($trace['object']) && $trace['object'] instanceof Twig_Template && 'Twig_Template' !== get_class($trace['object'])) {
  136. $template = $trace['object'];
  137. // update template filename
  138. if (null === $this->filename) {
  139. $this->filename = $template->getTemplateName();
  140. }
  141. break;
  142. }
  143. }
  144. if (null === $template || $this->lineno > -1) {
  145. return;
  146. }
  147. $r = new ReflectionObject($template);
  148. $file = $r->getFileName();
  149. $exceptions = array($e = $this);
  150. while (($e instanceof self || method_exists($e, 'getPrevious')) && $e = $e->getPrevious()) {
  151. $exceptions[] = $e;
  152. }
  153. while ($e = array_pop($exceptions)) {
  154. $traces = $e->getTrace();
  155. while ($trace = array_shift($traces)) {
  156. if (!isset($trace['file']) || !isset($trace['line']) || $file != $trace['file']) {
  157. continue;
  158. }
  159. foreach ($template->getDebugInfo() as $codeLine => $templateLine) {
  160. if ($codeLine <= $trace['line']) {
  161. // update template line
  162. $this->lineno = $templateLine;
  163. return;
  164. }
  165. }
  166. }
  167. }
  168. }
  169. }