[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/wact/src/compiler/compile_tree_node/ -> WactCompileTreeNode.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 WactCompileTreeNode.

  12   *

  13   * @package wact

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

  15   */
  16  class WactCompileTreeNode
  17  {
  18    var $children = array();
  19  
  20    var $parent = NULL;
  21  
  22    var $ServerId;
  23  
  24    /**

  25    * @var WactSourceLocation

  26    **/
  27    protected $location_in_template;
  28  
  29    protected $properties = array();
  30  
  31    function __construct($location = null)
  32    {
  33      if($location)
  34        $this->location_in_template = $location;
  35      else
  36        $this->location_in_template = new WactSourceLocation();
  37    }
  38  
  39    function raiseCompilerError($error, $vars = array())
  40    {
  41      $vars['file'] = $this->location_in_template->getFile();
  42      $vars['line'] = $this->location_in_template->getLine();
  43      throw new WactException($error, $vars);
  44    }
  45  
  46    function getLocationInTemplate()
  47    {
  48      return $this->location_in_template;
  49    }
  50  
  51    function getTemplateFile()
  52    {
  53      return $this->location_in_template->getFile();
  54    }
  55  
  56    function getTemplateLine()
  57    {
  58      return $this->location_in_template->getLine();
  59    }
  60  
  61    function getServerId()
  62    {
  63      if (empty($this->ServerId))
  64        $this->ServerId = self :: generateNewServerId();
  65  
  66      return $this->ServerId;
  67    }
  68  
  69    function addChild($child)
  70    {
  71      $child->parent = $this;
  72      $this->children[] = $child;
  73    }
  74  
  75    function removeChild($ServerId)
  76    {
  77      foreach(array_keys($this->children) as $key)
  78      {
  79        $child = $this->children[$key];
  80        if ($child->getServerid() == $ServerId)
  81        {
  82          unset($this->children[$key]);
  83          return $child;
  84        }
  85      }
  86    }
  87  
  88    function getChildren()
  89    {
  90      return $this->children;
  91    }
  92  
  93    function removeChildren()
  94    {
  95      foreach (array_keys($this->children) as $key)
  96      {
  97        $this->children[$key]->removeChildren();
  98        unset($this->children[$key]);
  99      }
 100    }
 101  
 102    function getChild($ServerId)
 103    {
 104      if($child = $this->findChild($ServerId))
 105        return $child;
 106      else
 107        $this->raiseCompilerError('Could not find component', array('ServerId' => $ServerId));
 108    }
 109  
 110    function findChild($ServerId)
 111    {
 112      foreach( array_keys($this->children) as $key)
 113      {
 114        if ($this->children[$key]->getServerid() == $ServerId)
 115          return $this->children[$key];
 116        else
 117        {
 118          if ($result = $this->children[$key]->findChild($ServerId))
 119            return $result;
 120        }
 121      }
 122      return FALSE;
 123    }
 124  
 125    /**

 126     * Sometimes it is useful to find treeNode located in another tree branch, eg:

 127     *  <code>

 128     *  <core:BLOCK><some_tag server_id='tag1'></core:BLOCK>

 129     *  <core:BLOCK><some_tag server_id='tag2'></core:BLOCK>

 130     *  </code>

 131     * in this case we can find tag1 tag from tag2 tag using findUpChild.

 132     * @see findChild()

 133     * @param string needed tag ServerId

 134     * @return object|false

 135     */
 136  
 137    function findUpChild($ServerId)
 138    {
 139      if($child = $this->findChild($ServerId))
 140        return $child;
 141  
 142      if($this->parent)
 143        return $this->parent->findUpChild($ServerId);
 144    }
 145  
 146    function findChildByClass($class)
 147    {
 148      foreach( array_keys($this->children) as $key)
 149      {
 150        if (is_a($this->children[$key], $class))
 151            return $this->children[$key];
 152        else
 153        {
 154          if ($result = $this->children[$key]->findChildByClass($class))
 155            return $result;
 156        }
 157      }
 158      $false = FALSE;
 159      return $false;
 160    }
 161  
 162    function findChildrenByClass($class)
 163    {
 164      $ret = array();
 165      foreach( array_keys($this->children) as $key)
 166      {
 167        if (is_a($this->children[$key], $class))
 168            $ret[] =& $this->children[$key];
 169        else
 170        {
 171          $more_children = $this->children[$key]->findChildrenByClass($class);
 172          if (count($more_children))
 173            $ret = array_merge($ret, $more_children);
 174        }
 175      }
 176      return $ret;
 177    }
 178  
 179    function findImmediateChildByClass($class)
 180    {
 181      foreach( array_keys($this->children) as $key)
 182      {
 183        if (is_a($this->children[$key], $class))
 184            return $this->children[$key];
 185      }
 186      $false = FALSE;
 187      return $false;
 188    }
 189  
 190    function findImmediateChildrenByClass($class)
 191    {
 192      $result = array();
 193  
 194      foreach(array_keys($this->children) as $key)
 195      {
 196        if (is_a($this->children[$key], $class))
 197          $result[] = $this->children[$key];
 198      }
 199  
 200      return $result;
 201    }
 202  
 203    function registerProperty($name, $property)
 204    {
 205        $this->properties[$name] = $property;
 206    }
 207  
 208    function getProperty($name)
 209    {
 210      if (array_key_exists($name, $this->properties))
 211          return $this->properties[$name];
 212  
 213      if ($this->isDataSource())
 214        return null;
 215      elseif($this->parent)
 216        return $this->parent->getProperty($name);
 217    }
 218  
 219    function findParentByClass($class)
 220    {
 221      $parent = $this->parent;
 222  
 223      while($parent && !is_a($parent, $class))
 224        $parent = $parent->parent;
 225  
 226      return $parent;
 227    }
 228  
 229    function findSelfOrParentByClass($class)
 230    {
 231      if (is_a($this, $class))
 232       return $this;
 233      else
 234       return $this->findParentByClass($class);
 235    }
 236  
 237    function prepare()
 238    {
 239      foreach( array_keys($this->children) as $key)
 240        $this->children[$key]->prepare();
 241    }
 242  
 243    /**

 244    * Used to perform error checking on template related to the syntax of

 245    * the concrete tag implementing this method.

 246    */
 247    function preParse()
 248    {
 249    }
 250  
 251    function isDataSource()
 252    {
 253      return FALSE;
 254    }
 255  
 256    /**

 257    * If a parent compile time component exists, returns the value of the

 258    * parent's getDataSource() method, which will be a concrete implementation

 259    */
 260    function getDataSource()
 261    {
 262      $result = null;
 263      if (!$this->isDataSource())
 264      {
 265        if (isset($this->parent))
 266          $result = $this->parent->getDataSource();
 267      }
 268  
 269      return $result;
 270    }
 271  
 272    /**

 273    * Gets the parent in the DataSource, if one exists

 274    */
 275    function getParentDataSource()
 276    {
 277      $result = null;
 278  
 279      $DataSource = $this->getDataSource();
 280      if($DataSource && !isset($DataSource->parent))
 281        return $DataSource;
 282  
 283      if (isset($DataSource->parent))
 284        $result = $DataSource->parent->getDataSource();
 285  
 286      return $result;
 287    }
 288  
 289    /**

 290    * Returns a root DataSource

 291    */
 292    function getRootDataSource()
 293    {
 294      $root = $this;
 295      while ($root->parent != NULL)
 296        $root = $root->parent;
 297      return $root;
 298    }
 299  
 300    /**

 301    * Gets the component reference code of the parent. This is a PHP string

 302    * which is used in the compiled template to reference the component in

 303    * the hierarchy at runtime

 304    */
 305    function getComponentRefCode()
 306    {
 307      return $this->parent->getComponentRefCode();
 308    }
 309  
 310    function generateConstructor($code_writer)
 311    {
 312      foreach( array_keys($this->children) as $key)
 313        $this->children[$key]->generateConstructor($code_writer);
 314    }
 315  
 316    function generate($code_writer)
 317    {
 318      foreach( array_keys($this->properties) as $key)
 319      {
 320        if ($this->properties[$key]->isActive())
 321          $this->properties[$key]->generateScopeEntry($code_writer);
 322      }
 323  
 324      $this->generateContent($code_writer);
 325  
 326      foreach(array_keys($this->properties) as $key)
 327      {
 328        if ($this->properties[$key]->isActive())
 329          $this->properties[$key]->generateScopeExit($code_writer);
 330      }
 331    }
 332  
 333    function generateContent($code_writer)
 334    {
 335      $this->generateChildren($code_writer);
 336    }
 337  
 338    function generateChildren($code_writer)
 339    {
 340      foreach( array_keys($this->children) as $key)
 341        $this->children[$key]->generate($code_writer);
 342    }
 343  
 344    function setServerId($id)
 345    {
 346      $this->ServerId = $id;
 347    }
 348  
 349    static function generateNewServerId()
 350    {
 351      static $counter = 1;
 352      return 'id00' . $counter++;
 353    }
 354  
 355    /**

 356    * Checks that each immediate child of the current component has a unique ID

 357    * amongst its siblings.

 358    */
 359    function checkChildrenServerIds()
 360    {
 361      $child_ids = array();
 362      $checked_children = array();
 363      foreach ($this->getChildren() as $key => $child)
 364      {
 365        $id = $child->getServerId();
 366        if (in_array($id, $child_ids))
 367        {
 368          $duplicate_child = $checked_children[$id];
 369          $child->raiseCompilerError('Duplicate "id" attribute',
 370                                     array('ServerId' => $id,
 371                                           'duplicate_component_file' => $duplicate_child->getTemplateFile(),
 372                                           'duplicate_component_line' => $duplicate_child->getTemplateLine()));
 373        }
 374        else
 375        {
 376          $child_ids[] = $id;
 377          $checked_children[$id] = $child;
 378        }
 379      }
 380    }
 381  }
 382  ?>


Generated: Tue Dec 2 03:54:09 2008 Cross-referenced by PHPXref 0.7