commit 67bb752e4e643b9fd30cc29d6c26e64df8914f27
parent b4e3b9288ce40cd4a298b36f9eb5daa394ed7bcc
Author: Jan Dankert <develop@jandankert.de>
Date: Sun, 23 Feb 2020 04:55:35 +0100
New: Formatter for indenting XML files.
Diffstat:
10 files changed, 76 insertions(+), 26 deletions(-)
diff --git a/modules/template_engine/components/html/Component.class.php b/modules/template_engine/components/html/Component.class.php
@@ -55,9 +55,6 @@ abstract class Component
{
$this->element = $this->createElement();
- if ( $this->element)
- $this->element->selfClosing(false);
-
// if there is no special adoptive element, lets use the root element.
if ( ! $this->adoptiveElement )
$this->adoptiveElement = $this->element;
diff --git a/modules/template_engine/components/html/image/Image.class.php b/modules/template_engine/components/html/image/Image.class.php
@@ -28,31 +28,26 @@ class ImageComponent extends Component
$styleClasses = [];
$tagName = 'img';
$file = '';
- $selfClosing = true;
if ( $this->menu )
{
$tagName = 'i';
$styleClasses = ['image-icon','image-icon--menu-'.$this->menu];
- $selfClosing = false;
}
elseif ( $this->elementtype )
{
$tagName = 'i';
$styleClasses = ['image-icon','image-icon--action-el_'.$this->elementtype];
- $selfClosing = false;
}
elseif ( $this->action )
{
$tagName = 'i';
$styleClasses = ['image-icon','image-icon--action-'.$this->action];
- $selfClosing = false;
}
elseif ( $this->method )
{
$tagName = 'i';
$styleClasses = ['image-icon','image-icon--method-'.$this->method];
- $selfClosing = false;
}
elseif ( $this->type )
{
@@ -100,8 +95,6 @@ class ImageComponent extends Component
if ( $file)
$image->addAttribute('src',$file);
- $image->selfClosing( $selfClosing );
-
return $image;
}
}
\ No newline at end of file
diff --git a/modules/template_engine/components/html/insert/Insert.class.php b/modules/template_engine/components/html/insert/Insert.class.php
@@ -26,7 +26,6 @@ class InsertComponent extends Component
{
// IFrame
$iframe = new CMSElement('iframe');
- $iframe->selfClosing(false);
if ( $this->name )
$iframe->addAttribute('name', $this->name);
if ( $this->url )
diff --git a/modules/template_engine/components/html/part/Part.class.php b/modules/template_engine/components/html/part/Part.class.php
@@ -13,7 +13,7 @@ class PartComponent extends Component
public function createElement()
{
- $element = (new CMSElement('div'))->selfClosing(false);
+ $element = (new CMSElement('div'));
foreach( explode(',',$this->class ) as $styleClass )
$element->addStyleClass( $styleClass );
diff --git a/modules/template_engine/components/html/upload/Upload.class.php b/modules/template_engine/components/html/upload/Upload.class.php
@@ -17,7 +17,6 @@ class UploadComponent extends Component
{
$input = new CMSElement('input');
- $input->selfClosing(true);
$input->addStyleClass( $this->class );
if ( $this->multiple )
diff --git a/modules/template_engine/element/Element.class.php b/modules/template_engine/element/Element.class.php
@@ -4,6 +4,7 @@
namespace template_engine\element;
+use template_engine\element\XMLFormatter;
use template_engine\element\attribute\SimpleAttribute;
class Element
@@ -56,11 +57,15 @@ class Element
return $this;
}
- public function render() {
+ /**
+ * @param $format XMLFormatter
+ * @return string
+ */
+ public function render($format ) {
$this->selfClosing = $this->selfClosing && !$this->content && !$this->children;
- $content = '';
+ $content = $format->getIndentation();
if ( $this->name )
$content .= '<'.$this->name.
@@ -68,13 +73,15 @@ class Element
$content .= $this->getContent();
- $content .= $this->renderChildren();
+ $content .= $this->renderChildren( $format );
if ( $this->selfClosing )
;
else
- if ( $this->name )
+ if ( $this->name ) {
+ $content .= $format->getIndentationOnClose();
$content .= '</'.$this->name.'>';
+ }
return $content;
}
@@ -94,13 +101,17 @@ class Element
return $this->content;
}
- protected function renderChildren()
+ /**
+ * @param $format XMLFormatter
+ * @return string
+ */
+ protected function renderChildren($format )
{
$content = '';
/** @var Element $child */
foreach($this->children as $child ) {
- $content .= $child->render();
+ $content .= $child->render( $format->deeper() );
}
return $content;
diff --git a/modules/template_engine/element/HtmlElement.class.php b/modules/template_engine/element/HtmlElement.class.php
@@ -53,12 +53,12 @@ class HtmlElement extends Element
}
- public function render()
+ public function render( $format )
{
if ( $this->styleClasses )
$this->addAttribute('class', implode(' ',$this->styleClasses) );
- return parent::render();
+ return parent::render( $format );
}
}
\ No newline at end of file
diff --git a/modules/template_engine/element/PHPBlockElement.class.php b/modules/template_engine/element/PHPBlockElement.class.php
@@ -4,6 +4,8 @@
namespace template_engine\element;
+use template_engine\element\XMLFormatter;
+
class PHPBlockElement extends HtmlElement
{
public $beforeBlock;
@@ -15,14 +17,19 @@ class PHPBlockElement extends HtmlElement
}
- public function render()
+ /**
+ * @param $format XMLFormatter
+ * @return string
+ */
+ public function render($format )
{
- $content = '';
+ $content = $format->getIndentation();
$content .= '<?php '.$this->beforeBlock.' { '.$this->inBlock.' ?>';
- $content .= $this->renderChildren();
+ $content .= $this->renderChildren( $format );
+ $content .= $format->getIndentationOnClose();
$content .= ' <?php } ?>';
return $content;
diff --git a/modules/template_engine/element/XMLFormatter.class.php b/modules/template_engine/element/XMLFormatter.class.php
@@ -0,0 +1,42 @@
+<?php
+
+
+namespace template_engine\element;
+
+
+class XMLFormatter
+{
+ private $depth;
+ private $indent;
+
+ /**
+ * XMLFormatter constructor.
+ * @param $indent
+ */
+ public function __construct($indent)
+ {
+ $this->depth = 0;
+ $this->indent = $indent;
+ }
+
+
+ public function deeper() {
+ $deeper = clone $this;
+ $deeper->depth+=1;
+ return $deeper;
+ }
+
+ public function getIndentation() {
+ if ( $this->depth && $this->indent )
+ return "\n".str_repeat($this->indent,$this->depth );
+ else
+ return '';
+ }
+
+ public function getIndentationOnClose() {
+ if ( $this->indent )
+ return "\n".str_repeat($this->indent,$this->depth );
+ else
+ return '';
+ }
+}+
\ 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,6 +7,7 @@ use DOMDocument;
use DOMElement;
use Exception;
use LogicException;
+use template_engine\element\XMLFormatter;
use template_engine\element\PHPBlockElement;
use template_engine\components\html\Component;
use template_engine\components\html\NativeHtmlComponent;
@@ -55,7 +56,7 @@ class TemplateEngine
$rootElement = new PHPBlockElement();
// The generated template should only be executable in our CMS environment (for security reasons).
- $rootElement->beforeBlock = 'if (!defined(\'OR_TITLE\')) die(\'Forbidden\');';
+ $rootElement->beforeBlock = 'if (defined(\'OR_TITLE\'))';
try
{
@@ -81,7 +82,7 @@ class TemplateEngine
if (is_file($filename) && ! is_writable($filename))
throw new LogicException("Template output file is read-only: $filename");
- $writtenBytes = file_put_contents( $filename, $rootElement->render() );
+ $writtenBytes = file_put_contents( $filename, $rootElement->render( new XMLFormatter(' ')) );
if ( $writtenBytes === FALSE )
throw new LogicException("Unable writing to output file: '$filename'");