1
0
mirror of https://github.com/vichan-devel/vichan.git synced 2024-11-25 07:50:23 +01:00
vichan/inc/lib/Twig/Error.php

251 lines
7.4 KiB
PHP
Raw Normal View History

2011-10-05 06:22:53 +02:00
<?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.
*
2013-08-01 21:20:12 +02:00
* This exception class and its children must only be used when
* an error occurs during the loading of a template, when a syntax error
* is detected in a template, or when rendering a template. Other
* errors must use regular PHP exception classes (like when the template
* cache directory is not writable for instance).
*
* To help debugging template issues, this class tracks the original template
* name and line where the error occurred.
*
* Whenever possible, you must set these information (original template name
* and line number) yourself by passing them to the constructor. If some or all
* these information are not available from where you throw the exception, then
* this class will guess them automatically (when the line number is set to -1
* and/or the filename is set to null). As this is a costly operation, this
* can be disabled by passing false for both the filename and the line number
* when creating a new instance of this class.
*
* @author Fabien Potencier <fabien@symfony.com>
2011-10-05 06:22:53 +02:00
*/
class Twig_Error extends Exception
{
protected $lineno;
protected $filename;
protected $rawMessage;
protected $previous;
/**
* Constructor.
*
2013-08-01 21:20:12 +02:00
* Set both the line number and the filename to false to
* disable automatic guessing of the original template name
* and line number.
*
* Set the line number to -1 to enable its automatic guessing.
* Set the filename to null to enable its automatic guessing.
*
* By default, automatic guessing is enabled.
*
2011-10-05 06:22:53 +02:00
* @param string $message The error message
2015-03-11 00:16:45 +01:00
* @param int $lineno The template line where the error occurred
2011-10-05 06:22:53 +02:00
* @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)
{
2015-03-11 00:16:45 +01:00
if (PHP_VERSION_ID < 50300) {
2013-08-01 21:20:12 +02:00
$this->previous = $previous;
parent::__construct('');
} else {
parent::__construct('', 0, $previous);
2011-10-05 06:22:53 +02:00
}
$this->lineno = $lineno;
$this->filename = $filename;
2013-08-01 21:20:12 +02:00
if (-1 === $this->lineno || null === $this->filename) {
$this->guessTemplateInfo();
}
2011-10-05 06:22:53 +02:00
$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.
*
2015-03-11 00:16:45 +01:00
* @return int The template line
2011-10-05 06:22:53 +02:00
*/
public function getTemplateLine()
{
return $this->lineno;
}
/**
* Sets the template line where the error occurred.
*
2015-03-11 00:16:45 +01:00
* @param int $lineno The template line
2011-10-05 06:22:53 +02:00
*/
public function setTemplateLine($lineno)
{
$this->lineno = $lineno;
$this->updateRepr();
}
2013-08-01 21:20:12 +02:00
public function guess()
{
$this->guessTemplateInfo();
$this->updateRepr();
}
2011-10-05 06:22:53 +02:00
/**
* For PHP < 5.3.0, provides access to the getPrevious() method.
*
2013-08-01 21:20:12 +02:00
* @param string $method The method name
* @param array $arguments The parameters to be passed to the method
2011-10-05 06:22:53 +02:00
*
* @return Exception The previous exception or null
2013-08-01 21:20:12 +02:00
*
* @throws BadMethodCallException
2011-10-05 06:22:53 +02:00
*/
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;
}
2013-08-01 21:20:12 +02:00
if ($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);
2011-10-05 06:22:53 +02:00
}
2013-08-01 21:20:12 +02:00
if ($this->lineno && $this->lineno >= 0) {
2011-10-05 06:22:53 +02:00
$this->message .= sprintf(' at line %d', $this->lineno);
}
if ($dot) {
$this->message .= '.';
}
}
2013-08-01 21:20:12 +02:00
protected function guessTemplateInfo()
2011-10-05 06:22:53 +02:00
{
2013-08-01 21:20:12 +02:00
$template = null;
2013-09-19 08:08:25 +02:00
$templateClass = null;
2013-08-01 21:20:12 +02:00
2015-03-11 00:16:45 +01:00
if (PHP_VERSION_ID >= 50306) {
2013-08-01 21:20:12 +02:00
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS | DEBUG_BACKTRACE_PROVIDE_OBJECT);
} else {
$backtrace = debug_backtrace();
2011-10-05 06:22:53 +02:00
}
2013-08-01 21:20:12 +02:00
foreach ($backtrace as $trace) {
if (isset($trace['object']) && $trace['object'] instanceof Twig_Template && 'Twig_Template' !== get_class($trace['object'])) {
2013-09-19 08:08:25 +02:00
$currentClass = get_class($trace['object']);
$isEmbedContainer = 0 === strpos($templateClass, $currentClass);
if (null === $this->filename || ($this->filename == $trace['object']->getTemplateName() && !$isEmbedContainer)) {
2013-08-01 21:20:12 +02:00
$template = $trace['object'];
2013-09-19 08:08:25 +02:00
$templateClass = get_class($trace['object']);
2013-08-01 21:20:12 +02:00
}
2011-10-05 06:22:53 +02:00
}
2013-08-01 21:20:12 +02:00
}
2011-10-05 06:22:53 +02:00
2013-08-01 21:20:12 +02:00
// update template filename
if (null !== $template && null === $this->filename) {
$this->filename = $template->getTemplateName();
}
2011-10-05 06:22:53 +02:00
2013-08-01 21:20:12 +02:00
if (null === $template || $this->lineno > -1) {
return;
}
2011-10-05 06:22:53 +02:00
2013-08-01 21:20:12 +02:00
$r = new ReflectionObject($template);
$file = $r->getFileName();
2011-10-05 06:22:53 +02:00
2015-03-11 00:16:45 +01:00
// hhvm has a bug where eval'ed files comes out as the current directory
if (is_dir($file)) {
$file = '';
}
2013-08-01 21:20:12 +02:00
$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();
2015-03-11 00:16:45 +01:00
array_unshift($traces, array('file' => $e->getFile(), 'line' => $e->getLine()));
2013-08-01 21:20:12 +02:00
while ($trace = array_shift($traces)) {
if (!isset($trace['file']) || !isset($trace['line']) || $file != $trace['file']) {
continue;
2011-10-05 06:22:53 +02:00
}
2013-08-01 21:20:12 +02:00
foreach ($template->getDebugInfo() as $codeLine => $templateLine) {
if ($codeLine <= $trace['line']) {
// update template line
$this->lineno = $templateLine;
return;
}
2011-10-05 06:22:53 +02:00
}
}
}
}
}