From cc6812d6e002110eb1870d84361a7356c41e6285 Mon Sep 17 00:00:00 2001 From: Simon Wisselink Date: Sat, 24 Feb 2024 22:31:40 +0100 Subject: [PATCH] Add template path to CompilerException to enable rich debug features Fixes #935 --- changelog/935.md | 1 + src/Compiler/CodeFrame.php | 2 +- src/Compiler/Template.php | 2 +- src/Template/Source.php | 4 ++-- 4 files changed, 5 insertions(+), 4 deletions(-) create mode 100644 changelog/935.md diff --git a/changelog/935.md b/changelog/935.md new file mode 100644 index 000000000..735a2af9f --- /dev/null +++ b/changelog/935.md @@ -0,0 +1 @@ +- Add template path to CompilerException to enable rich debug features [#935](https://github.com/smarty-php/smarty/issues/935) \ No newline at end of file diff --git a/src/Compiler/CodeFrame.php b/src/Compiler/CodeFrame.php index 69d17ed97..5e7821509 100644 --- a/src/Compiler/CodeFrame.php +++ b/src/Compiler/CodeFrame.php @@ -121,6 +121,6 @@ public function create( * @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"; } } diff --git a/src/Compiler/Template.php b/src/Compiler/Template.php index 184457f3a..c60aed212 100644 --- a/src/Compiler/Template.php +++ b/src/Compiler/Template.php @@ -848,7 +848,7 @@ public function trigger_template_error($args = null, $line = null, $tagline = nu $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])); diff --git a/src/Template/Source.php b/src/Template/Source.php index cba8106fc..382e710a7 100644 --- a/src/Template/Source.php +++ b/src/Template/Source.php @@ -271,11 +271,11 @@ public function getFullResourceName(): string { 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 {