[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/macro/src/ -> lmbMacroNode.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  lmb_require('limb/macro/src/lmbMacroException.class.php');
  10  lmb_require('limb/macro/src/lmbMacroSourceLocation.class.php');
  11  
  12  /**

  13   * class lmbMacroNode.

  14   *

  15   * @package macro

  16   * @version $Id$

  17   */
  18  class lmbMacroNode
  19  {
  20    protected $id;  
  21    protected $children = array();
  22    protected $parent;  
  23    /**

  24    * @var lmbMacroSourceLocation

  25    **/
  26    protected $location;    
  27  
  28    function __construct($location = null)
  29    {
  30      if($location)
  31        $this->location = $location;
  32      else
  33        $this->location = new lmbMacroSourceLocation();
  34    }
  35    
  36    function setParent($parent)
  37    {
  38      $this->parent = $parent;
  39    }
  40    
  41    function getParent()
  42    {
  43      return $this->parent;
  44    }
  45    
  46    function getLocationInTemplate()
  47    {
  48      return $this->location;
  49    }
  50  
  51    function getTemplateFile()
  52    {
  53      return $this->location->getFile();
  54    }
  55  
  56    function getTemplateLine()
  57    {
  58      return $this->location->getLine();
  59    }
  60  
  61    function getId()
  62    {
  63      if($this->id)
  64        return $this->id;
  65          
  66      $this->id = self :: generateNewId();
  67      return $this->id;
  68    }
  69  
  70    function setId($id)
  71    {
  72      $this->id = $id;
  73    }  
  74    
  75    static function generateNewId()
  76    {
  77      static $counter = 1;
  78      return 'id00' . $counter++;
  79    }    
  80    
  81    function raise($error, $vars = array())
  82    {    
  83      $vars['file'] = $this->location->getFile();
  84      $vars['line'] = $this->location->getLine();
  85      throw new lmbMacroException($error, $vars);
  86    }  
  87    
  88    function addChild($child)
  89    {
  90      $child->parent = $this;
  91      $this->children[] = $child;
  92    }
  93  
  94    function removeChild($id)
  95    {
  96      foreach(array_keys($this->children) as $key)
  97      {
  98        $child = $this->children[$key];
  99        if($child->getId() == $id)
 100        {
 101          unset($this->children[$key]);
 102          return $child;
 103        }
 104      }
 105    }
 106  
 107    function getChildren()
 108    {
 109      return $this->children;
 110    }
 111  
 112    function removeChildren()
 113    {
 114      foreach (array_keys($this->children) as $key)
 115      {
 116        $this->children[$key]->removeChildren();
 117        unset($this->children[$key]);
 118      }
 119    }
 120  
 121    function getChild($id)
 122    {
 123      if($child = $this->findChild($id))
 124        return $child;
 125      else
 126        $this->raise('Could not find component', array('id' => $id));
 127    }
 128  
 129    function findChild($id)
 130    {
 131      foreach(array_keys($this->children) as $key)
 132      {
 133        if($this->children[$key]->getId() == $id)
 134          return $this->children[$key];
 135        else
 136          return $this->children[$key]->findChild($id);          
 137      }
 138    }
 139  
 140    function findChildByClass($class)
 141    {
 142      foreach(array_keys($this->children) as $key)
 143      {
 144        if(is_a($this->children[$key], $class))
 145          return $this->children[$key];
 146        else
 147        {
 148          if($result = $this->children[$key]->findChildByClass($class))
 149            return $result;
 150        }
 151      }
 152    }
 153  
 154    function findChildrenByClass($class)
 155    {
 156      $ret = array();
 157      foreach(array_keys($this->children) as $key)
 158      {
 159        if(is_a($this->children[$key], $class))
 160          $ret[] = $this->children[$key];
 161        else
 162        {
 163          $more_children = $this->children[$key]->findChildrenByClass($class);
 164          if(count($more_children))
 165            $ret = array_merge($ret, $more_children);
 166        }
 167      }
 168      return $ret;
 169    }
 170  
 171    function findImmediateChildByClass($class)
 172    {
 173      foreach(array_keys($this->children) as $key)
 174      {
 175        if(is_a($this->children[$key], $class))
 176          return $this->children[$key];
 177      }
 178    }
 179  
 180    function findImmediateChildrenByClass($class)
 181    {
 182      $result = array();
 183      foreach(array_keys($this->children) as $key)
 184      {
 185        if(is_a($this->children[$key], $class))
 186          $result[] = $this->children[$key];
 187      }
 188      return $result;
 189    }  
 190    
 191    function findParentByClass($class)
 192    {
 193      $parent = $this->parent;
 194  
 195      while($parent && !is_a($parent, $class))
 196        $parent = $parent->parent;
 197  
 198      return $parent;
 199    }  
 200    
 201    function prepare()
 202    {
 203      foreach(array_keys($this->children) as $key)
 204        $this->children[$key]->prepare();
 205    }  
 206  
 207    function preParse(){}
 208    
 209    function generateConstructor($code_writer)
 210    {
 211      foreach(array_keys($this->children) as $key)
 212        $this->children[$key]->generateConstructor($code_writer);
 213    }
 214  
 215    function generateContents($code_writer)
 216    {
 217      foreach(array_keys($this->children) as $key)
 218        $this->children[$key]->generate($code_writer);
 219    }
 220    
 221    function generate($code_writer)
 222    {   
 223      $this->generateContents($code_writer);   
 224    }  
 225       
 226    /**

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

 228    * amongst its siblings.

 229    */
 230    function checkChildrenIds()
 231    {
 232      $child_ids = array();
 233      $checked_children = array();
 234      foreach($this->getChildren() as $key => $child)
 235      {
 236        $id = $child->getId();
 237        if (in_array($id, $child_ids))
 238        {
 239          $duplicate_child = $checked_children[$id];
 240          $child->raise('Duplicate "id" attribute',
 241                                     array('id' => $id,
 242                                           'duplicate_node_file' => $duplicate_child->getTemplateFile(),
 243                                           'duplicate_node_line' => $duplicate_child->getTemplateLine()));
 244        }
 245        else
 246        {
 247          $child_ids[] = $id;
 248          $checked_children[$id] = $child;
 249        }
 250      }
 251    }  
 252  }
 253  ?>


Generated: Tue Oct 14 04:47:40 2008 Cross-referenced by PHPXref 0.7