[780] | 1 | <?php |
---|
| 2 | |
---|
| 3 | /* |
---|
| 4 | * This file is part of Twig. |
---|
| 5 | * |
---|
| 6 | * (c) 2009 Fabien Potencier |
---|
| 7 | * |
---|
| 8 | * For the full copyright and license information, please view the LICENSE |
---|
| 9 | * file that was distributed with this source code. |
---|
| 10 | */ |
---|
| 11 | |
---|
| 12 | /** |
---|
| 13 | * Twig base exception. |
---|
| 14 | * |
---|
| 15 | * @package twig |
---|
| 16 | * @author Fabien Potencier <fabien.potencier@symfony-project.com> |
---|
| 17 | */ |
---|
| 18 | class Twig_Error extends Exception |
---|
| 19 | { |
---|
| 20 | protected $lineno; |
---|
| 21 | protected $filename; |
---|
| 22 | protected $rawMessage; |
---|
| 23 | protected $previous; |
---|
| 24 | |
---|
| 25 | /** |
---|
| 26 | * Constructor. |
---|
| 27 | * |
---|
| 28 | * @param string $message The error message |
---|
| 29 | * @param integer $lineno The template line where the error occurred |
---|
| 30 | * @param string $filename The template file name where the error occurred |
---|
| 31 | * @param Exception $previous The previous exception |
---|
| 32 | */ |
---|
| 33 | public function __construct($message, $lineno = -1, $filename = null, Exception $previous = null) |
---|
| 34 | { |
---|
| 35 | if (-1 === $lineno || null === $filename) { |
---|
| 36 | list($lineno, $filename) = $this->findTemplateInfo(null !== $previous ? $previous : $this); |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | $this->lineno = $lineno; |
---|
| 40 | $this->filename = $filename; |
---|
| 41 | $this->rawMessage = $message; |
---|
| 42 | |
---|
| 43 | $this->updateRepr(); |
---|
| 44 | |
---|
| 45 | if (version_compare(PHP_VERSION, '5.3.0', '<')) { |
---|
| 46 | $this->previous = $previous; |
---|
| 47 | parent::__construct($this->message); |
---|
| 48 | } else { |
---|
| 49 | parent::__construct($this->message, 0, $previous); |
---|
| 50 | } |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | /** |
---|
| 54 | * Gets the filename where the error occurred. |
---|
| 55 | * |
---|
| 56 | * @return string The filename |
---|
| 57 | */ |
---|
| 58 | public function getTemplateFile() |
---|
| 59 | { |
---|
| 60 | return $this->filename; |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | /** |
---|
| 64 | * Sets the filename where the error occurred. |
---|
| 65 | * |
---|
| 66 | * @param string $filename The filename |
---|
| 67 | */ |
---|
| 68 | public function setTemplateFile($filename) |
---|
| 69 | { |
---|
| 70 | $this->filename = $filename; |
---|
| 71 | |
---|
| 72 | $this->updateRepr(); |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | /** |
---|
| 76 | * Gets the template line where the error occurred. |
---|
| 77 | * |
---|
| 78 | * @return integer The template line |
---|
| 79 | */ |
---|
| 80 | public function getTemplateLine() |
---|
| 81 | { |
---|
| 82 | return $this->lineno; |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | /** |
---|
| 86 | * Sets the template line where the error occurred. |
---|
| 87 | * |
---|
| 88 | * @param integer $lineno The template line |
---|
| 89 | */ |
---|
| 90 | public function setTemplateLine($lineno) |
---|
| 91 | { |
---|
| 92 | $this->lineno = $lineno; |
---|
| 93 | |
---|
| 94 | $this->updateRepr(); |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | /** |
---|
| 98 | * For PHP < 5.3.0, provides access to the getPrevious() method. |
---|
| 99 | * |
---|
| 100 | * @param string $method The method name |
---|
| 101 | * @param array $arguments The parameters to be passed to the method |
---|
| 102 | * |
---|
| 103 | * @return Exception The previous exception or null |
---|
| 104 | */ |
---|
| 105 | public function __call($method, $arguments) |
---|
| 106 | { |
---|
| 107 | if ('getprevious' == strtolower($method)) { |
---|
| 108 | return $this->previous; |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | throw new BadMethodCallException(sprintf('Method "Twig_Error::%s()" does not exist.', $method)); |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | protected function updateRepr() |
---|
| 115 | { |
---|
| 116 | $this->message = $this->rawMessage; |
---|
| 117 | |
---|
| 118 | $dot = false; |
---|
| 119 | if ('.' === substr($this->message, -1)) { |
---|
| 120 | $this->message = substr($this->message, 0, -1); |
---|
| 121 | $dot = true; |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | if (null !== $this->filename) { |
---|
| 125 | $this->message .= sprintf(' in %s', json_encode($this->filename)); |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | if ($this->lineno >= 0) { |
---|
| 129 | $this->message .= sprintf(' at line %d', $this->lineno); |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | if ($dot) { |
---|
| 133 | $this->message .= '.'; |
---|
| 134 | } |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | protected function findTemplateInfo(Exception $e) |
---|
| 138 | { |
---|
| 139 | if (!function_exists('token_get_all')) { |
---|
| 140 | return array(-1, null); |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | $traces = $e->getTrace(); |
---|
| 144 | foreach ($traces as $i => $trace) { |
---|
| 145 | if (!isset($trace['class']) || 'Twig_Template' === $trace['class']) { |
---|
| 146 | continue; |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | $r = new ReflectionClass($trace['class']); |
---|
| 150 | if (!$r->implementsInterface('Twig_TemplateInterface')) { |
---|
| 151 | continue; |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | if (!file_exists($r->getFilename())) { |
---|
| 155 | // probably an eval()'d code |
---|
| 156 | return array(-1, null); |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | $trace = $traces[$i - 1]; |
---|
| 160 | if (!isset($trace['line'])) { |
---|
| 161 | $trace['line'] = -log(0); |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | $tokens = token_get_all(file_get_contents($r->getFilename())); |
---|
| 165 | $templateline = -1; |
---|
| 166 | $template = null; |
---|
| 167 | while ($token = array_shift($tokens)) { |
---|
| 168 | if (isset($token[2]) && $token[2] >= $trace['line']) { |
---|
| 169 | return array($templateline, $template); |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | if (T_COMMENT === $token[0] && null === $template && preg_match('#/\* +(.+) +\*/#', $token[1], $match)) { |
---|
| 173 | $template = $match[1]; |
---|
| 174 | } elseif (T_COMMENT === $token[0] && preg_match('#^//\s*line (\d+)\s*$#', $token[1], $match)) { |
---|
| 175 | $templateline = $match[1]; |
---|
| 176 | } |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | return array(-1, $template); |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | return array(-1, null); |
---|
| 183 | } |
---|
| 184 | } |
---|