- WIP on picea-asset AND error handling

This commit is contained in:
Dave M. 2023-10-12 19:07:16 +00:00
parent 3ccbf6bfab
commit d5de5e665b
7 changed files with 61 additions and 48 deletions

View File

@ -50,20 +50,21 @@ if (! class_exists("%NAMESPACE%\%CLASSNAME%", false) ) {
$__event->eventExecute(\Picea\Event\Builder\ClassTemplateOutputing::class, $variablesList); $__event->eventExecute(\Picea\Event\Builder\ClassTemplateOutputing::class, $variablesList);
( function($___class__template, $___global_variables, $___variables, $__event, $picea) { try {
extract($___global_variables); ( function($___class__template, $___global_variables, $___variables, $__event, $picea) {
extract($___variables, \EXTR_OVERWRITE); extract($___global_variables);
?>%CONTENT%<?php extract($___variables, \EXTR_OVERWRITE);
} )->call($this->thisProxy ?? new class() {}, $this, $this->variableList, $variablesList, $__event, $this->picea); ?>%CONTENT%<?php
} )->call($this->thisProxy ?? new class() {}, $this, $this->variableList, $variablesList, $__event, $this->picea);
} catch (\Throwable $error) {
throw $this->errorHandler($error);
}
$__event->eventExecute(\Picea\Event\Builder\ClassTemplateOutputDone::class, $variablesList); $__event->eventExecute(\Picea\Event\Builder\ClassTemplateOutputDone::class, $variablesList);
# try{ try { %PARENT_OUTPUT% } catch (\Throwable $error) {
%PARENT_OUTPUT% throw $this->errorHandler($error);
# } }
# catch(\Exception $e) {
# dump($this->isTemplateError($e));
# }
$this->depth--; $this->depth--;
@ -150,6 +151,25 @@ if (! class_exists("%NAMESPACE%\%CLASSNAME%", false) ) {
return substr($error->getFile(), -strlen($class)) === $class; return substr($error->getFile(), -strlen($class)) === $class;
} }
protected function errorHandler(\Throwable $error) : \Throwable
{
$class = substr(static::class, strrpos(static::class, '\\') + 1) . ".php";
#dump( __NAMESPACE__, basename($error->getFile()), $class );
$basename = basename($error->getFile());
#dump(is_subclass_of(sprintf("\\%s\\%s", __NAMESPACE__, substr($basename,0 , strrpos($basename, '.'))), static::class, true));
if (str_ends_with($error->getFile(), $class)) {
$error = new \Picea\Exception\RenderHtmlException($this, $error->getMessage(), $error->getCode(), $error);
}
#elseif (is_subclass_of(sprintf("\\%s\\%s", __NAMESPACE__, substr($basename,0 , strrpos($basename, '.'))), static::class, true)) {
# $error = new \Picea\Exception\RenderHtmlException($this, $error->getMessage(), $error->getCode(), $error);
#}
return $error;
}
} }
} }

View File

@ -63,9 +63,9 @@ class SectionToken implements ControlStructure {
$order = $options['order'] ?? "count(\$___class__template->sectionList[$name]['$action'])"; $order = $options['order'] ?? "count(\$___class__template->sectionList[$name]['$action'])";
return "<?php \$___class__template->sectionList[$name] ??= [ 'prepend' => [], 'append' => [], 'default' => [] ];" . return "<?php \$___class__template->sectionList[$name] ??= [ 'prepend' => [], 'append' => [], 'default' => [] ];" .
"\$___class__template->sectionList[$name]['$action'][] = [ "\$___class__template->sectionList[$name]['$action'][] = [ ".
'order' => $order, "'order' => $order, " .
'callback' => function() use (\$picea, \$___class__template, \$___global_variables, \$___variables, \$__event) {" . "'callback' => function() use (\$picea, \$___class__template, \$___global_variables, \$___variables, \$__event) {" .
"extract(\$___global_variables); extract(\$___variables, \EXTR_OVERWRITE);" . "extract(\$___global_variables); extract(\$___variables, \EXTR_OVERWRITE);" .
"\$___class__template->sectionStack[] = $name;" . "\$___class__template->sectionStack[] = $name;" .
"\$__event->eventExecute(\Picea\Event\Builder\ClassTemplateRenderSection::class, $name);?>"; "\$__event->eventExecute(\Picea\Event\Builder\ClassTemplateRenderSection::class, $name);?>";

View File

@ -4,26 +4,26 @@ namespace Picea;
trait EventTrait trait EventTrait
{ {
protected $_eventTraitMethod = "execute"; protected $eventTraitMethod = "execute";
public array $_eventList = []; public array $eventTraitList = [];
protected array $_returnList = []; protected array $eventTraitReturnList = [];
public function eventRegister(object $event) : void public function eventRegister(object $event) : void
{ {
$this->_eventList[] = $event; $this->eventTraitList[] = $event;
} }
public function eventFromType(string $type) : array public function eventFromType(string $type) : array
{ {
return array_filter($this->_eventList, fn($ev) => $ev instanceof $type); return array_filter($this->eventTraitList, fn($ev) => $ev instanceof $type);
} }
public function eventExecute(string $type, ...$arguments) : void public function eventExecute(string $type, ...$arguments) : void
{ {
foreach($this->eventFromType($type) as $event) { foreach($this->eventFromType($type) as $event) {
$this->_returnList[$event::class][] = call_user_func_array([ $event, $this->_eventTraitMethod ], $arguments); $this->eventTraitReturnList[$event::class][] = call_user_func_array([ $event, $this->eventTraitMethod ], $arguments);
} }
} }
} }

View File

@ -10,33 +10,20 @@ use Picea\Picea;
*/ */
class RenderHtmlException extends \Exception class RenderHtmlException extends \Exception
{ {
protected Picea $picea;
public function __construct(object $compiledObject, Picea $picea, string $message, int $code, \Throwable $previous) public function __construct(object $template, string $message, int $code, \Throwable $previous)
{ {
parent::__construct($message, $code, $previous); parent::__construct($message, $code, $previous);
$this->picea = $picea; $this->defineError($previous, $template);
$this->defineError($previous, $compiledObject);
} }
protected function defineError(\Throwable $previous, object $compiledObject) : void protected function defineError(\Throwable $previous, object $template) : void
{ {
$loadedTemplates = array_flip($this->picea->loadedTemplateFile); /*$loadedTemplates = array_flip($this->picea->loadedTemplateFile);*/
foreach($previous->getTrace() as $trace) { $this->file = $template::getParam('view');
if ( isset($trace['file'], $loadedTemplates[$trace['file']]) ) { $this->line = $template::getSourceLineFromException($previous->getLine());
$class = $loadedTemplates[ $trace['file'] ];
$content = include($trace['file']);
$this->file = $content['view'];
$this->line = $class::getSourceLineFromException($trace['line']);
return;
}
}
} }
protected function getTemplateFile(string $filePath) : ? array protected function getTemplateFile(string $filePath) : ? array

View File

@ -17,13 +17,10 @@ class TitleExtension implements Extension, FunctionExtension {
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
{ {
return <<<PHP return "<?php ".
<?php "if ( null !== \$title = title($arguments) ) {".
if ( null !== \$title = title($arguments) ) { "echo \$title;".
echo \$title; "} ?>";
}
?>
PHP;
} }
public function handleTitle(? string $set = null, ...$arguments) : ? string public function handleTitle(? string $set = null, ...$arguments) : ? string

View File

@ -5,9 +5,11 @@ namespace Picea\Extension;
use Notes\Route\Attribute\Object\Route; use Notes\Route\Attribute\Object\Route;
use Picea\EventTrait; use Picea\EventTrait;
use Picea\Compiler\Context; use Picea\Compiler\Context;
use Picea\Event\Extension\{ UrlBuildAssetEvent, UrlBuildUrlEvent, UrlBuildRouteEvent, UrlRegisterRouteEvent }; use Picea\Event\Extension\{ UrlBuildAssetEvent, UrlBuildUrlEvent, UrlBuildRouteEvent, UrlRegisterRouteEvent };
class UrlExtension implements Extension, FunctionExtension { class UrlExtension implements Extension, FunctionExtension {
use \Picea\EventTrait;
use EventTrait; use EventTrait;
@ -41,6 +43,13 @@ PATTERN;
$this->assetToken = $assetToken; $this->assetToken = $assetToken;
$this->appUrl = $appUrl; $this->appUrl = $appUrl;
$this->forceSSL = $forceSSL; $this->forceSSL = $forceSSL;
$this->eventRegister(new class() implements BuildAssetUrl {
public function execute(string $uri, array $parameters = [], bool $appendVersion = true) : mixed
{
}
});
} }
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string

View File

@ -75,11 +75,11 @@ class Picea implements LanguageRegistration
# throw new Exception\RenderHtmlException($object, $this, "An error occurred trying to render HTML view `$viewPath` : " . $ex->getMessage(), 911, $ex); # throw new Exception\RenderHtmlException($object, $this, "An error occurred trying to render HTML view `$viewPath` : " . $ex->getMessage(), 911, $ex);
#} #}
#else { #else {
throw $ex; throw $ex;
#} #}
} }
exit(); return null;
} }
/** /**