131 lines
3.9 KiB
PHP
131 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace %NAMESPACE%;
|
|
|
|
%USE%
|
|
|
|
# %PATHNAME%
|
|
if (! class_exists("%NAMESPACE%\%CLASSNAME%", false) ) {
|
|
|
|
class %CLASSNAME% %EXTENDS% {
|
|
public array $blockList = [];
|
|
|
|
public array $sectionList = [];
|
|
|
|
public array $variableList = [];
|
|
|
|
public ?object $thisProxy = null;
|
|
|
|
public \Picea\Picea $picea;
|
|
|
|
public static $context;
|
|
|
|
public bool $rendering = false;
|
|
|
|
public bool $renderingInsideSection = false;
|
|
|
|
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
|
|
{
|
|
$this->rendering = true;
|
|
|
|
( function($___class__template, $___global_variables, $___variables, $picea) {
|
|
extract($___global_variables);
|
|
extract($___variables, \EXTR_OVERWRITE);
|
|
?>%CONTENT%<?php
|
|
} )->call($this->thisProxy ?? new class(){}, $this, $this->variableList, $variablesList, $this->picea);
|
|
%PARENT_OUTPUT%
|
|
|
|
$this->rendering = false;
|
|
}
|
|
|
|
public function renderSection(string $name, bool $return = false) /*: string|void*/
|
|
{
|
|
$result = [];
|
|
|
|
foreach([ 'prepend', 'default', 'append' ] as $item) {
|
|
if ( ! 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();
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
return [ 'classname' => %CLASSNAME%::getParam('classname'), 'namespace' => %CLASSNAME%::getParam('namespace'), 'extends' => %CLASSNAME%::getParam('extends'), 'view' => %CLASSNAME%::getParam('view') ];
|