commit 8b980fcfcd8e1dbdd620499a8aec0cc0795985c2
parent a2d99ce407fbc962c83fc05425c625422f99e701
Author: Jan Dankert <develop@jandankert.de>
Date: Fri, 15 Nov 2019 02:09:02 +0100
Refactoring: No public attributes in Macro class; using YAML for macro parameters.
Diffstat:
5 files changed, 51 insertions(+), 95 deletions(-)
diff --git a/modules/cms-core/action/ElementAction.class.php b/modules/cms-core/action/ElementAction.class.php
@@ -8,6 +8,8 @@ use cms\model\Project;
use cms\model\Template;
use cms\model\Folder;
use cms\model\BaseObject;
+use ReflectionClass;
+use ReflectionProperty;
use Text;
@@ -420,7 +422,11 @@ class ElementAction extends BaseAction
$className = $this->element->subtype;
$fileName = OR_DYNAMICCLASSES_DIR.'/'.$className.'.class.'.PHP_EXT;
- if ( is_file( $fileName ) )
+ $description = '';
+ $paramList = array();
+ $parameters = array();
+
+ if ( is_file( $fileName ) )
{
require( $fileName );
@@ -428,41 +434,30 @@ class ElementAction extends BaseAction
{
$dynEl = new $className;
- $desc = array();
-
$description = $dynEl->description;
- $paramList = array();
$old = $this->element->getDynamicParameters();
- $parameters = '';
- foreach( get_object_vars($dynEl) as $paramName=>$paramDesc )
+ $reflect = new ReflectionClass($dynEl);
+ $props = $reflect->getProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED);
+ foreach( get_object_vars($dynEl) as $paramName=>$paramValue )
{
- if ( isset( $dynEl->$paramName ) )
- {
- if ( is_object($dynEl->$paramName))
- continue;
- if ( is_array($dynEl->$paramName))
- continue;
- if ( in_array($paramName,array('output')))
- continue;
- $paramList[$paramName] = $dynEl->$paramName;
-
- $parameters .= $paramName.':';
- if ( !empty($old[$paramName]) )
- $parameters .= $old[$paramName];
- else
- $parameters .= $dynEl->$paramName;
- $parameters .= "\n";
- }
+ $paramList[$paramName] = print_r( $paramValue, true);
+
+ if ( @$old[$paramName] )
+ $parameters[$paramName] = $old[$paramName];
+ else
+ $parameters[$paramName] = $paramValue;
}
- $this->setTemplateVar('dynamic_class_description',$dynEl->description );
- $this->setTemplateVar('dynamic_class_parameters' ,$paramList );
- $this->setTemplateVar('parameters' ,$parameters );
}
}
+ $this->setTemplateVar('dynamic_class_description',$description );
+ $this->setTemplateVar('dynamic_class_parameters' ,$paramList );
+ $this->setTemplateVar('parameters' ,\Spyc::YAMLDump($parameters) );
+
+
break;
case Element::ELEMENT_TYPE_CODE:
diff --git a/modules/cms-core/model/Element.class.php b/modules/cms-core/model/Element.class.php
@@ -2,6 +2,7 @@
namespace cms\model;
+use ArrayUtils;
use Logger;
/**
@@ -538,9 +539,9 @@ SQL
$items = \Spyc::YAMLLoadString( $this->code );
- Logger::trace('dynamic-parameters: '.print_r($items,true));
+ Logger::trace('dynamic-parameters: '.print_r($items,true));
- return $items;
+ return (array) $items;
}
diff --git a/modules/cms-core/model/Value.class.php b/modules/cms-core/model/Value.class.php
@@ -1253,9 +1253,7 @@ SQL
// In dieser wird der Code in eine Datei geschrieben und
// von dort eingebunden.
$code = new Code();
- $code->page = &$this->page;
- $code->setObjectId( $this->page->objectid );
- $code->delOutput();
+ $code->setContextPage($this->page );
$code->code = $this->element->code;
ob_start();
@@ -1292,13 +1290,11 @@ SQL
{
/** @var \Macro $macro */
$macro = new $className;
- $macro->page = &$this->page;
if ( method_exists( $macro,'execute' ) )
{
//$$macro->delOutput();
- $macro->objectid = $this->page->objectid;
- $macro->page = &$this->page;
+ $macro->setContextPage( $this->page );
$parameters = $this->element->getDynamicParameters();
@@ -1311,8 +1307,9 @@ SQL
foreach( $parameters as $param_name=>$param_value )
{
- if ( $param_value[0]=='{')
+ if ( $param_value && $param_value[0]=='{')
{
+ // TODO: Why this? Better use the VariableResolver for this.
$elName = substr($param_value,1,strpos($param_value,'}')-1);
$template = new Template($this->page->templateid);
$elements = $template->getElementNames();
@@ -1330,13 +1327,13 @@ SQL
}
if ( isset( $macro->$param_name ) )
{
- Logger::debug("Setting parameter for Macro-class $className, ".$param_name.':'.$param_value );
+ Logger::trace("Setting parameter for Macro-class $className, ".$param_name.':'.$param_value );
// Die Parameter der Makro-Klasse typisiert setzen.
if ( is_int($macro->$param_name) )
$macro->$param_name = intval($param_value);
elseif ( is_array($macro->$param_name) )
- $macro->$param_name = explode(',',$param_value);
+ $macro->$param_name = (array)$param_value;
else
$macro->$param_name = $param_value;
@@ -1344,7 +1341,7 @@ SQL
else
{
if ( !$this->publisher->isPublic() )
- $inhalt .= "WARNING: Unknown parameter $param_name in macro $className\n";
+ $inhalt .= "*WARNING*: Unknown parameter $param_name in macro $className\n";
}
}
diff --git a/modules/util/Macro.class.php b/modules/util/Macro.class.php
@@ -23,25 +23,19 @@ use cms\model\Project;
* Service-Klasse fuer allgemeine Interface-Methoden. Insbesondere
* in Code-Elementen kann und soll auf diese Methoden zurueckgegriffen
* werden.
- * @author $Author$
- * @version $Revision$
- * @package openrat.services
+ *
+ * @author Jan Dankert
*/
class Macro
{
/**
- * @var \cms\model\Project Projekt
- */
- var $output = '';
- var $objectid = 0;
-
- /**
* @var Page
*/
- var $page;
- var $parameters = array();
- var $description = '';
+ protected $page;
+ public function setContextPage( &$page ) {
+ $this->page = $page;
+ }
/**
* Ausführen des Makros. Diese Methode sollte durch die Unterklasse überschrieben werden.
@@ -55,9 +49,9 @@ class Macro
/**
* Holt die aktuellen Datenbankverbindung.
*/
- public function db()
+ protected function db()
{
- return db_connection();
+ return db();
}
@@ -65,9 +59,9 @@ class Macro
* Holt die aktuelle Objekt-Id.
* @return number
*/
- public function getObjectId()
+ protected function getObjectId()
{
- return $this->objectid;
+ return $this->page->objectid;
}
@@ -75,9 +69,9 @@ class Macro
* Holt die aktuelle Seite.
* @return \cms\model\Page
*/
- public function getPage()
+ protected function getPage()
{
- return new Page( $this->objectid );
+ return $this->page;
}
@@ -85,26 +79,17 @@ class Macro
* Holt das aktuelle Objekt.
* @return Object
*/
- public function &getObject()
+ protected function &getObject()
{
return $this->page;
}
- /**
- * Setzt eine Objekt-Id.
- * @param int $objectid
- */
- public function setObjectId( $objectid )
- {
- $this->objectid = $objectid;
- }
-
/**
* Ermittelt die Id des Wurzelordners im aktuellen Projekt.
*/
- public function getRootObjectId()
+ protected function getRootObjectId()
{
$project = new Project( $this->page->projectid);
return $project->getRootObjectId();
@@ -112,27 +97,16 @@ class Macro
/**
- * DO NOT USE.
- * @deprecated
- */
- public function folderid()
- {
- global $SESS;
- return $SESS['folderid'];
- }
-
-
- /**
* Löscht die bisher erzeugte Ausgabe.
* @deprecated useless
*/
public function delOutput()
{
- $this->output = '';
}
/**
- * Ergänzt die Standardausgabe um den gewünschten Wert.
+ * Ergänzt die Standardausgabe um den gewünschten Wert.
+ * @deprecated use echo()
*/
public function output( $text )
{
@@ -141,7 +115,8 @@ class Macro
/**
- * Ergänzt die Standardausgabe um den gewünschten Wert. Es wird ein Zeilenendezeichen ergänzt.
+ * Ergänzt die Standardausgabe um den gewünschten Wert. Es wird ein Zeilenendezeichen ergänzt.
+ * @deprecated use echo()
*/
public function outputLn( $text )
{
@@ -150,17 +125,6 @@ class Macro
/**
- * Ermittelt die bisher erstellte Ausgabe.
- * @deprecated
- * @return string
- */
- public function getOutput()
- {
- return $this->output;
- }
-
-
- /**
* Setzt eine Sitzungsvariable.
*
* @param String $var
diff --git a/modules/wikiparser/renderer/HtmlRenderer.class.php b/modules/wikiparser/renderer/HtmlRenderer.class.php
@@ -230,12 +230,11 @@ class HtmlRenderer
{
$macro = new $className;
/* @type $macro Macro */
- $macro->page = &$this->page;
+ $macro->setContextPage( $this->page );
if ( method_exists( $macro,'execute' ) )
{
- $macro->objectid = $this->page->objectid;
- $macro->page = &$this->page;
+ $macro->setContextPage( $this->page );
foreach( $child->attributes as $param_name=>$param_value )
{