openrat-cms

# OpenRat Content Management System
git clone http://git.code.weiherhei.de/openrat-cms.git
Log | Files | Refs

Component.class.php (4382B)


      1 <?php
      2 
      3 namespace template_engine\components;
      4 
      5 use cms\action\RequestParams;
      6 use modules\template_engine\Element;
      7 
      8 abstract class Component
      9 {
     10 
     11 	private $depth;
     12 
     13     /**
     14      * @var RequestParams
     15      */
     16     public $request;
     17 
     18 	/**
     19 	 * @var Element
     20 	 */
     21     private $element;
     22 
     23     public function __construct()
     24 	{
     25 		$this->element = $this->createElement();
     26 	}
     27 
     28 	public function getDepth()
     29 	{
     30 		return $this->depth;
     31 	}
     32 
     33 	public function setDepth($depth)
     34 	{
     35 		$this->depth = $depth;
     36 	}
     37 
     38 	public function createElement() {
     39     	return null;
     40 	}
     41 
     42 
     43 	/**
     44 	 * Gets the beginning of this component.
     45 	 * @return string
     46 	 */
     47 	public function getBegin()
     48 	{
     49 		if   ( $this->element )
     50 			return $this->element->getBegin();
     51 
     52 		ob_start();
     53 		$this->begin();
     54 		$src = ob_get_contents();
     55 		ob_end_clean();
     56 		return $src;
     57 	}
     58 
     59 	public function getEnd()
     60 	{
     61 		if   ( $this->element )
     62 			return $this->element->getEnd();
     63 
     64 		ob_start();
     65 		$this->end();
     66 		$src = ob_get_contents();
     67 		ob_end_clean();
     68 		return $src;
     69 	}
     70 
     71 	/**
     72 	 * Outputs the beginning of this component.
     73 	 */
     74 	protected function begin()
     75 	{}
     76 
     77 	protected function end()
     78 	{}
     79 	
     80 	
     81 	protected function textasvarname($value)
     82 	{
     83 		$expr = new Expression($value);
     84 		return $expr->getTextAsVarName();
     85 		
     86 	}
     87 	
     88 	
     89 	protected function varname($value)
     90 	{
     91 		$expr = new Expression($value);
     92 		return $expr->getVarName();
     93 	}
     94 	
     95 	
     96 	
     97 	protected function htmlvalue($value)
     98 	{
     99 		$expr = new Expression($value);
    100 		return $expr->getHTMLValue();
    101 	}
    102 	
    103 	
    104 	
    105 	protected function value( $value )
    106 	{
    107 		$expr = new Expression($value);
    108 		return $expr->getPHPValue();
    109 	}
    110 	
    111 
    112 	protected function includeResource($file )
    113 	{
    114 		echo "<?php include_once( 'modules/template-engine/components/html/".$file."') ?>";
    115 	}
    116 	
    117 }
    118 
    119 
    120 
    121 class Expression
    122 {
    123 	public $type;
    124 	public $value;
    125 	public $invert = false;
    126 	
    127 	public function __construct( $value )
    128 	{
    129 		// Falls der Wert 'true' oder 'false' ist.
    130 		if	( is_bool($value))
    131 			$value = $value?'true':'false';
    132 		
    133 		// Negierung berücksichtigen.
    134 		if	( substr($value,0,4)=='not:' )
    135 		{
    136 			$value = substr($value,4);
    137 			$this->invert = true;
    138 		}
    139 		
    140 		// Trennung 'type:value'
    141 		$parts = explode( ':', $value, 2 );
    142 		
    143 		if	( count($parts) < 2 )
    144 			$parts = array('',$value);
    145 			
    146 		list( $this->type,$this->value ) = $parts;
    147 		
    148 		// Fallback: Typ = 'text'.
    149 		if	( empty($this->type))
    150 			$this->type = 'text';
    151 		
    152 	}
    153 
    154 	
    155 	
    156 	public function getHTMLValue()
    157 	{
    158 		switch( $this->type )
    159 		{
    160 			case 'text':
    161 				return $this->value;
    162 				
    163 			default:
    164 				return '<'.'?php echo '.$this->getPHPValue().' ?>';
    165 		}
    166 	}
    167 	
    168 	
    169 	public function getVarName()
    170 	{
    171 		switch( $this->type )
    172 		{
    173 			case 'var':
    174 				return '$'.$this->value;
    175 			case 'text':
    176 				return $this->value;
    177 			default:
    178 				throw new \LogicException("Invalid expression type '$this->type' in attribute value. Allowed: text|var");
    179 		}
    180 	}
    181 	
    182 	
    183 	public function getTextAsVarName()
    184 	{
    185 		switch( $this->type )
    186 		{
    187 			case 'var':
    188 				return '$$'.$this->value;
    189 			case 'text':
    190 				return '$'.$this->value;
    191 			default:
    192 				return $this->getPHPValue();
    193 		}
    194 	}
    195 	
    196 	
    197 	public function getPHPValue()
    198 	{
    199 		$value = $this->value;
    200 		
    201 		$invert = $this->invert?'!':'';
    202 		
    203 		switch( $this->type )
    204 		{
    205 			case 'text':
    206 				// Sonderf�lle f�r die Attributwerte "true" und "false".
    207 				// Hinweis: Die Zeichenkette "false" entspricht in PHP true.
    208 				// Siehe http://de.php.net/manual/de/language.types.boolean.php
    209 				if	( $value == 'true' || $value == 'false' )
    210 					return $value;
    211 				else
    212 					return "'".$value."'";
    213 			case 'tpl':
    214 				// macht aus "text1{var}text2" => "text1".$var."text2"
    215 				$value = preg_replace('/{(\w+)\}/','\'.$\\1.\'',$value);
    216 				return "'".$value."'";
    217 			case 'var':
    218 				return $invert.'$'.$value;
    219 			case 'size':
    220 				return '@count($'.$value.')';
    221 			case 'message':
    222 				// macht aus "text1{var}text2" => "text1".$var."text2"
    223 				$value = preg_replace('/{(\w+)\}/','\'.$\\1.\'',$value);
    224 				return 'lang('."'".$value."'".')';
    225 			case 'arrayvar':
    226 				list($arr,$key) = explode(':',$value.':none');
    227 				return $invert.'@$'.$arr.'['.$key.']';
    228 			case 'config':
    229 				$config_parts = explode('/',$value);
    230 				return $invert.'config('."'".implode("'".','."'",$config_parts)."'".')';
    231 				
    232 			default:
    233 				throw new \LogicException("Unknown expression type '{$this->type}' in attribute value. Allowed: var|function|method|text|size|property|message|messagevar|arrayvar|config or none");
    234 		}
    235 	}
    236 	
    237 	
    238 }
    239 
    240 
    241 
    242 ?>