commit 651be9b411b4aa62deba5b1b483d32d2f8bf1a3e
parent c1f436bc9b635f49dc5ba565ae218bd69534c255
Author: Jan Dankert <develop@jandankert.de>
Date: Tue, 31 May 2022 00:30:18 +0200
New: DSL with support for parameterless functions.
Diffstat:
3 files changed, 40 insertions(+), 9 deletions(-)
diff --git a/modules/dsl/ast/DslFunction.class.php b/modules/dsl/ast/DslFunction.class.php
@@ -42,6 +42,7 @@ class DslFunction extends DslElement implements DslStatement
{
//$this->parameters = $functionParameter;
+ $this->parameters = [];
//var_export($this->splitByComma( $functionParameter ));
foreach( $this->splitByComma( $functionParameter ) as $parameter ) {
if ( sizeof($parameter) != 1 )
diff --git a/modules/dsl/ast/DslFunctionCall.class.php b/modules/dsl/ast/DslFunctionCall.class.php
@@ -17,6 +17,14 @@ class DslFunctionCall implements DslStatement
*/
public function __construct($name, $parameters)
{
+ //echo "name:";var_export( $name );
+ //echo "params:";var_export( $parameters );
+
+ // Parameterless function calls are not correctly detected by the AST parser
+ if ( $name==null) {
+ $name = $parameters;
+ $parameters = null;
+ }
$this->name = $name;
$this->parameters = $parameters;
}
@@ -37,7 +45,10 @@ class DslFunctionCall implements DslStatement
//
// $function = $context[$name];
- $parameterValues = $this->parameters->execute( $context );
+ if ( $this->parameters == null )
+ $parameterValues = []; // parameterless functions.
+ else
+ $parameterValues = $this->parameters->execute( $context );
// if there is only 1 parameter it must be converted to an array.
// if there are more than 1 parameter, it is already a sequence
@@ -52,6 +63,7 @@ class DslFunctionCall implements DslStatement
elseif ( $function instanceof DslFunction ) {
$parameters = $function->parameters;
+ //var_export( $function->parameters);
if ( sizeof($parameters) != sizeof($parameterValues) )
throw new DslRuntimeException('function call has '.sizeof($parameterValues).' parameters but the function has '.sizeof($parameters).' parameters');
diff --git a/modules/dsl/ast/DslProperty.class.php b/modules/dsl/ast/DslProperty.class.php
@@ -2,6 +2,7 @@
namespace dsl\ast;
+use cms\generator\dsl\DslObject;
use dsl\DslRuntimeException;
class DslProperty implements DslStatement
@@ -30,18 +31,35 @@ class DslProperty implements DslStatement
$object = $this->variable->execute( $context );
- if ( ! is_object( $object ) )
- throw new DslRuntimeException('is no object');
+ $objectContext = [];
- $objectContext = get_object_vars( $object );
+ if ( is_object( $object ) ) {
- // copy object methods to the object context to make them callable.
- foreach( get_class_methods( $object ) as $method ) {
- $objectContext[ $method ] = function() use ($method, $object) {
- return call_user_func_array( array($object,$method),func_get_args() );
- };
+ $objectContext = get_object_vars( $object );
+
+ // copy object methods to the object context to make them callable.
+ foreach( get_class_methods( $object ) as $method ) {
+ $objectContext[ $method ] = function() use ($method, $object) {
+ return call_user_func_array( array($object,$method),func_get_args() );
+ };
+ }
+ }
+ elseif ( is_array( $object ) ) {
+
+ $objectContext = $object;
+
+ } else {
+
+ throw new DslRuntimeException('not an object');
}
+
$prop = $this->property->execute( $objectContext );
+
+ // TODO: how to recognize objects
+ // For Security: Do not expose internal objects.
+ //if ( is_object($prop) && ! $prop instanceof DslObject )
+ // $prop = '@'.get_class($prop).'@';
+
return $prop;
}