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
…936)

* Add template path to CompilerException to enable rich debug features
Fixes #935
  • Loading branch information
wisskid authored Feb 24, 2024
1 parent 66edb56 commit 2b0ba0e
Show file tree
Hide file tree
Showing 5 changed files with 6 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)
1 change: 1 addition & 0 deletions docs/upgrading.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
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 2b0ba0e

Please sign in to comment.