openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

DslVariable.class.php (1377B)


      1 <?php
      2 
      3 namespace dsl\ast;
      4 
      5 use dsl\context\Scriptable;
      6 use dsl\DslRuntimeException;
      7 use dsl\executor\DslInterpreter;
      8 
      9 class DslVariable implements DslStatement
     10 {
     11 	public $name;
     12 
     13 	/**
     14 	 * @param $name
     15 	 */
     16 	public function __construct($name)
     17 	{
     18 		$this->name = $name;
     19 	}
     20 
     21 
     22 	public function execute( & $context ) {
     23 
     24 		if   (  is_object( $context) ) {
     25 
     26 			// copy object methods to the object context to make them callable.
     27 			$property = $this->name;
     28 
     29 			if   ( property_exists( $context,$property ) ) {
     30 
     31 				return $context->$property;
     32 			}
     33 
     34 			if   ( method_exists( $context,$this->name ) ) {
     35 
     36 				return function() use ($property, $context) {
     37 
     38 					// For Security: Do not expose all available objects, they must implement a marker interface.
     39 					if   ( DslInterpreter::isSecure() && ! $context instanceof Scriptable )
     40 						throw new DslRuntimeException('Object '.get_class($context).' is not marked as scriptable and therefore not available in secure mode');
     41 
     42 					return call_user_func_array( array($context,$property),func_get_args() );
     43 				};
     44 			}
     45 
     46 			throw new DslRuntimeException('method or property \''.$property.'\' does not exist' );
     47 		}
     48 
     49 		if   ( ! array_key_exists( $this->name, $context ) )
     50 			throw new DslRuntimeException('variable or property \''.$this->name.'\' does not exist');
     51 
     52 		return $context[ $this->name ];
     53 	}
     54 
     55 	public function parse($tokens)
     56 	{
     57 	}
     58 }