- WIP on the exception handler
This commit is contained in:
parent
69db3efdce
commit
082bdd4ad3
|
@ -5,7 +5,6 @@ namespace %NAMESPACE%;
|
||||||
%USE%
|
%USE%
|
||||||
|
|
||||||
# %PATHNAME%
|
# %PATHNAME%
|
||||||
|
|
||||||
if (! class_exists("%NAMESPACE%\%CLASSNAME%", false) ) {
|
if (! class_exists("%NAMESPACE%\%CLASSNAME%", false) ) {
|
||||||
|
|
||||||
class %CLASSNAME% %EXTENDS% {
|
class %CLASSNAME% %EXTENDS% {
|
||||||
|
@ -28,6 +27,8 @@ if (! class_exists("%NAMESPACE%\%CLASSNAME%", false) ) {
|
||||||
$this->exportFunctions();
|
$this->exportFunctions();
|
||||||
|
|
||||||
static::$context = $picea->context;
|
static::$context = $picea->context;
|
||||||
|
|
||||||
|
$picea->declareLoadedTemplate(static::class, __FILE__);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function output(array $variablesList = []) : void
|
public function output(array $variablesList = []) : void
|
||||||
|
@ -61,14 +62,14 @@ if (! class_exists("%NAMESPACE%\%CLASSNAME%", false) ) {
|
||||||
%FUNCTIONS%
|
%FUNCTIONS%
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getSourceLineFromException(\Throwable $ex) : ? int
|
public static function getSourceLineFromException(int $sourceLine) : ? int
|
||||||
{
|
{
|
||||||
$sourceFile = file_get_contents("%TEMPLATE%");
|
$sourceFile = file_get_contents("%TEMPLATE%");
|
||||||
|
|
||||||
if ( $sourceFile ) {
|
if ( $sourceFile ) {
|
||||||
foreach(explode(PHP_EOL, $sourceFile) as $line => $content) {
|
foreach(explode(PHP_EOL, $sourceFile) as $line => $content) {
|
||||||
if ( strpos($content, str_replace('$', '%', '$CONTENT$')) !== false ) {
|
if ( strpos($content, str_replace('$', '%', '$CONTENT$')) !== false ) {
|
||||||
return $ex->getLine() - $line;
|
return $sourceLine - $line;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -76,6 +77,16 @@ if (! class_exists("%NAMESPACE%\%CLASSNAME%", false) ) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function getParam(string $param) : ? string
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'classname' => "%CLASSNAME%",
|
||||||
|
'namespace' => "%NAMESPACE%",
|
||||||
|
'extends' => "%EXTENDS_TEMPLATE%",
|
||||||
|
'view' => "%FULLPATH%",
|
||||||
|
][$param] ?? null;
|
||||||
|
}
|
||||||
|
|
||||||
public function __invoke(array $variablesList = []) : string
|
public function __invoke(array $variablesList = []) : string
|
||||||
{
|
{
|
||||||
ob_start();
|
ob_start();
|
||||||
|
@ -86,4 +97,5 @@ if (! class_exists("%NAMESPACE%\%CLASSNAME%", false) ) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return [ 'classname' => "%CLASSNAME%", 'namespace' => "%NAMESPACE%", 'extends' => "%EXTENDS_TEMPLATE%", 'view' => "%FULLPATH%" ];
|
|
||||||
|
return [ 'classname' => %CLASSNAME%::getParam('classname'), 'namespace' => %CLASSNAME%::getParam('namespace'), 'extends' => %CLASSNAME%::getParam('extends'), 'view' => %CLASSNAME%::getParam('view') ];
|
||||||
|
|
|
@ -79,6 +79,7 @@ class Compiler
|
||||||
public function registerSyntax(Syntax\Syntax $syntaxObject) : self
|
public function registerSyntax(Syntax\Syntax $syntaxObject) : self
|
||||||
{
|
{
|
||||||
$this->syntaxObjectList[get_class($syntaxObject)] = $syntaxObject;
|
$this->syntaxObjectList[get_class($syntaxObject)] = $syntaxObject;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -28,6 +28,8 @@ class Picea implements LanguageRegistration
|
||||||
|
|
||||||
public bool $debug;
|
public bool $debug;
|
||||||
|
|
||||||
|
public array $loadedTemplateFile = [];
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
? Closure $responseHtml = null,
|
? Closure $responseHtml = null,
|
||||||
? Compiler\Context $context = null,
|
? Compiler\Context $context = null,
|
||||||
|
@ -62,18 +64,54 @@ class Picea implements LanguageRegistration
|
||||||
}
|
}
|
||||||
catch(\Throwable $ex) {
|
catch(\Throwable $ex) {
|
||||||
# Temporary class for an experiment
|
# Temporary class for an experiment
|
||||||
throw new class($object, "An error occurred trying to render HTML view `$viewPath` : " . $ex->getMessage(), 911, $ex) extends \Exception {
|
throw new class($object, $this, "An error occurred trying to render HTML view `$viewPath` : " . $ex->getMessage(), 911, $ex) extends \Exception {
|
||||||
public function __construct(object $compiledObject, string $message, int $code, \Throwable $previous)
|
|
||||||
|
protected Picea $picea;
|
||||||
|
|
||||||
|
public function __construct(object $compiledObject, Picea $picea, string $message, int $code, \Throwable $previous)
|
||||||
{
|
{
|
||||||
parent::__construct($message, $code, $previous);
|
parent::__construct($message, $code, $previous);
|
||||||
|
|
||||||
$classContent = include( $previous->getFile() );
|
$this->picea = $picea;
|
||||||
|
|
||||||
if ( is_array($classContent) ) {
|
# $template = $this->getTemplateFile( $previous->getFile() );
|
||||||
$this->file = $classContent['view'];
|
$this->fromPrevious($previous, $compiledObject);
|
||||||
$this->line = $compiledObject->getSourceLineFromException($previous);
|
}
|
||||||
|
|
||||||
|
protected function fromPrevious(\Throwable $previous, object $compiledObject) : void
|
||||||
|
{
|
||||||
|
$loadedTemplates = array_flip($this->picea->loadedTemplateFile);
|
||||||
|
|
||||||
|
foreach($previous->getTrace() as $trace) {
|
||||||
|
|
||||||
|
if ( isset($trace['file'], $loadedTemplates[$trace['file']]) ) {
|
||||||
|
$class = $loadedTemplates[ $trace['file'] ];
|
||||||
|
$content = include($trace['file']);
|
||||||
|
|
||||||
|
$this->file = $content['view'];
|
||||||
|
$this->line = $compiledObject->getSourceLineFromException($trace['line']);
|
||||||
|
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#if ( $template ) {
|
||||||
|
# $this->redefineErrorState($classContent);
|
||||||
|
#}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getTemplateFile(string $filePath) : ? array
|
||||||
|
{
|
||||||
|
$content = null;
|
||||||
|
|
||||||
|
if ( is_array($content) && isset($content['classname'], $content['namespace'], $content['view'], $content['extends']) ) {
|
||||||
|
return $content;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -223,6 +261,11 @@ class Picea implements LanguageRegistration
|
||||||
$this->languageRegistration->registerExtension($compiler);
|
$this->languageRegistration->registerExtension($compiler);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function declareLoadedTemplate(string $className, string $filePath) : void
|
||||||
|
{
|
||||||
|
$this->loadedTemplateFile[$className] = $filePath;
|
||||||
|
}
|
||||||
|
|
||||||
protected function compileView(string $viewPath) : Compiler\Context
|
protected function compileView(string $viewPath) : Compiler\Context
|
||||||
{
|
{
|
||||||
$tmpFolder = sys_get_temp_dir();
|
$tmpFolder = sys_get_temp_dir();
|
||||||
|
@ -234,7 +277,6 @@ class Picea implements LanguageRegistration
|
||||||
$context = $builder->build($compiled['context'], $compiled['source']) ;
|
$context = $builder->build($compiled['context'], $compiled['source']) ;
|
||||||
$context->classPath = $tmpFolder . DIRECTORY_SEPARATOR . $context->className . ".php";
|
$context->classPath = $tmpFolder . DIRECTORY_SEPARATOR . $context->className . ".php";
|
||||||
|
|
||||||
|
|
||||||
if ( $context->extendFrom ) {
|
if ( $context->extendFrom ) {
|
||||||
$this->compileView($context->extendFrom);
|
$this->compileView($context->extendFrom);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue