openrat-cms

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

commit b397bfed957fe052ceb103372ead6c3ad6933a47
parent 24b266078ca93f2e3afaa96b37df84e219cc7725
Author: Jan Dankert <develop@jandankert.de>
Date:   Sat, 22 Feb 2020 22:23:01 +0100

Refactoring: Enable Autoloading, Fix namespace structure.

Diffstat:
modules/template-engine/components/html/radiobox/component-radio-box.php | 41-----------------------------------------
modules/template_engine/ConditionalAttribute.php | 32--------------------------------
modules/template_engine/SimpleAttribute.php | 31-------------------------------
modules/template_engine/TemplateCompiler.php | 2+-
modules/template_engine/components/html/Component.class.php | 2+-
modules/template_engine/components/html/FieldComponent.class.php | 3++-
modules/template_engine/components/html/HtmlComponent.class.php | 4+++-
modules/template_engine/components/html/NativeHtmlComponent.class.php | 3++-
modules/template_engine/components/html/button/Button.class.php | 1+
modules/template_engine/components/html/checkbox/Checkbox.class.php | 1+
modules/template_engine/components/html/column/Column.class.php | 1+
modules/template_engine/components/html/date/Date.class.php | 1+
modules/template_engine/components/html/dummy/Dummy.class.php | 1+
modules/template_engine/components/html/editor/Editor.class.php | 1+
modules/template_engine/components/html/else/Else.class.php | 1+
modules/template_engine/components/html/focus/Focus.class.php | 1+
modules/template_engine/components/html/form/Form.class.php | 1+
modules/template_engine/components/html/group/Group.class.php | 1+
modules/template_engine/components/html/header/Header.class.php | 1+
modules/template_engine/components/html/hidden/Hidden.class.php | 1+
modules/template_engine/components/html/if/If.class.php | 1+
modules/template_engine/components/html/image/Image.class.php | 1+
modules/template_engine/components/html/include/Include.class.php | 2++
modules/template_engine/components/html/input/Input.class.php | 1+
modules/template_engine/components/html/inputarea/Inputarea.class.php | 1+
modules/template_engine/components/html/insert/Insert.class.php | 1+
modules/template_engine/components/html/label/Label.class.php | 1+
modules/template_engine/components/html/link/Link.class.php | 1+
modules/template_engine/components/html/list/List.class.php | 1+
modules/template_engine/components/html/logo/Logo.class.php | 1+
modules/template_engine/components/html/newline/Newline.class.php | 1+
modules/template_engine/components/html/output/Output.class.php | 1+
modules/template_engine/components/html/page/Page.class.php | 1+
modules/template_engine/components/html/part/Part.class.php | 1+
modules/template_engine/components/html/password/Password.class.php | 1+
modules/template_engine/components/html/qrcode/Qrcode.class.php | 1+
modules/template_engine/components/html/radio/Radio.class.php | 1+
modules/template_engine/components/html/radiobox/Radiobox.class.php | 1+
modules/template_engine/components/html/row/Row.class.php | 1+
modules/template_engine/components/html/selectbox/Selectbox.class.php | 1+
modules/template_engine/components/html/selector/Selector.class.php | 1+
modules/template_engine/components/html/set/Set.class.php | 1+
modules/template_engine/components/html/table/Table.class.php | 1+
modules/template_engine/components/html/text/Text.class.php | 1+
modules/template_engine/components/html/tree/Tree.class.php | 1+
modules/template_engine/components/html/upload/Upload.class.php | 1+
modules/template_engine/components/html/user/User.class.php | 1+
modules/template_engine/components/html/window/Window.class.php | 1+
modules/template_engine/element/CMSElement.class.php | 2+-
modules/template_engine/element/Element.class.php | 109+------------------------------------------------------------------------------
modules/template_engine/element/EmptyElement.class.php | 2+-
modules/template_engine/element/Value.class.php | 64++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
modules/template_engine/element/ValueExpression.class.php | 51+++++++++++++++++++++++++++++++++++++++++++++++++++
modules/template_engine/element/attribute/ConditionalAttribute.class.php | 32++++++++++++++++++++++++++++++++
modules/template_engine/element/attribute/SimpleAttribute.class.php | 31+++++++++++++++++++++++++++++++
modules/template_engine/engine/TemplateEngine.class.php | 15+++------------
modules/template_engine/require.php | 8--------
57 files changed, 234 insertions(+), 239 deletions(-)

diff --git a/modules/template-engine/components/html/radiobox/component-radio-box.php b/modules/template-engine/components/html/radiobox/component-radio-box.php @@ -1,40 +0,0 @@ -<?php - -/** - * - * @param unknown $name - * @param unknown $values - * @param unknown $value - */ -function component_radio_box($name, $values, $value) -{ - foreach ($values as $box_key => $box_value) - { - if (is_array($box_value) && isset($box_value['lang'])) - { - //$box_value = '<?php echo lang(\''.$box_value['lang'].'\') ? >'; - $box_value = lang($box_value['lang']); - $box_title = ''; - } - elseif (is_array($box_value)) - { - $box_key = $box_value['key']; - $box_title = $box_value['title']; - $box_value = $box_value['value']; - } - else - { - $box_title = ''; - } - - $id = REQUEST_ID.'_'.$name.'_'.$box_key; - echo '<input type="radio" id="'.$id.'" name="'.$name.'" value="' . $box_key . '" title="' . $box_title . '"'; - - if ((string) $box_key == $value) - echo ' checked="checked"'; - - echo ' />&nbsp;<label for="'.$id.'">'.$box_value.'</label><br />'; - } -} - -?>- \ No newline at end of file diff --git a/modules/template_engine/ConditionalAttribute.php b/modules/template_engine/ConditionalAttribute.php @@ -1,31 +0,0 @@ -<?php - - -namespace template_engine\components; - - -use cms\template_engine\SimpleAttribute; -use template_engine\element\Value; - -class ConditionalAttribute extends SimpleAttribute -{ - protected $condition; - - /** - * ConditionalAttribute constructor. - * @param $condition - * @param $name - * @param $value - */ - public function __construct($condition,$name,$value) - { - $this->condition = $condition; - parent::__construct($name,$value); - } - - - public function render() - { - return '<?php if('.$this->condition.'){ ?>'.parent::render().'<?php } ?>'; - } -}- \ No newline at end of file diff --git a/modules/template_engine/SimpleAttribute.php b/modules/template_engine/SimpleAttribute.php @@ -1,30 +0,0 @@ -<?php - - -namespace cms\template_engine; - - -use template_engine\element\Value; - -class SimpleAttribute -{ - - protected $name; - protected $value; - - /** - * SimpleAttribute constructor. - * @param $name - * @param $value - */ - public function __construct($name, $value) - { - $this->name = $name; - $this->value = $value; - } - - - public function render() { - return $this->name.'="'.(new Value($this->value))->render(Value::CONTEXT_HTML).'"'; - } -}- \ No newline at end of file diff --git a/modules/template_engine/TemplateCompiler.php b/modules/template_engine/TemplateCompiler.php @@ -11,7 +11,7 @@ ini_set('display_startup_errors', 1); require('../../modules/autoload.php'); -use template_engine\TemplateEngine; +use template_engine\engine\TemplateEngine; diff --git a/modules/template_engine/components/html/Component.class.php b/modules/template_engine/components/html/Component.class.php @@ -1,6 +1,6 @@ <?php -namespace template_engine\components; +namespace template_engine\components\html; use cms\action\RequestParams; use template_engine\element\Element; diff --git a/modules/template_engine/components/html/FieldComponent.class.php b/modules/template_engine/components/html/FieldComponent.class.php @@ -1,8 +1,9 @@ <?php -namespace template_engine\components; +namespace template_engine\components\html; +use template_engine\components\html\HtmlComponent; use template_engine\element\Element; abstract class FieldComponent extends HtmlComponent diff --git a/modules/template_engine/components/html/HtmlComponent.class.php b/modules/template_engine/components/html/HtmlComponent.class.php @@ -1,6 +1,8 @@ <?php -namespace template_engine\components; +namespace template_engine\components\html; + +use template_engine\components\html\Component; abstract class HtmlComponent extends Component { diff --git a/modules/template_engine/components/html/NativeHtmlComponent.class.php b/modules/template_engine/components/html/NativeHtmlComponent.class.php @@ -1,7 +1,8 @@ <?php -namespace template_engine\components; +namespace template_engine\components\html; +use template_engine\components\html\HtmlComponent; use template_engine\element\HtmlElement; class NativeHtmlComponent extends HtmlComponent diff --git a/modules/template_engine/components/html/button/Button.class.php b/modules/template_engine/components/html/button/Button.class.php @@ -2,6 +2,7 @@ namespace template_engine\components; +use template_engine\components\html\Component; use template_engine\element\Element; /* @DEPRECATED */ diff --git a/modules/template_engine/components/html/checkbox/Checkbox.class.php b/modules/template_engine/components/html/checkbox/Checkbox.class.php @@ -2,6 +2,7 @@ namespace template_engine\components; +use template_engine\components\html\Component; use template_engine\element\CMSElement; use template_engine\element\Value; use template_engine\element\ValueExpression; diff --git a/modules/template_engine/components/html/column/Column.class.php b/modules/template_engine/components/html/column/Column.class.php @@ -2,6 +2,7 @@ namespace template_engine\components; +use template_engine\components\html\Component; use template_engine\element\CMSElement; class ColumnComponent extends Component diff --git a/modules/template_engine/components/html/date/Date.class.php b/modules/template_engine/components/html/date/Date.class.php @@ -2,6 +2,7 @@ namespace template_engine\components; +use template_engine\components\html\Component; use template_engine\element\PHPBlockElement; class DateComponent extends Component diff --git a/modules/template_engine/components/html/dummy/Dummy.class.php b/modules/template_engine/components/html/dummy/Dummy.class.php @@ -2,6 +2,7 @@ namespace template_engine\components; +use template_engine\components\html\Component; use template_engine\element\Element; class DummyComponent extends Component diff --git a/modules/template_engine/components/html/editor/Editor.class.php b/modules/template_engine/components/html/editor/Editor.class.php @@ -2,6 +2,7 @@ namespace template_engine\components; +use template_engine\components\html\FieldComponent; use template_engine\element\CMSElement; use template_engine\element\Value; use template_engine\element\ValueExpression; diff --git a/modules/template_engine/components/html/else/Else.class.php b/modules/template_engine/components/html/else/Else.class.php @@ -2,6 +2,7 @@ namespace template_engine\components; +use template_engine\components\html\Component; use template_engine\element\PHPBlockElement; class ElseComponent extends Component diff --git a/modules/template_engine/components/html/focus/Focus.class.php b/modules/template_engine/components/html/focus/Focus.class.php @@ -2,6 +2,7 @@ namespace template_engine\components; +use template_engine\components\html\Component; use template_engine\element\Element; class FocusComponent extends Component diff --git a/modules/template_engine/components/html/form/Form.class.php b/modules/template_engine/components/html/form/Form.class.php @@ -2,6 +2,7 @@ namespace template_engine\components; +use template_engine\components\html\Component; use template_engine\element\CMSElement; use template_engine\element\HtmlElement; use template_engine\element\Value; diff --git a/modules/template_engine/components/html/group/Group.class.php b/modules/template_engine/components/html/group/Group.class.php @@ -2,6 +2,7 @@ namespace template_engine\components; +use template_engine\components\html\Component; use template_engine\element\CMSElement; use template_engine\element\HtmlElement; diff --git a/modules/template_engine/components/html/header/Header.class.php b/modules/template_engine/components/html/header/Header.class.php @@ -2,6 +2,7 @@ namespace template_engine\components; +use template_engine\components\html\Component; use template_engine\element\Element; class HeaderComponent extends Component diff --git a/modules/template_engine/components/html/hidden/Hidden.class.php b/modules/template_engine/components/html/hidden/Hidden.class.php @@ -2,6 +2,7 @@ namespace template_engine\components; +use template_engine\components\html\FieldComponent; use template_engine\element\CMSElement; use template_engine\element\Value; use template_engine\element\ValueExpression; diff --git a/modules/template_engine/components/html/if/If.class.php b/modules/template_engine/components/html/if/If.class.php @@ -2,6 +2,7 @@ namespace template_engine\components; +use template_engine\components\html\Component; use template_engine\element\PHPBlockElement; class IfComponent extends Component diff --git a/modules/template_engine/components/html/image/Image.class.php b/modules/template_engine/components/html/image/Image.class.php @@ -2,6 +2,7 @@ namespace template_engine\components; +use template_engine\components\html\Component; use template_engine\element\CMSElement; class ImageComponent extends Component diff --git a/modules/template_engine/components/html/include/Include.class.php b/modules/template_engine/components/html/include/Include.class.php @@ -2,6 +2,8 @@ namespace template_engine\components; +use template_engine\components\html\Component; + /** * Pseudo-Component. * The include component is a pseudo component. The template compiler will resolve this component first and is including another xml source file. diff --git a/modules/template_engine/components/html/input/Input.class.php b/modules/template_engine/components/html/input/Input.class.php @@ -2,6 +2,7 @@ namespace template_engine\components; +use template_engine\components\html\FieldComponent; use template_engine\element\CMSElement; use template_engine\element\HtmlElement; use template_engine\element\Value; diff --git a/modules/template_engine/components/html/inputarea/Inputarea.class.php b/modules/template_engine/components/html/inputarea/Inputarea.class.php @@ -2,6 +2,7 @@ namespace template_engine\components; +use template_engine\components\html\FieldComponent; use template_engine\element\CMSElement; use template_engine\element\Value; use template_engine\element\ValueExpression; diff --git a/modules/template_engine/components/html/insert/Insert.class.php b/modules/template_engine/components/html/insert/Insert.class.php @@ -2,6 +2,7 @@ namespace template_engine\components; +use template_engine\components\html\Component; use template_engine\element\CMSElement; class InsertComponent extends Component diff --git a/modules/template_engine/components/html/label/Label.class.php b/modules/template_engine/components/html/label/Label.class.php @@ -2,6 +2,7 @@ namespace template_engine\components; +use template_engine\components\html\Component; use template_engine\element\CMSElement; use template_engine\element\Value; use template_engine\element\ValueExpression; diff --git a/modules/template_engine/components/html/link/Link.class.php b/modules/template_engine/components/html/link/Link.class.php @@ -4,6 +4,7 @@ namespace template_engine\components; use Html; use JSON; +use template_engine\components\html\Component; use template_engine\element\CMSElement; /** diff --git a/modules/template_engine/components/html/list/List.class.php b/modules/template_engine/components/html/list/List.class.php @@ -2,6 +2,7 @@ namespace template_engine\components; +use template_engine\components\html\Component; use template_engine\element\PHPBlockElement; class ListComponent extends Component diff --git a/modules/template_engine/components/html/logo/Logo.class.php b/modules/template_engine/components/html/logo/Logo.class.php @@ -2,6 +2,7 @@ namespace template_engine\components; +use template_engine\components\html\Component; use template_engine\element\CMSElement;use template_engine\element\HtmlElement; use template_engine\element\Value; use template_engine\element\ValueExpression; diff --git a/modules/template_engine/components/html/newline/Newline.class.php b/modules/template_engine/components/html/newline/Newline.class.php @@ -2,6 +2,7 @@ namespace template_engine\components; +use template_engine\components\html\Component; use template_engine\element\Element; use template_engine\element\HtmlElement; diff --git a/modules/template_engine/components/html/output/Output.class.php b/modules/template_engine/components/html/output/Output.class.php @@ -2,6 +2,7 @@ namespace template_engine\components; +use template_engine\components\html\Component; use template_engine\element\Element; class OutputComponent extends Component diff --git a/modules/template_engine/components/html/page/Page.class.php b/modules/template_engine/components/html/page/Page.class.php @@ -2,6 +2,7 @@ namespace template_engine\components; +use template_engine\components\html\Component; use template_engine\element\Element; class PageComponent extends Component diff --git a/modules/template_engine/components/html/part/Part.class.php b/modules/template_engine/components/html/part/Part.class.php @@ -2,6 +2,7 @@ namespace template_engine\components; +use template_engine\components\html\Component; use template_engine\element\CMSElement; use template_engine\element\HtmlElement; diff --git a/modules/template_engine/components/html/password/Password.class.php b/modules/template_engine/components/html/password/Password.class.php @@ -2,6 +2,7 @@ namespace template_engine\components; +use template_engine\components\html\FieldComponent; use template_engine\element\CMSElement; use template_engine\element\HtmlElement; use template_engine\element\Value; diff --git a/modules/template_engine/components/html/qrcode/Qrcode.class.php b/modules/template_engine/components/html/qrcode/Qrcode.class.php @@ -2,6 +2,7 @@ namespace template_engine\components; +use template_engine\components\html\Component; use template_engine\element\CMSElement; use template_engine\element\Value; use template_engine\element\ValueExpression; diff --git a/modules/template_engine/components/html/radio/Radio.class.php b/modules/template_engine/components/html/radio/Radio.class.php @@ -2,6 +2,7 @@ namespace template_engine\components; +use template_engine\components\html\FieldComponent; use template_engine\element\CMSElement; use template_engine\element\Value; use template_engine\element\ValueExpression; diff --git a/modules/template_engine/components/html/radiobox/Radiobox.class.php b/modules/template_engine/components/html/radiobox/Radiobox.class.php @@ -2,6 +2,7 @@ namespace template_engine\components; +use template_engine\components\html\Component; use template_engine\element\CMSElement; use template_engine\element\PHPBlockElement; use template_engine\element\Value; diff --git a/modules/template_engine/components/html/row/Row.class.php b/modules/template_engine/components/html/row/Row.class.php @@ -2,6 +2,7 @@ namespace template_engine\components; +use template_engine\components\html\Component; use template_engine\element\CMSElement; class RowComponent extends Component diff --git a/modules/template_engine/components/html/selectbox/Selectbox.class.php b/modules/template_engine/components/html/selectbox/Selectbox.class.php @@ -2,6 +2,7 @@ namespace template_engine\components; +use template_engine\components\html\Component; use template_engine\element\CMSElement; use template_engine\element\PHPBlockElement; use template_engine\element\Value; diff --git a/modules/template_engine/components/html/selector/Selector.class.php b/modules/template_engine/components/html/selector/Selector.class.php @@ -2,6 +2,7 @@ namespace template_engine\components; +use template_engine\components\html\Component; use template_engine\element\CMSElement; class SelectorComponent extends Component diff --git a/modules/template_engine/components/html/set/Set.class.php b/modules/template_engine/components/html/set/Set.class.php @@ -2,6 +2,7 @@ namespace template_engine\components; +use template_engine\components\html\Component; use template_engine\element\PHPBlockElement; class SetComponent extends Component diff --git a/modules/template_engine/components/html/table/Table.class.php b/modules/template_engine/components/html/table/Table.class.php @@ -2,6 +2,7 @@ namespace template_engine\components; +use template_engine\components\html\HtmlComponent; use template_engine\element\CMSElement; use template_engine\element\HtmlElement; use template_engine\element\Value; diff --git a/modules/template_engine/components/html/text/Text.class.php b/modules/template_engine/components/html/text/Text.class.php @@ -2,6 +2,7 @@ namespace template_engine\components; +use template_engine\components\html\HtmlComponent; use template_engine\element\CMSElement;use template_engine\element\HtmlElement; use template_engine\element\Value; use template_engine\element\ValueExpression; diff --git a/modules/template_engine/components/html/tree/Tree.class.php b/modules/template_engine/components/html/tree/Tree.class.php @@ -2,6 +2,7 @@ namespace template_engine\components; +use template_engine\components\html\Component; use template_engine\element\CMSElement; class TreeComponent extends Component diff --git a/modules/template_engine/components/html/upload/Upload.class.php b/modules/template_engine/components/html/upload/Upload.class.php @@ -2,6 +2,7 @@ namespace template_engine\components; +use template_engine\components\html\Component; use template_engine\element\CMSElement; class UploadComponent extends Component diff --git a/modules/template_engine/components/html/user/User.class.php b/modules/template_engine/components/html/user/User.class.php @@ -2,6 +2,7 @@ namespace template_engine\components; +use template_engine\components\html\Component; use template_engine\element\PHPBlockElement; class UserComponent extends Component diff --git a/modules/template_engine/components/html/window/Window.class.php b/modules/template_engine/components/html/window/Window.class.php @@ -3,6 +3,7 @@ namespace template_engine\components; +use template_engine\components\html\Component; use template_engine\element\Element; class WindowComponent extends Component diff --git a/modules/template_engine/element/CMSElement.class.php b/modules/template_engine/element/CMSElement.class.php @@ -3,7 +3,7 @@ namespace template_engine\element; -use template_engine\components\ConditionalAttribute; +use template_engine\element\attribute\ConditionalAttribute; class CMSElement extends HtmlElement { diff --git a/modules/template_engine/element/Element.class.php b/modules/template_engine/element/Element.class.php @@ -4,7 +4,7 @@ namespace template_engine\element; -use cms\template_engine\SimpleAttribute; +use template_engine\element\attribute\SimpleAttribute; class Element { @@ -108,109 +108,3 @@ class Element } - - -class Value { - private $value; - private $expressions = []; - - const CONTEXT_PHP = 0; - const CONTEXT_HTML = 1; - const CONTEXT_RAW = 2; - - public static function createExpression( $type, $name ) { - return $type.'{'.$name.'}'; - } - - public function __construct( $value ) - { - while( true ) { - if ( ! $value ) - break; - - $epos = strpos($value,'{',1); - $fpos = strpos($value,'}',1); - - if ( $epos === false || $fpos === false ) - break; - - $type = substr($value,$epos-1,1 ); - $name = substr($value,$epos+1,$fpos-$epos-1); - - $this->expressions[] = new ValueExpression($type,$name,$epos-1); - $value = substr($value,0,$epos-1).substr($value,$fpos+1); - } - $this->value = $value; - } - - - public function render( $context ) { - switch( $context ) { - case Value::CONTEXT_PHP: - return "'".array_reduce(array_reverse($this->expressions),function($carry,$expr) { - return substr($carry, 0,$expr->position)."'.".$expr->render().'.\''.substr($carry,$expr->position); - },$this->value)."'"; - - case Value::CONTEXT_HTML: - case Value::CONTEXT_RAW: - $escape = function($expr) use($context) { - if ( $context == self::CONTEXT_HTML ) - return 'encodeHtml(htmlentities('.$expr.'))'; - else - return $expr; - }; - return array_reduce(array_reverse($this->expressions),function($carry,$expr) use ($escape) { - //echo "carry:".$carry.";expr:".$expr->position.':'.$expr->name; - return substr($carry, 0,$expr->position)."<?php echo ".$escape($expr->render()).' ?>'.substr($carry,$expr->position); - },$this->value); - } - } -} - - - -class ValueExpression { - - public $type; - public $name; - public $position; - - const TYPE_DATA_VAR = '$'; - const TYPE_MESSAGE = '#'; - const TYPE_CONFIG = '%'; - - /** - * ValueExpression constructor. - * @param $type - * @param $name - * @param $position - */ - public function __construct($type, $name, $position) - { - $this->type = $type; - $this->name = $name; - $this->position = $position; - } - - public function render() - { - switch( $this->type ) { - case self::TYPE_DATA_VAR: - $parts = explode('.', $this->name); - - return array_reduce($parts, function ($carry, $item) { - if (!$carry) - return '@$' . $item; - else - return $carry.'[\'' . $item . '\']'; - }, ''); - case self::TYPE_MESSAGE: - return '@lang(\'' . $this->name . '\')'; - - case self::TYPE_CONFIG: - $config_parts = explode('/', $this->name); - return 'config(' . "'" . implode("'" . ',' . "'", $config_parts) . "'" . ')'; - } - } - -}- \ No newline at end of file diff --git a/modules/template_engine/element/EmptyElement.class.php b/modules/template_engine/element/EmptyElement.class.php @@ -4,7 +4,7 @@ namespace template_engine\element; -use cms\template_engine\SimpleAttribute; +use template_engine\element\attribute\SimpleAttribute; /** diff --git a/modules/template_engine/element/Value.class.php b/modules/template_engine/element/Value.class.php @@ -0,0 +1,63 @@ +<?php + +namespace template_engine\element; + +class Value +{ + private $value; + private $expressions = []; + + const CONTEXT_PHP = 0; + const CONTEXT_HTML = 1; + const CONTEXT_RAW = 2; + + public static function createExpression($type, $name) + { + return $type . '{' . $name . '}'; + } + + public function __construct($value) + { + while (true) { + if (!$value) + break; + + $epos = strpos($value, '{', 1); + $fpos = strpos($value, '}', 1); + + if ($epos === false || $fpos === false) + break; + + $type = substr($value, $epos - 1, 1); + $name = substr($value, $epos + 1, $fpos - $epos - 1); + + $this->expressions[] = new ValueExpression($type, $name, $epos - 1); + $value = substr($value, 0, $epos - 1) . substr($value, $fpos + 1); + } + $this->value = $value; + } + + + public function render($context) + { + switch ($context) { + case Value::CONTEXT_PHP: + return "'" . array_reduce(array_reverse($this->expressions), function ($carry, $expr) { + return substr($carry, 0, $expr->position) . "'." . $expr->render() . '.\'' . substr($carry, $expr->position); + }, $this->value) . "'"; + + case Value::CONTEXT_HTML: + case Value::CONTEXT_RAW: + $escape = function ($expr) use ($context) { + if ($context == self::CONTEXT_HTML) + return 'encodeHtml(htmlentities(' . $expr . '))'; + else + return $expr; + }; + return array_reduce(array_reverse($this->expressions), function ($carry, $expr) use ($escape) { + //echo "carry:".$carry.";expr:".$expr->position.':'.$expr->name; + return substr($carry, 0, $expr->position) . "<?php echo " . $escape($expr->render()) . ' ?>' . substr($carry, $expr->position); + }, $this->value); + } + } +}+ \ No newline at end of file diff --git a/modules/template_engine/element/ValueExpression.class.php b/modules/template_engine/element/ValueExpression.class.php @@ -0,0 +1,50 @@ +<?php + +namespace template_engine\element; + +class ValueExpression +{ + + public $type; + public $name; + public $position; + + const TYPE_DATA_VAR = '$'; + const TYPE_MESSAGE = '#'; + const TYPE_CONFIG = '%'; + + /** + * ValueExpression constructor. + * @param $type + * @param $name + * @param $position + */ + public function __construct($type, $name, $position) + { + $this->type = $type; + $this->name = $name; + $this->position = $position; + } + + public function render() + { + switch ($this->type) { + case self::TYPE_DATA_VAR: + $parts = explode('.', $this->name); + + return array_reduce($parts, function ($carry, $item) { + if (!$carry) + return '@$' . $item; + else + return $carry . '[\'' . $item . '\']'; + }, ''); + case self::TYPE_MESSAGE: + return '@lang(\'' . $this->name . '\')'; + + case self::TYPE_CONFIG: + $config_parts = explode('/', $this->name); + return 'config(' . "'" . implode("'" . ',' . "'", $config_parts) . "'" . ')'; + } + } + +}+ \ No newline at end of file diff --git a/modules/template_engine/element/attribute/ConditionalAttribute.class.php b/modules/template_engine/element/attribute/ConditionalAttribute.class.php @@ -0,0 +1,31 @@ +<?php + + +namespace template_engine\element\attribute; + + +use template_engine\element\attribute\SimpleAttribute; +use template_engine\element\Value; + +class ConditionalAttribute extends SimpleAttribute +{ + protected $condition; + + /** + * ConditionalAttribute constructor. + * @param $condition + * @param $name + * @param $value + */ + public function __construct($condition,$name,$value) + { + $this->condition = $condition; + parent::__construct($name,$value); + } + + + public function render() + { + return '<?php if('.$this->condition.'){ ?>'.parent::render().'<?php } ?>'; + } +}+ \ No newline at end of file diff --git a/modules/template_engine/element/attribute/SimpleAttribute.class.php b/modules/template_engine/element/attribute/SimpleAttribute.class.php @@ -0,0 +1,30 @@ +<?php + + +namespace template_engine\element\attribute; + + +use template_engine\element\Value; + +class SimpleAttribute +{ + + protected $name; + protected $value; + + /** + * SimpleAttribute constructor. + * @param $name + * @param $value + */ + public function __construct($name, $value) + { + $this->name = $name; + $this->value = $value; + } + + + public function render() { + return $this->name.'="'.(new Value($this->value))->render(Value::CONTEXT_HTML).'"'; + } +}+ \ No newline at end of file diff --git a/modules/template_engine/engine/TemplateEngine.class.php b/modules/template_engine/engine/TemplateEngine.class.php @@ -1,18 +1,15 @@ <?php -namespace template_engine; +namespace template_engine\engine; use DomainException; use DOMDocument; use DOMElement; use Exception; use LogicException; -use template_engine\element\EmptyElement; -use template_engine\element\HtmlElement; use template_engine\element\PHPBlockElement; -use SimpleXMLElement; -use \template_engine\components\Component; -use template_engine\components\NativeHtmlComponent; +use template_engine\components\html\Component; +use template_engine\components\html\NativeHtmlComponent; /** * Wandelt eine Vorlage in ein PHP-Skript um. @@ -54,12 +51,6 @@ class TemplateEngine { $this->srcFilename = $srcXmlFilename; - // Imports the base class of all component types. - require_once (dirname(__FILE__).'/../components/'.$this->renderType.'/Component.class.' . PHP_EXT); - require_once (dirname(__FILE__).'/../components/'.$this->renderType.'/HtmlComponent.class.' . PHP_EXT); - require_once (dirname(__FILE__).'/../components/'.$this->renderType.'/NativeHtmlComponent.class.' . PHP_EXT); - require_once (dirname(__FILE__).'/../components/'.$this->renderType.'/FieldComponent.class.' . PHP_EXT); - // We are now building a complete DOM tree and this is the root element. $rootElement = new PHPBlockElement(); diff --git a/modules/template_engine/require.php b/modules/template_engine/require.php @@ -1,11 +1,3 @@ <?php include( dirname(__FILE__) . '/TemplateEngineInfo.class.php'); -include( dirname(__FILE__) . '/engine/TemplateEngine.class.php'); -//include( dirname(__FILE__) . '/Element.class.php'); -//include( dirname(__FILE__) . '/HtmlElement.class.php'); -//include( dirname(__FILE__) . '/CMSElement.class.php'); -//include( dirname(__FILE__) . '/EmptyElement.class.php'); -//include( dirname(__FILE__) . '/PHPBlockElement.class.php'); -include( dirname(__FILE__) . '/SimpleAttribute.php'); -include( dirname(__FILE__) . '/ConditionalAttribute.php');