[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/wact/src/compiler/expression/node/ -> WactDataBindingExpressionNode.class.php (source)

   1  <?php
   2  /*
   3   * Limb PHP Framework
   4   *
   5   * @link http://limb-project.com 
   6   * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
   7   * @license    LGPL http://www.gnu.org/copyleft/lesser.html 
   8   */
   9  
  10  /**

  11   * class WactDataBindingExpressionNode.

  12   *

  13   * @package wact

  14   * @version $Id: WactDataBindingExpressionNode.class.php 5945 2007-06-06 08:31:43Z pachanga $

  15   */
  16  class WactDataBindingExpressionNode
  17  {
  18    protected $context;
  19    protected $datasource_context;
  20    protected $original_expression;
  21    protected $processed_expression;
  22  
  23    protected $path_to_target_datasource;
  24    protected $field_name;
  25  
  26    protected $datasource_ref_var;
  27  
  28    protected $expression_analyzed = FALSE;
  29  
  30    protected $property;
  31    protected $php_variable = false;
  32  
  33    function __construct($expression, $context_node, $datasource_context = null)
  34    {
  35      $this->original_expression = $expression;
  36      $this->context = $context_node;
  37  
  38      if($datasource_context)
  39        $this->datasource_context = $datasource_context;
  40      else
  41        $this->datasource_context = $context_node;
  42    }
  43  
  44    function analyzeExpression()
  45    {
  46      if ($this->expression_analyzed)
  47        return;
  48  
  49      $this->_findRealContext();
  50  
  51      if(!$this->datasource_context && !$this->php_variable)
  52        $this->context->raiseCompilerError('Expression datasource context not found', array('expression' => $this->original_expression));
  53  
  54      $this->_extractPathToTargetDatasource();
  55  
  56      $this->_extractTargetFieldName();
  57  
  58      if (is_object($this->datasource_context))
  59      {
  60        $this->property = $this->datasource_context->getProperty($this->field_name);
  61        if (is_object($this->property))
  62          $this->property->activate();
  63      }
  64  
  65      $this->expression_analyzed = TRUE;
  66    }
  67  
  68    function getDatasourceContext()
  69    {
  70      $this->analyzeExpression();
  71      return $this->datasource_context;
  72    }
  73  
  74    protected function _findRealContext()
  75    {
  76      $this->processed_expression = $this->original_expression;
  77      do
  78      {
  79        $modifier = $this->processed_expression{0};
  80        // local PHP variable

  81        if ($modifier == "$")
  82        {
  83          $this->datasource_context = null;
  84          $this->php_variable = true;
  85          $this->processed_expression = substr($this->processed_expression, 1);
  86          return;
  87        }
  88  
  89        // tag property

  90        if ($modifier == ":")
  91        {
  92          $this->processed_expression = substr($this->processed_expression, 1);
  93          return;
  94        }
  95  
  96        // root context

  97        if ($modifier == "#")
  98        {
  99          $this->datasource_context = $this->datasource_context->getRootDataSource();
 100          $this->processed_expression = substr($this->processed_expression, 1);
 101          continue;
 102        }
 103  
 104        // parent context

 105        if ($modifier == "^")
 106        {
 107          $this->datasource_context = $this->datasource_context->getParentDataSource();
 108          $this->processed_expression = substr($this->processed_expression, 1);
 109          continue;
 110        }
 111  
 112        // child context

 113        if($modifier == '[')
 114        {
 115          $pos = strpos($this->processed_expression, ']');
 116          $context_name = substr($this->processed_expression, 1, $pos - 1);
 117          $this->datasource_context = $this->datasource_context->findChild($context_name);
 118          if(!$this->datasource_context)
 119            $this->context->raiseCompilerError('None existing expression datasource context', array('expression' => $this->original_expression));
 120  
 121          $this->processed_expression = substr($this->processed_expression, $pos+1);
 122          continue;
 123        }
 124  
 125        break;
 126      }
 127      while(true);
 128    }
 129  
 130    protected function _extractPathToTargetDatasource()
 131    {
 132      $pos = strpos($this->processed_expression, '.');
 133      if (!is_integer($pos))
 134        return;
 135  
 136      $this->path_to_target_datasource = array();
 137      while (preg_match('/^(\w+)\.((?s).*)$/', $this->processed_expression, $match))
 138      {
 139        $this->path_to_target_datasource[] = $match[1];
 140        $this->processed_expression = $match[2];
 141      }
 142    }
 143  
 144    protected function _extractTargetFieldName()
 145    {
 146      if(is_null($this->processed_expression) || $this->processed_expression == "")
 147        return;
 148  
 149      if (preg_match("/^\w+$/", $this->processed_expression))
 150        $this->field_name = $this->processed_expression;
 151      else
 152        $this->context->raiseCompilerError('Invalid data binding', array('expression' => $this->original_expression));
 153    }
 154  
 155    function prepare()
 156    {
 157      $this->analyzeExpression();
 158    }
 159  
 160    function getFieldName()
 161    {
 162      $this->analyzeExpression();
 163      return $this->field_name;
 164    }
 165  
 166    function getPathToTargetDatasource()
 167    {
 168      $this->analyzeExpression();
 169      return $this->path_to_target_datasource;
 170    }
 171  
 172    function isConstant()
 173    {
 174      $this->analyzeExpression();
 175  
 176      if($this->php_variable)
 177        return false;
 178  
 179      if (is_null($this->datasource_context))
 180        return TRUE;
 181  
 182      if (is_object($this->property))
 183        return $this->property->isConstant();
 184  
 185      return FALSE;
 186    }
 187  
 188    /**

 189    * Return the value of this expression

 190    */
 191    function getValue()
 192    {
 193      $this->analyzeExpression();
 194  
 195      if (is_null($this->property) || !$this->property->isConstant())
 196        $this->datasource_context->raiseCompilerError('Cannot resolve data binding', array('expression' => $this->original_expression));
 197      else
 198        return $this->property->getValue();
 199    }
 200  
 201    /**

 202    * Generate setup code for an expression reference

 203    */
 204    function generatePreStatement($code_writer)
 205    {
 206      $this->analyzeExpression();
 207  
 208      if (is_object($this->property))
 209        $this->property->generatePreStatement($code_writer);
 210  
 211      $this->_generateReferencesChainToTargetDatasource($code_writer);
 212    }
 213  
 214    protected function _generateReferencesChainToTargetDatasource($code_writer)
 215    {
 216      if (!isset($this->path_to_target_datasource))
 217        return;
 218  
 219      $key = array_shift($this->path_to_target_datasource);
 220  
 221      $this->datasource_ref_var = $code_writer->getTempVarRef();
 222  
 223      if($this->php_variable)
 224      {
 225        $code_writer->writePHP($this->datasource_ref_var . '= WactTemplate :: makeObject($' . $key . ');');
 226      }
 227      else
 228      {
 229        $code_writer->writePHP($this->datasource_ref_var . '= WactTemplate :: makeObject(' . $this->datasource_context->getDataSource()->getComponentRefCode() . '->get(');
 230        $code_writer->writePHPLIteral($key);
 231        $code_writer->writePHP('));');
 232      }
 233  
 234      foreach ($this->path_to_target_datasource as $key)
 235      {
 236        $datasource_ref_var = $code_writer->getTempVarRef();
 237        $code_writer->writePHP($datasource_ref_var . '= WactTemplate :: makeObject(' . $this->datasource_ref_var . '->get(');
 238        $code_writer->writePHPLIteral($key);
 239        $code_writer->writePHP('));');
 240        $this->datasource_ref_var = $datasource_ref_var;
 241      }
 242    }
 243  
 244    /**

 245    * Generate the code to read the data value at run time

 246    * Must generate only a valid PHP Expression.

 247    */
 248    function generateExpression($code_writer)
 249    {
 250      $this->analyzeExpression();
 251  
 252      if (is_object($this->property))
 253      {
 254        $this->property->generateExpression($code_writer);
 255        return;
 256      }
 257  
 258      if($this->php_variable)
 259      {
 260        if($this->datasource_ref_var)
 261        {
 262          $code_writer->writePHP($this->datasource_ref_var . '->get(');
 263          $code_writer->writePHPLiteral($this->field_name);
 264          $code_writer->writePHP(')');
 265          return;
 266        }
 267        else
 268        {
 269          $code_writer->writePHP('$' . $this->field_name);
 270          return;
 271        }
 272      }
 273  
 274      if (isset($this->datasource_ref_var))
 275      {
 276        $code_writer->writePHP($this->datasource_ref_var . '->get(');
 277        $code_writer->writePHPLiteral($this->field_name);
 278        $code_writer->writePHP(')');
 279      }
 280      else
 281      {
 282        if($this->field_name)
 283        {
 284          $code_writer->writePHP('' . $this->datasource_context->getDatasource()->getComponentRefCode() . '->get(');
 285          $code_writer->writePHPLiteral($this->field_name);
 286          $code_writer->writePHP(')');
 287        }
 288        else
 289        {
 290          $code_writer->writePHP($this->datasource_context->getComponentRefCode());
 291        }
 292      }
 293    }
 294  
 295    function generatePostStatement($code_writer)
 296    {
 297      $this->analyzeExpression();
 298  
 299      if (is_object($this->property))
 300        $this->property->generatePostStatement($code_writer);
 301    }
 302  }
 303  
 304  ?>


Generated: Mon Dec 1 03:56:46 2008 Cross-referenced by PHPXref 0.7