File modules/template_engine/components/html/Component.class.php

Last commit: Wed Nov 17 23:22:19 2021 +0100	Jan Dankert	Refactoring: Using a template context for templates instead of the HTTP-Request-Data
1 <?php 2 3 namespace template_engine\components\html; 4 5 use cms\action\RequestParams; 6 use template_engine\element\Element; 7 use template_engine\engine\TemplateContext; 8 9 /** 10 * Base class for all components. 11 * 12 * @package template_engine\components\html 13 */ 14 abstract class Component 15 { 16 /** 17 * Contains child components. 18 * 19 * @var array 20 */ 21 private $childComponents = []; 22 23 private $depth; 24 25 /** 26 * @param $component Component 27 */ 28 public function addChildComponent($component ) { 29 if ( $component ) 30 $this->childComponents[] = $component; 31 } 32 33 /** 34 * @var TemplateContext 35 */ 36 public $context; 37 38 /** 39 * @var Element 40 */ 41 private $element; 42 43 /** 44 * If a component is generating a tree of elements, then this element is the 45 * element, which is getting the elements, which sub-components are creating. 46 * Sublasses should set this element if necessary. 47 * 48 * @var Element 49 */ 50 protected $adoptiveElement; 51 52 public function __construct() 53 { 54 } 55 56 final public function getDepth() 57 { 58 return $this->depth; 59 } 60 61 final public function setDepth($depth) 62 { 63 $this->depth = $depth; 64 } 65 66 /** 67 * Every component must generate a single element or a tree of elements. 68 * Sublasses must overwrite this method. 69 * 70 * @return Element 71 */ 72 public abstract function createElement(); 73 74 final public function init() 75 { 76 $this->element = $this->createElement(); 77 78 // if there is no special adoptive element, lets use the root element. 79 if ( ! $this->adoptiveElement ) 80 $this->adoptiveElement = $this->element; 81 } 82 83 84 /** 85 * Gets the element with all child elements from all child components. 86 * 87 * @return Element 88 */ 89 final public function getElement() 90 { 91 /** @var Component $childComponent */ 92 foreach ($this->childComponents as $childComponent ) 93 $this->adoptiveElement->addChild( $childComponent->getElement() ); 94 95 return $this->element; 96 } 97 98 99 100 /** 101 * Splits a text at the comma char and trims the parts. 102 * 103 * @param $text 104 * @return array 105 */ 106 public static function splitByComma($text) 107 { 108 $parts = explode(',',$text); 109 return array_map( function($text) { 110 return trim($text); 111 },$parts); 112 } 113 114 115 }
Download modules/template_engine/components/html/Component.class.php
History Wed, 17 Nov 2021 23:22:19 +0100 Jan Dankert Refactoring: Using a template context for templates instead of the HTTP-Request-Data Wed, 21 Oct 2020 21:30:22 +0200 Jan Dankert Documentation Sun, 4 Oct 2020 23:53:25 +0200 Jan Dankert New: The tree is now hidable with a dedicated button. No more hover effect in the navigation. Sun, 23 Feb 2020 04:55:35 +0100 Jan Dankert New: Formatter for indenting XML files. Sat, 22 Feb 2020 22:23:01 +0100 Jan Dankert Refactoring: Enable Autoloading, Fix namespace structure. Sat, 22 Feb 2020 01:58:20 +0100 Jan Dankert New: Autoloading of classes in all modules. Sat, 22 Feb 2020 00:46:59 +0100 Jan Dankert Refactoring: Renaming template-engine to template_engine because '-' is no valid namespace char.