openrat-cms

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

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

New: Autoloading of classes in all modules.

Diffstat:
api/index.php | 1+
index.php | 1+
modules/autoload.php | 22++++++++++++++++++++++
modules/cms-ui/UI.class.php | 2+-
modules/cms-ui/require.php | 2+-
modules/template_engine/CMSElement.class.php | 20--------------------
modules/template_engine/ConditionalAttribute.php | 2+-
modules/template_engine/Element.class.php | 217-------------------------------------------------------------------------------
modules/template_engine/EmptyElement.class.php | 23-----------------------
modules/template_engine/HtmlElement.class.php | 65-----------------------------------------------------------------
modules/template_engine/PHPBlockElement.class.php | 173-------------------------------------------------------------------------------
modules/template_engine/SimpleAttribute.php | 2+-
modules/template_engine/TemplateCompiler.php | 2++
modules/template_engine/components/html/Component.class.php | 2+-
modules/template_engine/components/html/FieldComponent.class.php | 2+-
modules/template_engine/components/html/NativeHtmlComponent.class.php | 2+-
modules/template_engine/components/html/button/Button.class.php | 2+-
modules/template_engine/components/html/checkbox/Checkbox.class.php | 6+++---
modules/template_engine/components/html/column/Column.class.php | 2+-
modules/template_engine/components/html/date/Date.class.php | 2+-
modules/template_engine/components/html/dummy/Dummy.class.php | 2+-
modules/template_engine/components/html/editor/Editor.class.php | 6+++---
modules/template_engine/components/html/else/Else.class.php | 2+-
modules/template_engine/components/html/focus/Focus.class.php | 2+-
modules/template_engine/components/html/form/Form.class.php | 8++++----
modules/template_engine/components/html/group/Group.class.php | 4++--
modules/template_engine/components/html/header/Header.class.php | 2+-
modules/template_engine/components/html/hidden/Hidden.class.php | 6+++---
modules/template_engine/components/html/if/If.class.php | 2+-
modules/template_engine/components/html/image/Image.class.php | 2+-
modules/template_engine/components/html/input/Input.class.php | 8++++----
modules/template_engine/components/html/inputarea/Inputarea.class.php | 6+++---
modules/template_engine/components/html/insert/Insert.class.php | 2+-
modules/template_engine/components/html/label/Label.class.php | 6+++---
modules/template_engine/components/html/link/Link.class.php | 2+-
modules/template_engine/components/html/list/List.class.php | 2+-
modules/template_engine/components/html/logo/Logo.class.php | 6+++---
modules/template_engine/components/html/newline/Newline.class.php | 4++--
modules/template_engine/components/html/output/Output.class.php | 2+-
modules/template_engine/components/html/page/Page.class.php | 2+-
modules/template_engine/components/html/part/Part.class.php | 4++--
modules/template_engine/components/html/password/Password.class.php | 8++++----
modules/template_engine/components/html/qrcode/Qrcode.class.php | 6+++---
modules/template_engine/components/html/radio/Radio.class.php | 6+++---
modules/template_engine/components/html/radiobox/Radiobox.class.php | 8++++----
modules/template_engine/components/html/row/Row.class.php | 2+-
modules/template_engine/components/html/selectbox/Selectbox.class.php | 8++++----
modules/template_engine/components/html/selector/Selector.class.php | 2+-
modules/template_engine/components/html/set/Set.class.php | 2+-
modules/template_engine/components/html/table/Table.class.php | 8++++----
modules/template_engine/components/html/text/Text.class.php | 6+++---
modules/template_engine/components/html/tree/Tree.class.php | 2+-
modules/template_engine/components/html/upload/Upload.class.php | 2+-
modules/template_engine/components/html/user/User.class.php | 2+-
modules/template_engine/components/html/window/Window.class.php | 2+-
modules/template_engine/element/CMSElement.class.php | 20++++++++++++++++++++
modules/template_engine/element/Element.class.php | 217+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
modules/template_engine/element/EmptyElement.class.php | 23+++++++++++++++++++++++
modules/template_engine/element/HtmlElement.class.php | 65+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
modules/template_engine/element/PHPBlockElement.class.php | 173+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
modules/template_engine/engine/TemplateEngine.class.php | 6+++---
modules/template_engine/require.php | 10+++++-----
62 files changed, 617 insertions(+), 591 deletions(-)

diff --git a/api/index.php b/api/index.php @@ -1,6 +1,7 @@ <?php // Excecuting the CMS application programming interface (API) +require('../modules/autoload.php'); require('../modules/cms-api/require.php'); use cms_api\API; diff --git a/index.php b/index.php @@ -1,5 +1,6 @@ <?php // Excecuting the CMS user interface (UI) +require('modules/autoload.php'); require('modules/cms-ui/require.php'); use cms_ui\UI; diff --git a/modules/autoload.php b/modules/autoload.php @@ -0,0 +1,21 @@ +<?php +// Enable class autoloading for all classes in all modules + +spl_autoload_register( + + /** + * Loads classes from modules. + * + * The PHP default loader is unusable, because it will always use lowercase file names. + * + * @param $className Class name + * @return void + */ + function ($className) { + + $c = __DIR__.DIRECTORY_SEPARATOR.str_replace( "\\", DIRECTORY_SEPARATOR, $className).'.class.php'; + + if ( is_file($c) ) + require($c); + } +);+ \ No newline at end of file diff --git a/modules/cms-ui/UI.class.php b/modules/cms-ui/UI.class.php @@ -245,7 +245,7 @@ class UI foreach (TemplateEngineInfo::getLESSFiles() as $lessFile) { - $css[] = OR_HTML_MODULES_DIR . 'template-engine/components/html/' . $lessFile . '/' . $lessFile; + $css[] = OR_HTML_MODULES_DIR . 'template_engine/components/html/' . $lessFile . '/' . $lessFile; } return $css; diff --git a/modules/cms-ui/require.php b/modules/cms-ui/require.php @@ -1,7 +1,7 @@ <?php require_once(__DIR__ . "/../cms-core/require.php"); -require_once(__DIR__ . "/../template-engine/require.php"); +require_once(__DIR__ . "/../template_engine/require.php"); require_once(__DIR__ . "/UI.class.php"); diff --git a/modules/template_engine/CMSElement.class.php b/modules/template_engine/CMSElement.class.php @@ -1,19 +0,0 @@ -<?php - - -namespace modules\template_engine; - -use template_engine\components\ConditionalAttribute; - -class CMSElement extends HtmlElement -{ - public function __construct( $name ) - { - parent::__construct( $name ); - } - - public function addConditionalAttribute($name, $condition, $value ) { - $this->attributes[] = new ConditionalAttribute($condition,$name,$value); - return $this; - } -}- \ No newline at end of file diff --git a/modules/template_engine/ConditionalAttribute.php b/modules/template_engine/ConditionalAttribute.php @@ -5,7 +5,7 @@ namespace template_engine\components; use cms\template_engine\SimpleAttribute; -use modules\template_engine\Value; +use template_engine\element\Value; class ConditionalAttribute extends SimpleAttribute { diff --git a/modules/template_engine/Element.class.php b/modules/template_engine/Element.class.php @@ -1,216 +0,0 @@ -<?php - - -namespace modules\template_engine; - - -use cms\template_engine\SimpleAttribute; - -class Element -{ - private $name; - /** - * @var array - */ - protected $attributes = []; - protected $content = ''; - - protected $selfClosing = false; - - /** - * @var array - */ - protected $children = []; - - - /** - * @param $element Element - */ - public function asChildOf($element ) { - $element->addChild($this); - return $this; - } - - public function addChild($child ) { - - if ( is_array( $child ) ) - $this->children += $child; - else - $this->children[] = $child; - - return $this; - } - - public function __construct( $name ) - { - $this->name = $name; - } - - protected function getAttributeValue($name ){ - return $this->attributes[$name]->render(); - } - - public function content( $content ) - { - $this->content = $content; - return $this; - } - - public function render() { - - $this->selfClosing = $this->selfClosing && !$this->content && !$this->children; - - $content = ''; - - if ( $this->name ) - $content .= '<'.$this->name. - array_reduce( array_keys($this->attributes),function($carry,$key){return $carry.' '.$this->getAttributeValue($key);},'').(($this->selfClosing ?' /':'').'>'); - - $content .= $this->getContent(); - - $content .= $this->renderChildren(); - - if ( $this->selfClosing ) - ; - else - if ( $this->name ) - $content .= '</'.$this->name.'>'; - - return $content; - } - - public function addAttribute($key, $value) { - $this->attributes[] = new SimpleAttribute($key,$value); - return $this; - } - - public function selfClosing($selfClosing) { - $this->selfClosing = boolval($selfClosing); - return $this; - } - - protected function getContent() - { - return $this->content; - } - - protected function renderChildren() - { - $content = ''; - - /** @var Element $child */ - foreach($this->children as $child ) { - $content .= $child->render(); - } - - return $content; - } -} - - - - -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/EmptyElement.class.php b/modules/template_engine/EmptyElement.class.php @@ -1,23 +0,0 @@ -<?php - - -namespace modules\template_engine; - - -use cms\template_engine\SimpleAttribute; - - -/** - * An empty element. - * - * 'empty' means, the element has no tag and no text content. But it may (and should) contain child elements. - * - * @package modules\template_engine - */ -class EmptyElement extends Element -{ - public function __construct() - { - parent::__construct(null); - } -} diff --git a/modules/template_engine/HtmlElement.class.php b/modules/template_engine/HtmlElement.class.php @@ -1,64 +0,0 @@ -<?php - - -namespace modules\template_engine; - - -class HtmlElement extends Element -{ - private $styleClasses = []; - - /** - * Auto-escaping the content. - * - * @var bool - */ - private $escape = true; - - - public function addStyleClass( $clazz ) { - $this->styleClasses[] = $clazz; - return $this; - } - - - public function setEscaping( $escape ) { - $this->escape = $escape; - return $this; - } - - - public function getAttributeValue($name) - { - return parent::getAttributeValue(htmlspecialchars($name)); - } - - - public function __construct( $name ) - { - parent::__construct( $name ); - - // Only "void elements" are self-closable, see - // https://html.spec.whatwg.org/multipage/syntax.html#void-elements - if ( in_array($name,['area','base','br','col','embed','hr','img','input','link','meta','param','source','track','wbr'])) - $this->selfClosing = true; - } - - - protected function getContent() - { - $context = $this->escape ? Value::CONTEXT_HTML : Value::CONTEXT_RAW; - - return (new Value(parent::getContent()))->render( $context ); - } - - - public function render() - { - if ( $this->styleClasses ) - $this->addAttribute('class', implode(' ',$this->styleClasses) ); - - return parent::render(); - } - -}- \ No newline at end of file diff --git a/modules/template_engine/PHPBlockElement.class.php b/modules/template_engine/PHPBlockElement.class.php @@ -1,172 +0,0 @@ -<?php - - -namespace modules\template_engine; - - -class PHPBlockElement extends HtmlElement -{ - public $beforeBlock; - public $inBlock; - - public function __construct() - { - parent::__construct( null ); - } - - - public function render() - { - $content = ''; - - $content .= '<?php '.$this->beforeBlock.' { '.$this->inBlock.' ?>'; - - $content .= $this->renderChildren(); - - $content .= ' <?php } ?>'; - - return $content; - } - - - - - - public function textasvarname($value) - { - $expr = new Expression($value); - return $expr->getTextAsVarName(); - - } - - - public function varname($value) - { - $expr = new Expression($value); - return $expr->getVarName(); - } - - - - public function htmlvalue($value) - { - $expr = new Expression($value); - return $expr->getHTMLValue(); - } - - - - public function value( $value ) - { - $expr = new Expression($value); - return $expr->getPHPValue(); - } - - - public function includeResource($file ) - { - return "include_once( 'modules/template_engine/components/html/".$file."');"; - } - -} - - - -class Expression -{ - public $type; - public $value; - public $invert = false; - - public function getHTMLValue() - { - switch ($this->type) { - case 'text': - return $this->value; - - default: - return '<' . '?php echo ' . $this->getPHPValue() . ' ?>'; - } - } - - - public function __construct($value) - { - // Falls der Wert 'true' oder 'false' ist. - if (is_bool($value)) - $value = $value ? 'true' : 'false'; - - // Negierung berücksichtigen. - if (substr($value, 0, 4) == 'not:') { - $value = substr($value, 4); - $this->invert = true; - } - - if ($value && strlen($value) >= 3 && $value[1] == '{') { - $this->type = $value[0]; - $this->value = substr($value, 2, -1); - } else { - $this->type = 'text'; - $this->value = $value; - } - } - - - public function getVarName() - { - switch ($this->type) { - case '$': - return '$' . $this->value; - case 'text': - return $this->value; - default: - throw new \LogicException("Invalid expression type '$this->type' in attribute value. Allowed: text|var"); - } - } - - - public function getTextAsVarName() - { - switch ($this->type) { - case '$': - return '$$' . $this->value; - case 'text': - return '$' . $this->value; - default: - return $this->getPHPValue(); - } - } - - - public function getPHPValue() - { - $value = $this->value; - - $invert = $this->invert ? '!' : ''; - - switch ($this->type) { - case 'text': - // Sonderf�lle f�r die Attributwerte "true" und "false". - // Hinweis: Die Zeichenkette "false" entspricht in PHP true. - // Siehe http://de.php.net/manual/de/language.types.boolean.php - if ($value == 'true' || $value == 'false') - return $value; - else - return "'" . $value . "'"; - case '$': - return $invert . '$' . $value; - case 'size': - return '@count($' . $value . ')'; - case '#': - // macht aus "text1{var}text2" => "text1".$var."text2" - $value = preg_replace('/{(\w+)\}/', '\'.$\\1.\'', $value); - return 'lang(' . "'" . $value . "'" . ')'; - case '%': - $config_parts = explode('/', $value); - return $invert . 'config(' . "'" . implode("'" . ',' . "'", $config_parts) . "'" . ')'; - - default: - throw new \LogicException("Unknown expression type '{$this->type}' in attribute value. Allowed: var|function|method|text|size|property|message|messagevar|arrayvar|config or none"); - } - } -}- \ No newline at end of file diff --git a/modules/template_engine/SimpleAttribute.php b/modules/template_engine/SimpleAttribute.php @@ -4,7 +4,7 @@ namespace cms\template_engine; -use modules\template_engine\Value; +use template_engine\element\Value; class SimpleAttribute { diff --git a/modules/template_engine/TemplateCompiler.php b/modules/template_engine/TemplateCompiler.php @@ -9,6 +9,8 @@ error_reporting(E_ALL); ini_set('display_errors', 1); ini_set('display_startup_errors', 1); +require('../../modules/autoload.php'); + use template_engine\TemplateEngine; diff --git a/modules/template_engine/components/html/Component.class.php b/modules/template_engine/components/html/Component.class.php @@ -3,7 +3,7 @@ namespace template_engine\components; use cms\action\RequestParams; -use modules\template_engine\Element; +use template_engine\element\Element; abstract class Component { diff --git a/modules/template_engine/components/html/FieldComponent.class.php b/modules/template_engine/components/html/FieldComponent.class.php @@ -3,7 +3,7 @@ namespace template_engine\components; -use modules\template_engine\Element; +use template_engine\element\Element; abstract class FieldComponent extends HtmlComponent { diff --git a/modules/template_engine/components/html/NativeHtmlComponent.class.php b/modules/template_engine/components/html/NativeHtmlComponent.class.php @@ -2,7 +2,7 @@ namespace template_engine\components; -use modules\template_engine\HtmlElement; +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,7 +2,7 @@ namespace template_engine\components; -use modules\template_engine\Element; +use template_engine\element\Element; /* @DEPRECATED */ class ButtonComponent extends Component diff --git a/modules/template_engine/components/html/checkbox/Checkbox.class.php b/modules/template_engine/components/html/checkbox/Checkbox.class.php @@ -2,9 +2,9 @@ namespace template_engine\components; -use modules\template_engine\CMSElement; -use modules\template_engine\Value; -use modules\template_engine\ValueExpression; +use template_engine\element\CMSElement; +use template_engine\element\Value; +use template_engine\element\ValueExpression; class CheckboxComponent extends Component { diff --git a/modules/template_engine/components/html/column/Column.class.php b/modules/template_engine/components/html/column/Column.class.php @@ -2,7 +2,7 @@ namespace template_engine\components; -use modules\template_engine\CMSElement; +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,7 +2,7 @@ namespace template_engine\components; -use modules\template_engine\PHPBlockElement; +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,7 +2,7 @@ namespace template_engine\components; -use modules\template_engine\Element; +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,9 +2,9 @@ namespace template_engine\components; -use modules\template_engine\CMSElement; -use modules\template_engine\Value; -use modules\template_engine\ValueExpression; +use template_engine\element\CMSElement; +use template_engine\element\Value; +use template_engine\element\ValueExpression; class EditorComponent extends FieldComponent { diff --git a/modules/template_engine/components/html/else/Else.class.php b/modules/template_engine/components/html/else/Else.class.php @@ -2,7 +2,7 @@ namespace template_engine\components; -use modules\template_engine\PHPBlockElement; +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,7 +2,7 @@ namespace template_engine\components; -use modules\template_engine\Element; +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,10 +2,10 @@ namespace template_engine\components; -use modules\template_engine\CMSElement; -use modules\template_engine\HtmlElement; -use modules\template_engine\Value; -use modules\template_engine\ValueExpression; +use template_engine\element\CMSElement; +use template_engine\element\HtmlElement; +use template_engine\element\Value; +use template_engine\element\ValueExpression; class FormComponent extends Component { diff --git a/modules/template_engine/components/html/group/Group.class.php b/modules/template_engine/components/html/group/Group.class.php @@ -2,8 +2,8 @@ namespace template_engine\components; -use modules\template_engine\CMSElement; -use modules\template_engine\HtmlElement; +use template_engine\element\CMSElement; +use template_engine\element\HtmlElement; /** * A group. diff --git a/modules/template_engine/components/html/header/Header.class.php b/modules/template_engine/components/html/header/Header.class.php @@ -2,7 +2,7 @@ namespace template_engine\components; -use modules\template_engine\Element; +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,9 +2,9 @@ namespace template_engine\components; -use modules\template_engine\CMSElement; -use modules\template_engine\Value; -use modules\template_engine\ValueExpression; +use template_engine\element\CMSElement; +use template_engine\element\Value; +use template_engine\element\ValueExpression; class HiddenComponent extends FieldComponent { diff --git a/modules/template_engine/components/html/if/If.class.php b/modules/template_engine/components/html/if/If.class.php @@ -2,7 +2,7 @@ namespace template_engine\components; -use modules\template_engine\PHPBlockElement; +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,7 +2,7 @@ namespace template_engine\components; -use modules\template_engine\CMSElement; +use template_engine\element\CMSElement; class ImageComponent extends Component { diff --git a/modules/template_engine/components/html/input/Input.class.php b/modules/template_engine/components/html/input/Input.class.php @@ -2,10 +2,10 @@ namespace template_engine\components; -use modules\template_engine\CMSElement; -use modules\template_engine\HtmlElement; -use modules\template_engine\Value; -use modules\template_engine\ValueExpression; +use template_engine\element\CMSElement; +use template_engine\element\HtmlElement; +use template_engine\element\Value; +use template_engine\element\ValueExpression; class InputComponent extends FieldComponent { diff --git a/modules/template_engine/components/html/inputarea/Inputarea.class.php b/modules/template_engine/components/html/inputarea/Inputarea.class.php @@ -2,9 +2,9 @@ namespace template_engine\components; -use modules\template_engine\CMSElement; -use modules\template_engine\Value; -use modules\template_engine\ValueExpression; +use template_engine\element\CMSElement; +use template_engine\element\Value; +use template_engine\element\ValueExpression; class InputareaComponent extends FieldComponent { diff --git a/modules/template_engine/components/html/insert/Insert.class.php b/modules/template_engine/components/html/insert/Insert.class.php @@ -2,7 +2,7 @@ namespace template_engine\components; -use modules\template_engine\CMSElement; +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,9 +2,9 @@ namespace template_engine\components; -use modules\template_engine\CMSElement; -use modules\template_engine\Value; -use modules\template_engine\ValueExpression; +use template_engine\element\CMSElement; +use template_engine\element\Value; +use template_engine\element\ValueExpression; class LabelComponent extends Component { diff --git a/modules/template_engine/components/html/link/Link.class.php b/modules/template_engine/components/html/link/Link.class.php @@ -4,7 +4,7 @@ namespace template_engine\components; use Html; use JSON; -use modules\template_engine\CMSElement; +use template_engine\element\CMSElement; /** * Erzeugt einen HTML-Link. diff --git a/modules/template_engine/components/html/list/List.class.php b/modules/template_engine/components/html/list/List.class.php @@ -2,7 +2,7 @@ namespace template_engine\components; -use modules\template_engine\PHPBlockElement; +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,9 +2,9 @@ namespace template_engine\components; -use modules\template_engine\CMSElement;use modules\template_engine\HtmlElement; -use modules\template_engine\Value; -use modules\template_engine\ValueExpression; +use template_engine\element\CMSElement;use template_engine\element\HtmlElement; +use template_engine\element\Value; +use template_engine\element\ValueExpression; class LogoComponent extends Component { diff --git a/modules/template_engine/components/html/newline/Newline.class.php b/modules/template_engine/components/html/newline/Newline.class.php @@ -2,8 +2,8 @@ namespace template_engine\components; -use modules\template_engine\Element; -use modules\template_engine\HtmlElement; +use template_engine\element\Element; +use template_engine\element\HtmlElement; /** * Newline-Component diff --git a/modules/template_engine/components/html/output/Output.class.php b/modules/template_engine/components/html/output/Output.class.php @@ -2,7 +2,7 @@ namespace template_engine\components; -use modules\template_engine\Element; +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,7 +2,7 @@ namespace template_engine\components; -use modules\template_engine\Element; +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,8 +2,8 @@ namespace template_engine\components; -use modules\template_engine\CMSElement; -use modules\template_engine\HtmlElement; +use template_engine\element\CMSElement; +use template_engine\element\HtmlElement; class PartComponent extends Component { diff --git a/modules/template_engine/components/html/password/Password.class.php b/modules/template_engine/components/html/password/Password.class.php @@ -2,10 +2,10 @@ namespace template_engine\components; -use modules\template_engine\CMSElement; -use modules\template_engine\HtmlElement; -use modules\template_engine\Value; -use modules\template_engine\ValueExpression; +use template_engine\element\CMSElement; +use template_engine\element\HtmlElement; +use template_engine\element\Value; +use template_engine\element\ValueExpression; class PasswordComponent extends FieldComponent { diff --git a/modules/template_engine/components/html/qrcode/Qrcode.class.php b/modules/template_engine/components/html/qrcode/Qrcode.class.php @@ -2,9 +2,9 @@ namespace template_engine\components; -use modules\template_engine\CMSElement; -use modules\template_engine\Value; -use modules\template_engine\ValueExpression; +use template_engine\element\CMSElement; +use template_engine\element\Value; +use template_engine\element\ValueExpression; class QrcodeComponent extends Component { diff --git a/modules/template_engine/components/html/radio/Radio.class.php b/modules/template_engine/components/html/radio/Radio.class.php @@ -2,9 +2,9 @@ namespace template_engine\components; -use modules\template_engine\CMSElement; -use modules\template_engine\Value; -use modules\template_engine\ValueExpression; +use template_engine\element\CMSElement; +use template_engine\element\Value; +use template_engine\element\ValueExpression; class RadioComponent extends FieldComponent { diff --git a/modules/template_engine/components/html/radiobox/Radiobox.class.php b/modules/template_engine/components/html/radiobox/Radiobox.class.php @@ -2,10 +2,10 @@ namespace template_engine\components; -use modules\template_engine\CMSElement; -use modules\template_engine\PHPBlockElement; -use modules\template_engine\Value; -use modules\template_engine\ValueExpression; +use template_engine\element\CMSElement; +use template_engine\element\PHPBlockElement; +use template_engine\element\Value; +use template_engine\element\ValueExpression; class RadioboxComponent extends Component { diff --git a/modules/template_engine/components/html/row/Row.class.php b/modules/template_engine/components/html/row/Row.class.php @@ -2,7 +2,7 @@ namespace template_engine\components; -use modules\template_engine\CMSElement; +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,10 +2,10 @@ namespace template_engine\components; -use modules\template_engine\CMSElement; -use modules\template_engine\PHPBlockElement; -use modules\template_engine\Value; -use modules\template_engine\ValueExpression; +use template_engine\element\CMSElement; +use template_engine\element\PHPBlockElement; +use template_engine\element\Value; +use template_engine\element\ValueExpression; class SelectboxComponent extends Component { diff --git a/modules/template_engine/components/html/selector/Selector.class.php b/modules/template_engine/components/html/selector/Selector.class.php @@ -2,7 +2,7 @@ namespace template_engine\components; -use modules\template_engine\CMSElement; +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,7 +2,7 @@ namespace template_engine\components; -use modules\template_engine\PHPBlockElement; +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,10 +2,10 @@ namespace template_engine\components; -use modules\template_engine\CMSElement; -use modules\template_engine\HtmlElement; -use modules\template_engine\Value; -use modules\template_engine\ValueExpression; +use template_engine\element\CMSElement; +use template_engine\element\HtmlElement; +use template_engine\element\Value; +use template_engine\element\ValueExpression; class TableComponent extends HtmlComponent { diff --git a/modules/template_engine/components/html/text/Text.class.php b/modules/template_engine/components/html/text/Text.class.php @@ -2,9 +2,9 @@ namespace template_engine\components; -use modules\template_engine\CMSElement;use modules\template_engine\HtmlElement; -use modules\template_engine\Value; -use modules\template_engine\ValueExpression; +use template_engine\element\CMSElement;use template_engine\element\HtmlElement; +use template_engine\element\Value; +use template_engine\element\ValueExpression; class TextComponent extends HtmlComponent { diff --git a/modules/template_engine/components/html/tree/Tree.class.php b/modules/template_engine/components/html/tree/Tree.class.php @@ -2,7 +2,7 @@ namespace template_engine\components; -use modules\template_engine\CMSElement; +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,7 +2,7 @@ namespace template_engine\components; -use modules\template_engine\CMSElement; +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,7 +2,7 @@ namespace template_engine\components; -use modules\template_engine\PHPBlockElement; +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,7 +3,7 @@ namespace template_engine\components; -use modules\template_engine\Element; +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 @@ -0,0 +1,19 @@ +<?php + + +namespace template_engine\element; + +use template_engine\components\ConditionalAttribute; + +class CMSElement extends HtmlElement +{ + public function __construct( $name ) + { + parent::__construct( $name ); + } + + public function addConditionalAttribute($name, $condition, $value ) { + $this->attributes[] = new ConditionalAttribute($condition,$name,$value); + return $this; + } +}+ \ No newline at end of file diff --git a/modules/template_engine/element/Element.class.php b/modules/template_engine/element/Element.class.php @@ -0,0 +1,216 @@ +<?php + + +namespace template_engine\element; + + +use cms\template_engine\SimpleAttribute; + +class Element +{ + private $name; + /** + * @var array + */ + protected $attributes = []; + protected $content = ''; + + protected $selfClosing = false; + + /** + * @var array + */ + protected $children = []; + + + /** + * @param $element Element + */ + public function asChildOf($element ) { + $element->addChild($this); + return $this; + } + + public function addChild($child ) { + + if ( is_array( $child ) ) + $this->children += $child; + else + $this->children[] = $child; + + return $this; + } + + public function __construct( $name ) + { + $this->name = $name; + } + + protected function getAttributeValue($name ){ + return $this->attributes[$name]->render(); + } + + public function content( $content ) + { + $this->content = $content; + return $this; + } + + public function render() { + + $this->selfClosing = $this->selfClosing && !$this->content && !$this->children; + + $content = ''; + + if ( $this->name ) + $content .= '<'.$this->name. + array_reduce( array_keys($this->attributes),function($carry,$key){return $carry.' '.$this->getAttributeValue($key);},'').(($this->selfClosing ?' /':'').'>'); + + $content .= $this->getContent(); + + $content .= $this->renderChildren(); + + if ( $this->selfClosing ) + ; + else + if ( $this->name ) + $content .= '</'.$this->name.'>'; + + return $content; + } + + public function addAttribute($key, $value) { + $this->attributes[] = new SimpleAttribute($key,$value); + return $this; + } + + public function selfClosing($selfClosing) { + $this->selfClosing = boolval($selfClosing); + return $this; + } + + protected function getContent() + { + return $this->content; + } + + protected function renderChildren() + { + $content = ''; + + /** @var Element $child */ + foreach($this->children as $child ) { + $content .= $child->render(); + } + + return $content; + } +} + + + + +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 @@ -0,0 +1,23 @@ +<?php + + +namespace template_engine\element; + + +use cms\template_engine\SimpleAttribute; + + +/** + * An empty element. + * + * 'empty' means, the element has no tag and no text content. But it may (and should) contain child elements. + * + * @package modules\template_engine + */ +class EmptyElement extends Element +{ + public function __construct() + { + parent::__construct(null); + } +} diff --git a/modules/template_engine/element/HtmlElement.class.php b/modules/template_engine/element/HtmlElement.class.php @@ -0,0 +1,64 @@ +<?php + + +namespace template_engine\element; + + +class HtmlElement extends Element +{ + private $styleClasses = []; + + /** + * Auto-escaping the content. + * + * @var bool + */ + private $escape = true; + + + public function addStyleClass( $clazz ) { + $this->styleClasses[] = $clazz; + return $this; + } + + + public function setEscaping( $escape ) { + $this->escape = $escape; + return $this; + } + + + public function getAttributeValue($name) + { + return parent::getAttributeValue(htmlspecialchars($name)); + } + + + public function __construct( $name ) + { + parent::__construct( $name ); + + // Only "void elements" are self-closable, see + // https://html.spec.whatwg.org/multipage/syntax.html#void-elements + if ( in_array($name,['area','base','br','col','embed','hr','img','input','link','meta','param','source','track','wbr'])) + $this->selfClosing = true; + } + + + protected function getContent() + { + $context = $this->escape ? Value::CONTEXT_HTML : Value::CONTEXT_RAW; + + return (new Value(parent::getContent()))->render( $context ); + } + + + public function render() + { + if ( $this->styleClasses ) + $this->addAttribute('class', implode(' ',$this->styleClasses) ); + + return parent::render(); + } + +}+ \ No newline at end of file diff --git a/modules/template_engine/element/PHPBlockElement.class.php b/modules/template_engine/element/PHPBlockElement.class.php @@ -0,0 +1,172 @@ +<?php + + +namespace template_engine\element; + + +class PHPBlockElement extends HtmlElement +{ + public $beforeBlock; + public $inBlock; + + public function __construct() + { + parent::__construct( null ); + } + + + public function render() + { + $content = ''; + + $content .= '<?php '.$this->beforeBlock.' { '.$this->inBlock.' ?>'; + + $content .= $this->renderChildren(); + + $content .= ' <?php } ?>'; + + return $content; + } + + + + + + public function textasvarname($value) + { + $expr = new Expression($value); + return $expr->getTextAsVarName(); + + } + + + public function varname($value) + { + $expr = new Expression($value); + return $expr->getVarName(); + } + + + + public function htmlvalue($value) + { + $expr = new Expression($value); + return $expr->getHTMLValue(); + } + + + + public function value( $value ) + { + $expr = new Expression($value); + return $expr->getPHPValue(); + } + + + public function includeResource($file ) + { + return "include_once( 'modules/template_engine/components/html/".$file."');"; + } + +} + + + +class Expression +{ + public $type; + public $value; + public $invert = false; + + public function getHTMLValue() + { + switch ($this->type) { + case 'text': + return $this->value; + + default: + return '<' . '?php echo ' . $this->getPHPValue() . ' ?>'; + } + } + + + public function __construct($value) + { + // Falls der Wert 'true' oder 'false' ist. + if (is_bool($value)) + $value = $value ? 'true' : 'false'; + + // Negierung berücksichtigen. + if (substr($value, 0, 4) == 'not:') { + $value = substr($value, 4); + $this->invert = true; + } + + if ($value && strlen($value) >= 3 && $value[1] == '{') { + $this->type = $value[0]; + $this->value = substr($value, 2, -1); + } else { + $this->type = 'text'; + $this->value = $value; + } + } + + + public function getVarName() + { + switch ($this->type) { + case '$': + return '$' . $this->value; + case 'text': + return $this->value; + default: + throw new \LogicException("Invalid expression type '$this->type' in attribute value. Allowed: text|var"); + } + } + + + public function getTextAsVarName() + { + switch ($this->type) { + case '$': + return '$$' . $this->value; + case 'text': + return '$' . $this->value; + default: + return $this->getPHPValue(); + } + } + + + public function getPHPValue() + { + $value = $this->value; + + $invert = $this->invert ? '!' : ''; + + switch ($this->type) { + case 'text': + // Sonderf�lle f�r die Attributwerte "true" und "false". + // Hinweis: Die Zeichenkette "false" entspricht in PHP true. + // Siehe http://de.php.net/manual/de/language.types.boolean.php + if ($value == 'true' || $value == 'false') + return $value; + else + return "'" . $value . "'"; + case '$': + return $invert . '$' . $value; + case 'size': + return '@count($' . $value . ')'; + case '#': + // macht aus "text1{var}text2" => "text1".$var."text2" + $value = preg_replace('/{(\w+)\}/', '\'.$\\1.\'', $value); + return 'lang(' . "'" . $value . "'" . ')'; + case '%': + $config_parts = explode('/', $value); + return $invert . 'config(' . "'" . implode("'" . ',' . "'", $config_parts) . "'" . ')'; + + default: + throw new \LogicException("Unknown expression type '{$this->type}' in attribute value. Allowed: var|function|method|text|size|property|message|messagevar|arrayvar|config or none"); + } + } +}+ \ No newline at end of file diff --git a/modules/template_engine/engine/TemplateEngine.class.php b/modules/template_engine/engine/TemplateEngine.class.php @@ -7,9 +7,9 @@ use DOMDocument; use DOMElement; use Exception; use LogicException; -use modules\template_engine\EmptyElement; -use modules\template_engine\HtmlElement; -use modules\template_engine\PHPBlockElement; +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; diff --git a/modules/template_engine/require.php b/modules/template_engine/require.php @@ -2,10 +2,10 @@ 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__) . '/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');