| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of Twig. |
| 5 |
* |
| 6 |
* (c) 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 |
namespace ElementorDeps\Twig\Error; |
| 12 |
|
| 13 |
use ElementorDeps\Twig\Source; |
| 14 |
use ElementorDeps\Twig\Template; |
| 15 |
/** |
| 16 |
* Twig base exception. |
| 17 |
* |
| 18 |
* This exception class and its children must only be used when |
| 19 |
* an error occurs during the loading of a template, when a syntax error |
| 20 |
* is detected in a template, or when rendering a template. Other |
| 21 |
* errors must use regular PHP exception classes (like when the template |
| 22 |
* cache directory is not writable for instance). |
| 23 |
* |
| 24 |
* To help debugging template issues, this class tracks the original template |
| 25 |
* name and line where the error occurred. |
| 26 |
* |
| 27 |
* Whenever possible, you must set these information (original template name |
| 28 |
* and line number) yourself by passing them to the constructor. If some or all |
| 29 |
* these information are not available from where you throw the exception, then |
| 30 |
* this class will guess them automatically (when the line number is set to -1 |
| 31 |
* and/or the name is set to null). As this is a costly operation, this |
| 32 |
* can be disabled by passing false for both the name and the line number |
| 33 |
* when creating a new instance of this class. |
| 34 |
* |
| 35 |
* @author Fabien Potencier <fabien@symfony.com> |
| 36 |
*/ |
| 37 |
class Error extends \Exception |
| 38 |
{ |
| 39 |
private $lineno; |
| 40 |
private $name; |
| 41 |
private $rawMessage; |
| 42 |
private $sourcePath; |
| 43 |
private $sourceCode; |
| 44 |
/** |
| 45 |
* Constructor. |
| 46 |
* |
| 47 |
* By default, automatic guessing is enabled. |
| 48 |
* |
| 49 |
* @param string $message The error message |
| 50 |
* @param int $lineno The template line where the error occurred |
| 51 |
* @param Source|null $source The source context where the error occurred |
| 52 |
*/ |
| 53 |
public function __construct(string $message, int $lineno = -1, ?Source $source = null, ?\Throwable $previous = null) |
| 54 |
{ |
| 55 |
parent::__construct('', 0, $previous); |
| 56 |
if (null === $source) { |
| 57 |
$name = null; |
| 58 |
} else { |
| 59 |
$name = $source->getName(); |
| 60 |
$this->sourceCode = $source->getCode(); |
| 61 |
$this->sourcePath = $source->getPath(); |
| 62 |
} |
| 63 |
$this->lineno = $lineno; |
| 64 |
$this->name = $name; |
| 65 |
$this->rawMessage = $message; |
| 66 |
$this->updateRepr(); |
| 67 |
} |
| 68 |
public function getRawMessage() : string |
| 69 |
{ |
| 70 |
return $this->rawMessage; |
| 71 |
} |
| 72 |
public function getTemplateLine() : int |
| 73 |
{ |
| 74 |
return $this->lineno; |
| 75 |
} |
| 76 |
public function setTemplateLine(int $lineno) : void |
| 77 |
{ |
| 78 |
$this->lineno = $lineno; |
| 79 |
$this->updateRepr(); |
| 80 |
} |
| 81 |
public function getSourceContext() : ?Source |
| 82 |
{ |
| 83 |
return $this->name ? new Source($this->sourceCode, $this->name, $this->sourcePath) : null; |
| 84 |
} |
| 85 |
public function setSourceContext(?Source $source = null) : void |
| 86 |
{ |
| 87 |
if (null === $source) { |
| 88 |
$this->sourceCode = $this->name = $this->sourcePath = null; |
| 89 |
} else { |
| 90 |
$this->sourceCode = $source->getCode(); |
| 91 |
$this->name = $source->getName(); |
| 92 |
$this->sourcePath = $source->getPath(); |
| 93 |
} |
| 94 |
$this->updateRepr(); |
| 95 |
} |
| 96 |
public function guess() : void |
| 97 |
{ |
| 98 |
$this->guessTemplateInfo(); |
| 99 |
$this->updateRepr(); |
| 100 |
} |
| 101 |
public function appendMessage($rawMessage) : void |
| 102 |
{ |
| 103 |
$this->rawMessage .= $rawMessage; |
| 104 |
$this->updateRepr(); |
| 105 |
} |
| 106 |
private function updateRepr() : void |
| 107 |
{ |
| 108 |
$this->message = $this->rawMessage; |
| 109 |
if ($this->sourcePath && $this->lineno > 0) { |
| 110 |
$this->file = $this->sourcePath; |
| 111 |
$this->line = $this->lineno; |
| 112 |
return; |
| 113 |
} |
| 114 |
$dot = \false; |
| 115 |
if (\str_ends_with($this->message, '.')) { |
| 116 |
$this->message = \substr($this->message, 0, -1); |
| 117 |
$dot = \true; |
| 118 |
} |
| 119 |
$questionMark = \false; |
| 120 |
if (\str_ends_with($this->message, '?')) { |
| 121 |
$this->message = \substr($this->message, 0, -1); |
| 122 |
$questionMark = \true; |
| 123 |
} |
| 124 |
if ($this->name) { |
| 125 |
if (\is_string($this->name) || \is_object($this->name) && \method_exists($this->name, '__toString')) { |
| 126 |
$name = \sprintf('"%s"', $this->name); |
| 127 |
} else { |
| 128 |
$name = \json_encode($this->name); |
| 129 |
} |
| 130 |
$this->message .= \sprintf(' in %s', $name); |
| 131 |
} |
| 132 |
if ($this->lineno && $this->lineno >= 0) { |
| 133 |
$this->message .= \sprintf(' at line %d', $this->lineno); |
| 134 |
} |
| 135 |
if ($dot) { |
| 136 |
$this->message .= '.'; |
| 137 |
} |
| 138 |
if ($questionMark) { |
| 139 |
$this->message .= '?'; |
| 140 |
} |
| 141 |
} |
| 142 |
private function guessTemplateInfo() : void |
| 143 |
{ |
| 144 |
$template = null; |
| 145 |
$templateClass = null; |
| 146 |
$backtrace = \debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS | \DEBUG_BACKTRACE_PROVIDE_OBJECT); |
| 147 |
foreach ($backtrace as $trace) { |
| 148 |
if (isset($trace['object']) && $trace['object'] instanceof Template) { |
| 149 |
$currentClass = \get_class($trace['object']); |
| 150 |
$isEmbedContainer = null === $templateClass ? \false : \str_starts_with($templateClass, $currentClass); |
| 151 |
if (null === $this->name || $this->name == $trace['object']->getTemplateName() && !$isEmbedContainer) { |
| 152 |
$template = $trace['object']; |
| 153 |
$templateClass = \get_class($trace['object']); |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
// update template name |
| 158 |
if (null !== $template && null === $this->name) { |
| 159 |
$this->name = $template->getTemplateName(); |
| 160 |
} |
| 161 |
// update template path if any |
| 162 |
if (null !== $template && null === $this->sourcePath) { |
| 163 |
$src = $template->getSourceContext(); |
| 164 |
$this->sourceCode = $src->getCode(); |
| 165 |
$this->sourcePath = $src->getPath(); |
| 166 |
} |
| 167 |
if (null === $template || $this->lineno > -1) { |
| 168 |
return; |
| 169 |
} |
| 170 |
$r = new \ReflectionObject($template); |
| 171 |
$file = $r->getFileName(); |
| 172 |
$exceptions = [$e = $this]; |
| 173 |
while ($e = $e->getPrevious()) { |
| 174 |
$exceptions[] = $e; |
| 175 |
} |
| 176 |
while ($e = \array_pop($exceptions)) { |
| 177 |
$traces = $e->getTrace(); |
| 178 |
\array_unshift($traces, ['file' => $e->getFile(), 'line' => $e->getLine()]); |
| 179 |
while ($trace = \array_shift($traces)) { |
| 180 |
if (!isset($trace['file']) || !isset($trace['line']) || $file != $trace['file']) { |
| 181 |
continue; |
| 182 |
} |
| 183 |
foreach ($template->getDebugInfo() as $codeLine => $templateLine) { |
| 184 |
if ($codeLine <= $trace['line']) { |
| 185 |
// update template line |
| 186 |
$this->lineno = $templateLine; |
| 187 |
return; |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
|