179 lines
5.7 KiB
PHP
179 lines
5.7 KiB
PHP
<?php
|
|
|
|
namespace %NAMESPACE%;
|
|
|
|
%USE%
|
|
|
|
# %PATHNAME%
|
|
if (! class_exists("%NAMESPACE%\%CLASSNAME%", false) ) {
|
|
|
|
class %CLASSNAME% %EXTENDS% {
|
|
|
|
public array $blockList = [];
|
|
|
|
public array $sectionList = [];
|
|
|
|
public array $sectionStack = [];
|
|
|
|
public array $variableList = [];
|
|
|
|
public ?object $thisProxy = null;
|
|
|
|
public \Picea\Picea $picea;
|
|
|
|
public static $context;
|
|
|
|
public bool $rendering = false;
|
|
|
|
public bool $renderingInsideSection = false;
|
|
|
|
public int $depth = 0;
|
|
|
|
public function __construct(\Picea\Picea $picea, array $variablesList = [], ?object $thisProxy = null) {
|
|
$this->picea = $picea;
|
|
$this->variableList = $variablesList;
|
|
$this->thisProxy = $thisProxy;
|
|
$this->exportFunctions();
|
|
|
|
static::$context = $picea->context;
|
|
|
|
$picea->declareLoadedTemplate(static::class, __FILE__);
|
|
}
|
|
|
|
public function output(array $variablesList = []) : void
|
|
{
|
|
$__event = new \Picea\Builder\ClassTemplateEvent();
|
|
|
|
$this->rendering = true;
|
|
|
|
$this->depth++;
|
|
|
|
$__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 (\Throwable $error) {
|
|
throw $this->errorHandler($error);
|
|
}
|
|
|
|
$this->depth--;
|
|
|
|
$this->rendering = false;
|
|
}
|
|
|
|
public function renderSection(string $name, bool $return = false) /*: string|void*/
|
|
{
|
|
$result = [];
|
|
|
|
foreach([ 'prepend', 'default', 'append' ] as $item) {
|
|
if ( !isset($this->sectionList[$name][$item]) || ! is_array($this->sectionList[$name][$item]) ) continue;
|
|
|
|
usort($this->sectionList[$name][$item], fn($a, $b) => $a['order'] <=> $b['order']);
|
|
|
|
foreach($this->sectionList[$name][$item] as $section) {
|
|
if ($return) {
|
|
ob_start();
|
|
}
|
|
|
|
$section['callback']();
|
|
|
|
if ($return) {
|
|
$result[] = ob_get_clean();
|
|
}
|
|
|
|
if ( $item === 'default' ) {
|
|
break 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
return $return ? implode("", $result) : "";
|
|
}
|
|
|
|
public function exportFunctions() : void
|
|
{
|
|
%FUNCTIONS%
|
|
}
|
|
|
|
public static function getSourceLineFromException(int $sourceLine) : ? int
|
|
{
|
|
$selfSource = file_get_contents(__FILE__);
|
|
|
|
foreach(explode("\n", $selfSource) as $line => $content) {
|
|
if ( strpos($content, str_replace('$', '%', '/*$EXCEPTION_LINE_BASE$*/')) !== false ) {
|
|
return $sourceLine - $line;
|
|
}
|
|
}
|
|
|
|
$sourceFile = file_get_contents("%TEMPLATE%");
|
|
|
|
if ( $sourceFile ) {
|
|
foreach(explode("\n", $sourceFile) as $line => $content) {
|
|
if ( strpos($content, str_replace('$', '%', '$CONTENT$')) !== false ) {
|
|
return $sourceLine - $line;
|
|
}
|
|
}
|
|
}
|
|
|
|
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
|
|
{
|
|
ob_start();
|
|
$this->output($variablesList);
|
|
return ob_get_clean();
|
|
}
|
|
|
|
public static function isTemplateError(\Throwable $error) : bool
|
|
{
|
|
$class = substr(static::class, strripos(static::class, '\\') + 1) . ".php";
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
return [ 'classname' => %CLASSNAME%::getParam('classname'), 'namespace' => %CLASSNAME%::getParam('namespace'), 'extends' => %CLASSNAME%::getParam('extends'), 'view' => %CLASSNAME%::getParam('view') ];
|