picea/src/Builder/ClassTemplate.php

90 lines
2.6 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 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;
}
public function output(array $variablesList = []) : void
{
( 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%
}
public function renderSection($name) : void
{
foreach([ 'prepend', 'default', 'append' ] as $item) {
usort($this->sectionList[$name][$item], fn($a, $b) => $a['order'] <=> $b['order']);
foreach($this->sectionList[$name][$item] as $section) {
$section['callback']();
if ( $item === 'default' ) {
break 1;
}
}
}
}
public function exportFunctions() : void
{
static $caching = [];
%FUNCTIONS%
}
public function getSourceLineFromException(\Throwable $ex) : ? int
{
$sourceFile = file_get_contents("%TEMPLATE%");
if ( $sourceFile ) {
foreach(explode(PHP_EOL, $sourceFile) as $line => $content) {
if ( strpos($content, str_replace('$', '%', '$CONTENT$')) !== false ) {
return $ex->getLine() - $line;
}
}
}
return null;
}
public function __invoke(array $variablesList = []) : string
{
ob_start();
$this->output($variablesList);
return ob_get_clean();
}
}
}
return [ 'classname' => "%CLASSNAME%", 'namespace' => "%NAMESPACE%", 'extends' => "%EXTENDS_TEMPLATE%", 'view' => "%FULLPATH%" ];