Add template path to CompilerException to enable rich debug features (#936)

* Add template path to CompilerException to enable rich debug features
Fixes #935
This commit is contained in:
Simon Wisselink
2024-02-24 23:36:06 +01:00
committed by GitHub
parent 66edb56911
commit 2b0ba0eabc
5 changed files with 6 additions and 4 deletions

1
changelog/935.md Normal file
View File

@@ -0,0 +1 @@
- Add template path to CompilerException to enable rich debug features [#935](https://github.com/smarty-php/smarty/issues/935)

View File

@@ -117,6 +117,7 @@ The following constants have been removed to prevent global side effects.
- Smarty now always runs in multibyte mode. Make sure you use the [PHP multibyte extension](https://www.php.net/manual/en/book.mbstring.php) in production for optimal performance.
- Generated `<script>` tags lo longer have deprecated `type="text/javascript"` or `language="Javascript"` attributes
- Smarty will throw a compiler exception instead of silently ignoring a modifier on a function call, like this: `{include|dot:"x-template-id" file="included.dot.tpl"}`
- The ::getFile() method of a CompilerException will now return the full path of the template being compiled, if possible. This used to be 'file:relative_dir/filename.tpl'.
## Upgrading from v3 to v4

View File

@@ -121,6 +121,6 @@ class CodeFrame
* @return string
*/
public function insertLocalVariables(): string {
return '$_smarty_current_dir = ' . var_export(dirname($this->_template->getSource()->getFilepath()), true) . ";\n";
return '$_smarty_current_dir = ' . var_export(dirname($this->_template->getSource()->getFilepath() ?? '.'), true) . ";\n";
}
}

View File

@@ -848,7 +848,7 @@ class Template extends BaseCompiler {
$e = new CompilerException(
$error_text,
0,
$this->template->getSource()->getFullResourceName(),
$this->template->getSource()->getFilepath() ?? $this->template->getSource()->getFullResourceName(),
$line
);
$e->source = trim(preg_replace('![\t\r\n]+!', ' ', $match[$line - 1]));

View File

@@ -271,11 +271,11 @@ class Source {
return $this->type . ':' . $this->name;
}
public function getFilepath(): string {
public function getFilepath(): ?string {
if ($this->handler instanceof FilePlugin) {
return $this->handler->getFilePath($this->name, $this->smarty, $this->isConfig);
}
return '.';
return null;
}
public function isConfig(): bool {