Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading