- 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);
try {
( function($___class__template, $___global_variables, $___variables, $__event, $picea) {
extract($___global_variables);
extract($___variables, \EXTR_OVERWRITE);
?>%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);
# try{
%PARENT_OUTPUT%
# }
# catch(\Exception $e) {
# dump($this->isTemplateError($e));
# }
try { %PARENT_OUTPUT% } catch (\Throwable $error) {
throw $this->errorHandler($error);
}
$this->depth--;
@ -150,6 +151,25 @@ if (! class_exists("%NAMESPACE%\%CLASSNAME%", false) ) {
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'])";
return "<?php \$___class__template->sectionList[$name] ??= [ 'prepend' => [], 'append' => [], 'default' => [] ];" .
"\$___class__template->sectionList[$name]['$action'][] = [
'order' => $order,
'callback' => function() use (\$picea, \$___class__template, \$___global_variables, \$___variables, \$__event) {" .
"\$___class__template->sectionList[$name]['$action'][] = [ ".
"'order' => $order, " .
"'callback' => function() use (\$picea, \$___class__template, \$___global_variables, \$___variables, \$__event) {" .
"extract(\$___global_variables); extract(\$___variables, \EXTR_OVERWRITE);" .
"\$___class__template->sectionStack[] = $name;" .
"\$__event->eventExecute(\Picea\Event\Builder\ClassTemplateRenderSection::class, $name);?>";

View File

@ -4,26 +4,26 @@ namespace Picea;
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
{
$this->_eventList[] = $event;
$this->eventTraitList[] = $event;
}
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
{
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
{
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);
$this->picea = $picea;
$this->defineError($previous, $compiledObject);
$this->defineError($previous, $template);
}
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) {
if ( isset($trace['file'], $loadedTemplates[$trace['file']]) ) {
$class = $loadedTemplates[ $trace['file'] ];
$content = include($trace['file']);
$this->file = $content['view'];
$this->line = $class::getSourceLineFromException($trace['line']);
return;
}
}
$this->file = $template::getParam('view');
$this->line = $template::getSourceLineFromException($previous->getLine());
}
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
{
return <<<PHP
<?php
if ( null !== \$title = title($arguments) ) {
echo \$title;
}
?>
PHP;
return "<?php ".
"if ( null !== \$title = title($arguments) ) {".
"echo \$title;".
"} ?>";
}
public function handleTitle(? string $set = null, ...$arguments) : ? string

View File

@ -5,9 +5,11 @@ namespace Picea\Extension;
use Notes\Route\Attribute\Object\Route;
use Picea\EventTrait;
use Picea\Compiler\Context;
use Picea\Event\Extension\{ UrlBuildAssetEvent, UrlBuildUrlEvent, UrlBuildRouteEvent, UrlRegisterRouteEvent };
class UrlExtension implements Extension, FunctionExtension {
use \Picea\EventTrait;
use EventTrait;
@ -41,6 +43,13 @@ PATTERN;
$this->assetToken = $assetToken;
$this->appUrl = $appUrl;
$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

View File

@ -79,7 +79,7 @@ class Picea implements LanguageRegistration
#}
}
exit();
return null;
}
/**