123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- <?php
-
- /*
- * This file is part of Twig.
- *
- * (c) 2009 Fabien Potencier
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
-
- /**
- * Twig base exception.
- *
- * @package twig
- * @author Fabien Potencier <fabien@symfony.com>
- */
- class Twig_Error extends Exception
- {
- protected $lineno;
- protected $filename;
- protected $rawMessage;
- protected $previous;
-
- /**
- * Constructor.
- *
- * @param string $message The error message
- * @param integer $lineno The template line where the error occurred
- * @param string $filename The template file name where the error occurred
- * @param Exception $previous The previous exception
- */
- public function __construct($message, $lineno = -1, $filename = null, Exception $previous = null)
- {
- if (version_compare(PHP_VERSION, '5.3.0', '<')) {
- $this->previous = $previous;
- parent::__construct('');
- } else {
- parent::__construct('', 0, $previous);
- }
-
- $this->lineno = $lineno;
- $this->filename = $filename;
-
- if (-1 === $this->lineno || null === $this->filename) {
- $this->guessTemplateInfo();
- }
-
- $this->rawMessage = $message;
-
- $this->updateRepr();
- }
-
- /**
- * Gets the raw message.
- *
- * @return string The raw message
- */
- public function getRawMessage()
- {
- return $this->rawMessage;
- }
-
- /**
- * Gets the filename where the error occurred.
- *
- * @return string The filename
- */
- public function getTemplateFile()
- {
- return $this->filename;
- }
-
- /**
- * Sets the filename where the error occurred.
- *
- * @param string $filename The filename
- */
- public function setTemplateFile($filename)
- {
- $this->filename = $filename;
-
- $this->updateRepr();
- }
-
- /**
- * Gets the template line where the error occurred.
- *
- * @return integer The template line
- */
- public function getTemplateLine()
- {
- return $this->lineno;
- }
-
- /**
- * Sets the template line where the error occurred.
- *
- * @param integer $lineno The template line
- */
- public function setTemplateLine($lineno)
- {
- $this->lineno = $lineno;
-
- $this->updateRepr();
- }
-
- /**
- * For PHP < 5.3.0, provides access to the getPrevious() method.
- *
- * @param string $method The method name
- * @param array $arguments The parameters to be passed to the method
- *
- * @return Exception The previous exception or null
- */
- public function __call($method, $arguments)
- {
- if ('getprevious' == strtolower($method)) {
- return $this->previous;
- }
-
- throw new BadMethodCallException(sprintf('Method "Twig_Error::%s()" does not exist.', $method));
- }
-
- protected function updateRepr()
- {
- $this->message = $this->rawMessage;
-
- $dot = false;
- if ('.' === substr($this->message, -1)) {
- $this->message = substr($this->message, 0, -1);
- $dot = true;
- }
-
- if (null !== $this->filename) {
- if (is_string($this->filename) || (is_object($this->filename) && method_exists($this->filename, '__toString'))) {
- $filename = sprintf('"%s"', $this->filename);
- } else {
- $filename = json_encode($this->filename);
- }
- $this->message .= sprintf(' in %s', $filename);
- }
-
- if ($this->lineno >= 0) {
- $this->message .= sprintf(' at line %d', $this->lineno);
- }
-
- if ($dot) {
- $this->message .= '.';
- }
- }
-
- protected function guessTemplateInfo()
- {
- $template = null;
- foreach (debug_backtrace() as $trace) {
- if (isset($trace['object']) && $trace['object'] instanceof Twig_Template && 'Twig_Template' !== get_class($trace['object'])) {
- $template = $trace['object'];
- }
- }
-
- // update template filename
- if (null !== $template && null === $this->filename) {
- $this->filename = $template->getTemplateName();
- }
-
- if (null === $template || $this->lineno > -1) {
- return;
- }
-
- $r = new ReflectionObject($template);
- $file = $r->getFileName();
-
- $exceptions = array($e = $this);
- while (($e instanceof self || method_exists($e, 'getPrevious')) && $e = $e->getPrevious()) {
- $exceptions[] = $e;
- }
-
- while ($e = array_pop($exceptions)) {
- $traces = $e->getTrace();
- while ($trace = array_shift($traces)) {
- if (!isset($trace['file']) || !isset($trace['line']) || $file != $trace['file']) {
- continue;
- }
-
- foreach ($template->getDebugInfo() as $codeLine => $templateLine) {
- if ($codeLine <= $trace['line']) {
- // update template line
- $this->lineno = $templateLine;
-
- return;
- }
- }
- }
- }
- }
- }
|