commit 3d3511d7412555e9fe07d5a4c49f968bdd018a2a
parent 53362f6746ddcc1b35506223193d42caeb8a8eb0
Author: Jan Dankert <develop@jandankert.de>
Date: Thu, 23 Jan 2020 00:50:55 +0100
Refactoring: Switching to PHPs newer SimpleXML-extension (pre-work for using XML namespaces)
Diffstat:
4 files changed, 87 insertions(+), 125 deletions(-)
diff --git a/modules/cms-ui/themes/default/html/views/include/timestamps.inc.xml b/modules/cms-ui/themes/default/html/views/include/timestamps.inc.xml
@@ -4,7 +4,7 @@
<part class="line">
<part class="label">
<label for="create_date">
- <text text="global_created"></text>
+ <text text="global_created"/>
</label>
</part>
<part class="input">
diff --git a/modules/template-engine/engine/TemplateEngine.class.php b/modules/template-engine/engine/TemplateEngine.class.php
@@ -5,6 +5,7 @@ namespace template_engine;
use DomainException;
use Exception;
use LogicException;
+use SimpleXMLElement;
use \template_engine\components\Component;
/**
@@ -22,6 +23,10 @@ class TemplateEngine
public $config = array();
public $request;
+ private $srcFilename;
+ private $debug = false;
+
+
/**
* Erzeugt einen Templateparser.
*/
@@ -38,6 +43,8 @@ class TemplateEngine
*/
public function compile($srcXmlFilename, $tplOutName )
{
+ $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);
@@ -71,78 +78,8 @@ class TemplateEngine
if (! is_resource($outFile))
throw new \LogicException("Template '$srcXmlFilename': Unable to open file for writing: '$filename'");
- $openCmd = array();
- $depth = 0;
- $components = array();
- $counter = 0;
+ $this->processElement( $document, $outFile );
- $document = $this->parseIncludes( $document, dirname($srcXmlFilename) );
-
- foreach ($document as $element)
- {
- // Initialisieren der m�glichen Element-Inhalte
- $type = '';
- $attributes = array();
- $value = '';
- $tag = '';
- $counter++;
-
- // Setzt: $tag, $attributes, $value, $type
- extract($element);
-
- if ( in_array($type, array('open','complete') ) )
- {
- $depth ++;
-
- $className = ucfirst($tag);
- $classFilename = dirname(__FILE__).'/../components/'.$this->renderType."/$tag/$className.class." . PHP_EXT;
-
- if (!is_file($classFilename))
- throw new \LogicException("Component Class File '$classFilename' does not exist." );
-
- require_once ($classFilename);
-
- $className = 'template_engine\components\\'.$className .'Component';
- /* @var $component Component */
- $component = new $className();
- $component->setDepth($depth);
- $component->request = $this->request;
-
- foreach ($attributes as $prop => $value)
- {
- // Aus String 'true' und 'false' typechtes Boolean machen.
- // Sonst wäre 'false'==true!
- if ($value == 'false') $value = false;
- if ($value == 'true') $value = true;
-
- $component->$prop = $value;
- }
- // $component->depth = $depth;
-
- $components[$depth] = $component;
-
- $output = $component->getBegin();
- if ($output) {
- $prepend = ($counter>1?"\n":'').str_repeat("\t",$depth-1);
- fwrite($outFile, $prepend.$output);
- }
- }
-
- if ( in_array($type, array('close','complete') ) )
- {
- $component = $components[$depth];
-
- $output = $component->getEnd();
- if ( $output ) {
- $prepend = "\n".str_repeat("\t",$depth-1);
- fwrite($outFile, $prepend.$component->getEnd());
- }
- unset($components[$depth]); // Cleanup
-
- $depth --;
- }
- }
-
fclose($outFile);
// CHMOD ausfuehren.
@@ -152,11 +89,77 @@ class TemplateEngine
}
catch (\Exception $e)
{
+ echo $e->getTraceAsString();
throw new \LogicException("Template '$srcXmlFilename' failed to compile", 0, $e);
}
}
-
+
+ /**
+ * @param SimpleXMLElement $element
+ * @param resource $outFile
+ * @param int $depth
+ */
+ private function processElement( $element, $outFile, $depth = 0 ) {
+
+ $attributes = iterator_to_array($element->attributes());
+ $tag = $element->getName();
+
+ if ( $tag == 'include') {
+
+ $element = $this->loadDocument(dirname($this->srcFilename ) . '/' . $attributes['file'] . '.inc.xml');
+ $attributes = iterator_to_array( $element->attributes() );
+ $tag = $element->getName();
+ }
+
+ $className = ucfirst($tag);
+ $classFilename = dirname(__FILE__).'/../components/'.$this->renderType."/$tag/$className.class." . PHP_EXT;
+
+ if (!is_file($classFilename))
+ throw new \LogicException("Component Class File '$classFilename' does not exist." );
+
+ require_once ($classFilename);
+
+ $className = 'template_engine\components\\'.$className .'Component';
+ /* @var $component Component */
+ $component = new $className();
+ $component->setDepth($depth);
+ $component->request = $this->request;
+
+ foreach ($attributes as $attributeName => $attributeValue)
+ {
+ settype($attributeValue,'string');
+ // Aus String 'true' und 'false' typechtes Boolean machen.
+ // Sonst wäre 'false'==true!
+ if ($attributeValue == 'false') $attributeValue = false;
+ if ($attributeValue == 'true') $attributeValue = true;
+
+ $component->$attributeName = $attributeValue;
+ }
+
+ $output = $component->getBegin();
+ if($this->debug) $output = '<!-- Begin '.$tag.' -->'.$output;
+
+ if ($output) {
+ $prepend = ($depth>=0?"\n":'').str_repeat("\t",$depth);
+ fwrite($outFile, $prepend.$output);
+ }
+
+ foreach( $element->children() as $child ) {
+ $this->processElement($child,$outFile,$depth+1);
+ }
+
+ $output = $component->getEnd();
+ if($this->debug) $output = $output.'<!-- End '.$tag.' -->';
+ if ( $output ) {
+ $prepend = "\n".str_repeat("\t",$depth);
+ fwrite($outFile, $prepend.$output);
+ }
+
+ }
+
+
+
/**
* Diese Funktion lädt die Vorlagedatei.
*/
@@ -172,17 +175,9 @@ class TemplateEngine
private function loadXmlDocument( $filename )
{
if (!is_file($filename))
- throw new LogicException("File '$filename' was not found.'");
-
- $index = array();
- $vals = array();
- $p = xml_parser_create();
- xml_parser_set_option ( $p, XML_OPTION_CASE_FOLDING,false );
- xml_parser_set_option ( $p, XML_OPTION_SKIP_WHITE,false );
- xml_parse_into_struct($p, implode('',file($filename)), $vals, $index);
- xml_parser_free($p);
-
- return $vals;
+ throw new LogicException("XML file '$filename' was not found.'");
+
+ return new SimpleXMLElement( implode('',file($filename)) );
}
@@ -227,42 +222,5 @@ class TemplateEngine
}
-
- /**
- * @param $document array
- * @param $includePath
- * @return array
- */
- private function parseIncludes($document, $includePath )
- {
- $newDocument = array();
-
- foreach ($document as $element) {
- // Initialisieren der m�glichen Element-Inhalte
- $type = '';
- $attributes = array();
- $value = '';
- $tag = '';
-
- // Setzt: $tag, $attributes, $value, $type
- extract($element);
-
- if ($tag == 'include') {
- if ($type == 'complete' || $type == 'open') {
-
- $includeDocument = $this->loadDocument($includePath . '/' . $attributes['file'] . '.inc.xml');
- $newDocument = array_merge($newDocument,$includeDocument);
-
- }
- }
- else
- {
- $newDocument[] = $element;
- }
- }
-
- return $newDocument;
- }
}
-?>-
\ No newline at end of file
diff --git a/modules/util/Browser.class.php b/modules/util/Browser.class.php
@@ -1,13 +1,18 @@
<?php
+/**
+ * Very simple approach to identify a browsers name and platform.
+ */
class Browser {
public $name;
public $platform;
-
- public function Browser()
+ /**
+ * Takes the user agent from the HTTP request and analyzes name and platform.
+ */
+ public function __construct()
{
$agent = @$_SERVER['HTTP_USER_AGENT'];
diff --git a/modules/util/JSON.class.php b/modules/util/JSON.class.php
@@ -798,7 +798,7 @@ class JSON
*/
class JSON_Error
{
- function JSON_Error($message = 'unknown error', $code = null,
+ function __construct($message = 'unknown error', $code = null,
$mode = null, $options = null, $userinfo = null)
{