-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement template execution error handling
Closes #16.
- Loading branch information
Showing
2 changed files
with
56 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DlangDockerized\Ddct\Util; | ||
|
||
use Exception; | ||
use LogicException; | ||
|
||
final class TemplateExecutionErrorHandlers | ||
{ | ||
public const fileEndingCompiledTemplate = '.compiled-tpl.php'; | ||
|
||
private int $counter = 0; | ||
|
||
public function push(string $templateName): void | ||
{ | ||
$fileEnding = self::fileEndingCompiledTemplate; | ||
set_error_handler(function ($errno, $errstr, $errfile, $errline) use ($templateName, $fileEnding) { | ||
throw new Exception( | ||
"Template execution error (code: {$errno})" | ||
. " in `{$templateName}{$fileEnding}`({$errline}): {$errstr}" | ||
); | ||
}, E_ALL); | ||
|
||
++$this->counter; | ||
} | ||
|
||
public function pop(): void | ||
{ | ||
restore_error_handler(); | ||
--$this->counter; | ||
} | ||
|
||
public function resetAndCheck(): void | ||
{ | ||
if ($this->counter !== 0) { | ||
throw new LogicException('Popped less error handlers than pushed.'); | ||
} | ||
} | ||
} |