Skip to content

Commit

Permalink
Add template path to CompilerException to enable rich debug features
Browse files Browse the repository at this point in the history
Fixes #935
  • Loading branch information
wisskid committed Feb 24, 2024
1 parent 66edb56 commit cc6812d
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 4 deletions.
1 change: 1 addition & 0 deletions changelog/935.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Add template path to CompilerException to enable rich debug features [#935](https://github.com/smarty-php/smarty/issues/935)
2 changes: 1 addition & 1 deletion src/Compiler/CodeFrame.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
}
2 changes: 1 addition & 1 deletion src/Compiler/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]));
Expand Down
4 changes: 2 additions & 2 deletions src/Template/Source.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit cc6812d

Please sign in to comment.