147 lines
		
	
	
		
			4.3 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			147 lines
		
	
	
		
			4.3 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);
 | |
| 
 | |
|             ( 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);
 | |
| 
 | |
|             $__event->eventExecute(\Picea\Event\Builder\ClassTemplateOutputDone::class, $variablesList);
 | |
| 
 | |
|             %PARENT_OUTPUT%
 | |
| 
 | |
|             $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();
 | |
|         }
 | |
|     }
 | |
| 
 | |
| }
 | |
| 
 | |
| 
 | |
| return [ 'classname' => %CLASSNAME%::getParam('classname'), 'namespace' => %CLASSNAME%::getParam('namespace'), 'extends' => %CLASSNAME%::getParam('extends'), 'view' => %CLASSNAME%::getParam('view') ];
 |