DslProperty.class.php (1521B)
1 <?php 2 3 namespace dsl\ast; 4 5 use cms\generator\dsl\DslObject; 6 use dsl\context\Scriptable; 7 use dsl\DslRuntimeException; 8 9 class DslProperty implements DslStatement 10 { 11 public $variable; 12 public $property; 13 14 /** 15 * DslProperty constructor. 16 * @param $variable 17 * @param $property 18 */ 19 public function __construct($variable, $property) 20 { 21 $this->variable = $variable; 22 $this->property = $property; 23 } 24 25 26 /** 27 * @param array $context 28 * @return mixed 29 * @throws DslRuntimeException 30 */ 31 public function execute( & $context ) { 32 33 $object = $this->variable->execute( $context ); 34 35 $objectContext = []; 36 37 if ( is_object( $object ) ) { 38 39 $objectContext = get_object_vars( $object ); 40 41 // copy object methods to the object context to make them callable. 42 foreach( get_class_methods( $object ) as $method ) { 43 $objectContext[ $method ] = function() use ($method, $object) { 44 45 // For Security: Do not expose all available objects, they must implement a marker interface. 46 if ( ! $object instanceof Scriptable ) 47 throw new DslRuntimeException('security: Object '.get_class($object).' is not scriptable and therefore not available in script context'); 48 49 return call_user_func_array( array($object,$method),func_get_args() ); 50 }; 51 } 52 } 53 elseif ( is_array( $object ) ) { 54 55 $objectContext = $object; 56 57 } else { 58 59 throw new DslRuntimeException('not an object'); 60 } 61 62 $prop = $this->property->execute( $objectContext ); 63 64 65 return $prop; 66 } 67 68 public function parse($tokens) 69 { 70 } 71 }