[limb-svn] r6777 - in 3.x/trunk/limb/macro: src/compiler src/tags/core src/tags/tree tests/cases/tags/tree

svn at limb-project.com svn at limb-project.com
Fri Feb 8 12:55:05 MSK 2008


Author: serega
Date: 2008-02-08 12:55:05 +0300 (Fri, 08 Feb 2008)
New Revision: 6777
URL: http://fisheye.limb-project.com/changelog/limb/?cs=6777

Added:
   3.x/trunk/limb/macro/src/tags/tree/tree_item.tag.php
   3.x/trunk/limb/macro/src/tags/tree/tree_next_level.tag.php
Removed:
   3.x/trunk/limb/macro/src/tags/tree/tree_branch.tag.php
   3.x/trunk/limb/macro/src/tags/tree/tree_item.tag.php
Modified:
   3.x/trunk/limb/macro/src/compiler/lmbMacroTag.class.php
   3.x/trunk/limb/macro/src/tags/core/apply.tag.php
   3.x/trunk/limb/macro/src/tags/core/include.tag.php
   3.x/trunk/limb/macro/src/tags/tree/tree.tag.php
   3.x/trunk/limb/macro/tests/cases/tags/tree/lmbMacroTreeTagTest.class.php
Log:
-- temporary commit on making {{tree}} tags more flexible.

Modified: 3.x/trunk/limb/macro/src/compiler/lmbMacroTag.class.php
===================================================================
--- 3.x/trunk/limb/macro/src/compiler/lmbMacroTag.class.php	2008-02-08 08:43:19 UTC (rev 6776)
+++ 3.x/trunk/limb/macro/src/compiler/lmbMacroTag.class.php	2008-02-08 09:55:05 UTC (rev 6777)
@@ -224,5 +224,42 @@
 
     }
   }
+  
+  function attributesIntoArgs()
+  {
+    $keys = array();
+    $vals = array();
+    foreach($this->attributes as $k => $attribute)
+    {
+      $keys[] = '$' . $k;
+      $vals[] = $this->getEscaped($k);
+    }
+    return array($keys, $vals);
+  }
+
+  function attributesIntoArray($skip = array())
+  {
+    $arr = array();
+    foreach($this->attributes as $k => $attribute)
+    {
+      if(in_array($k, $skip))
+        continue;
+      
+      $arr[$k] = $this->getEscaped($k);
+    }
+    return $arr;
+  }
+  
+  function attributesIntoArrayString($skip = array())
+  {
+    $args = $this->attributesIntoArray($skip);
+
+    $arg_str = 'array(';
+    foreach($args as $key => $value)
+      $arg_str .= "'$key' => $value,";
+    $arg_str .= ')';
+    
+    return $arg_str;
+  }
 }
 

Modified: 3.x/trunk/limb/macro/src/tags/core/apply.tag.php
===================================================================
--- 3.x/trunk/limb/macro/src/tags/core/apply.tag.php	2008-02-08 08:43:19 UTC (rev 6776)
+++ 3.x/trunk/limb/macro/src/tags/core/apply.tag.php	2008-02-08 09:55:05 UTC (rev 6777)
@@ -19,22 +19,9 @@
   {
     $name = $this->get('template');
 
-    $args = $this->_attributesIntoArray();
+    $arg_str = $this->attributesIntoArrayString();
 
-    $arg_str = 'array(';
-    foreach($args as $key => $value)
-      $arg_str .= "'$key' => $value,";
-    $arg_str .= ')';
-
     $code->writePHP('$this->_template'. $name . '(' . $arg_str . ');');
   }
-
-  protected function _attributesIntoArray()
-  {
-    $arr = array();
-    foreach($this->attributes as $k => $attribute)
-      $arr[$k] = $this->getEscaped($k);
-    return $arr;
-  }
 }
 

Modified: 3.x/trunk/limb/macro/src/tags/core/include.tag.php
===================================================================
--- 3.x/trunk/limb/macro/src/tags/core/include.tag.php	2008-02-08 08:43:19 UTC (rev 6776)
+++ 3.x/trunk/limb/macro/src/tags/core/include.tag.php	2008-02-08 09:55:05 UTC (rev 6777)
@@ -43,13 +43,8 @@
 
   function _generateDynamicaly($code)
   {
-    $args = $this->_attributesIntoArray();
+    $arg_str = $this->attributesIntoArrayString();
 
-    $arg_str = 'array(';
-    foreach($args as $key => $value)
-      $arg_str .= "'$key' => $value,";
-    $arg_str .= ')';
-
     $code->writePHP('$this->includeTemplate(' . $this->get('file') . ',' . $arg_str . ');');
   }
 
@@ -57,7 +52,7 @@
   {
     static $counter = 1;
 
-    list($keys, $vals) = $this->_attributesIntoArgs();
+    list($keys, $vals) = $this->attributesIntoArgs();
 
     $method = $code->beginMethod('__staticInclude' . ($counter++), $keys);
     parent :: _generateContent($code);
@@ -65,25 +60,5 @@
 
     $code->writePHP('$this->' . $method . '(' . implode(', ', $vals) . ');');
   }
-
-  protected function _attributesIntoArgs()
-  {
-    $keys = array();
-    $vals = array();
-    foreach($this->attributes as $k => $attribute)
-    {
-      $keys[] = '$' . $k;
-      $vals[] = $this->getEscaped($k);
-    }
-    return array($keys, $vals);
-  }
-
-  protected function _attributesIntoArray()
-  {
-    $arr = array();
-    foreach($this->attributes as $k => $attribute)
-      $arr[$k] = $this->getEscaped($k);
-    return $arr;
-  }
 }
 

Modified: 3.x/trunk/limb/macro/src/tags/tree/tree.tag.php
===================================================================
--- 3.x/trunk/limb/macro/src/tags/tree/tree.tag.php	2008-02-08 08:43:19 UTC (rev 6776)
+++ 3.x/trunk/limb/macro/src/tags/tree/tree.tag.php	2008-02-08 09:55:05 UTC (rev 6777)
@@ -27,47 +27,59 @@
 
     $before_branch = $this->_getTagsBeforeBranch();
     $after_branch = $this->_getTagsAfterBranch();
-    $branch = $this->findImmediateChildByClass('lmbMacroTreeBranchTag');
+    $branch = $this->findImmediateChildByClass('lmbMacroTreeItemTag');
 
     $tree = $this->get('using');
 
     $items = $code->generateVar();
     $counter = $code->generateVar();
+    $extra_params = $code->generateVar(); 
 
-    $method = $code->beginMethod('_render_tree'. uniqid(), array($items, $level));
-    $code->writePHP($counter . '=0;');
+    $this->method = $code->beginMethod('_render_tree'. uniqid(), array($items, $level, $extra_params . '= array()'));
+    
+    $code->writePHP("if($extra_params) extract($extra_params);"); 
 
-    $code->writePHP('foreach(' . $items . ' as ' . $as . ') {');
+    $code->writePHP($counter . "=0;\n");
 
+    $code->writePHP('foreach(' . $items . ' as ' . $as . ") {\n");
+
+    if($user_counter = $this->get('counter'))
+      $code->writePHP($user_counter . ' = ' . $counter . "+1;\n");
+     
     //rendering tags before branch
-    $code->writePHP('if(!' . $counter . ') {');
+    $code->writePHP('if(!' . $counter . ") {\n");
     foreach($before_branch as $tag)
       $tag->generate($code);
-    $code->writePHP('}');
+    $code->writePHP("}\n");
 
-    $branch->setRecursionMethod($method);
     $branch->generate($code);
 
-    $code->writePHP($counter . '++;');
-    $code->writePHP('}');//foreach
+    $code->writePHP($counter . "++;\n");
+    $code->writePHP("}\n");//foreach
 
     //rendering tags after branch
-    $code->writePHP('if(' . $counter . ') {');
+    $code->writePHP('if(' . $counter . ") {\n");
     foreach($after_branch as $tag)
       $tag->generate($code);
-    $code->writePHP('}');
+    $code->writePHP("}\n");
 
     $code->endMethod();
 
-    $code->writePHP('$this->' . $method . '(' . $tree . ', 0);');
+    $arg_str = $this->extraAttributesIntoArrayString();
+    $code->writePHP('$this->' . $this->method . '(' . $tree . ', 0' . ',' . $arg_str . ");\n");
   }
+  
+  function getRecursionMethod()
+  {
+    return $this->method;
+  }
 
   protected function _getTagsBeforeBranch()
   {
     $tags = array();
     foreach($this->children as $child)
     {
-      if(is_a($child, 'lmbMacroTreeBranchTag'))
+      if(is_a($child, 'lmbMacroTreeItemTag'))
         break;
       $tags[] = $child;
     }
@@ -82,10 +94,15 @@
     {
       if($collect)
         $tags[] = $child;
-      if(is_a($child, 'lmbMacroTreeBranchTag'))
+      if(is_a($child, 'lmbMacroTreeItemTag'))
         $collect = true;
     }
     return $tags;
   }
+  
+  function extraAttributesIntoArrayString()
+  {
+    return $this->attributesIntoArrayString($skip = array('as', 'using', 'counter', 'level'));
+  }
 }
 

Deleted: 3.x/trunk/limb/macro/src/tags/tree/tree_branch.tag.php
===================================================================
--- 3.x/trunk/limb/macro/src/tags/tree/tree_branch.tag.php	2008-02-08 08:43:19 UTC (rev 6776)
+++ 3.x/trunk/limb/macro/src/tags/tree/tree_branch.tag.php	2008-02-08 09:55:05 UTC (rev 6777)
@@ -1,84 +0,0 @@
-<?php
-/*
- * Limb PHP Framework
- *
- * @link http://limb-project.com
- * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
- * @license    LGPL http://www.gnu.org/copyleft/lesser.html
- */
-
-/**
- * @tag tree:branch
- * @package macro
- * @version $Id$
- */
-class lmbMacroTreeBranchTag extends lmbMacroTag
-{
-  protected $method;
-
-  function setRecursionMethod($name)
-  {
-    $this->method = $name;
-  }
-
-  protected function _generateContent($code)
-  {
-    if(!$level = $this->parent->get('level'))
-      $level = '$level';
-
-    if(!$as = $this->parent->get('as'))
-      $as = '$item';
-
-    if(!$kids_prop = $this->parent->get('kids_prop'))
-      $kids_prop = 'kids';
-
-    $before_item = $this->_getTagsBeforeItem();
-    $after_item = $this->_getTagsAfterItem();
-    $item = $this->findImmediateChildByClass('lmbMacroTreeItemTag');
-
-    $code->writePHP('if(isset(' . $as . '["' . $kids_prop . '"])) {');
-
-    foreach($before_item as $tag)
-      $tag->generate($code);
-
-    $item->generate($code);
-
-    $code->writePHP('$this->' . $this->method . '(' . $as . '["' . $kids_prop . '"], ' . $level . ' + 1);');
-
-    foreach($after_item as $tag)
-      $tag->generate($code);
-
-    $code->writePHP('} else {');
-
-    parent :: _generateContent($code);
-
-    $code->writePHP('}');
-  }
-
-  protected function _getTagsBeforeItem()
-  {
-    $tags = array();
-    foreach($this->children as $child)
-    {
-      if(is_a($child, 'lmbMacroTreeItemTag'))
-        break;
-      $tags[] = $child;
-    }
-    return $tags;
-  }
-
-  protected function _getTagsAfterItem()
-  {
-    $tags = array();
-    $collect = false;
-    foreach($this->children as $child)
-    {
-      if($collect)
-        $tags[] = $child;
-      if(is_a($child, 'lmbMacroTreeItemTag'))
-        $collect = true;
-    }
-    return $tags;
-  }
-}
-

Deleted: 3.x/trunk/limb/macro/src/tags/tree/tree_item.tag.php
===================================================================
--- 3.x/trunk/limb/macro/src/tags/tree/tree_item.tag.php	2008-02-08 08:43:19 UTC (rev 6776)
+++ 3.x/trunk/limb/macro/src/tags/tree/tree_item.tag.php	2008-02-08 09:55:05 UTC (rev 6777)
@@ -1,18 +0,0 @@
-<?php
-/*
- * Limb PHP Framework
- *
- * @link http://limb-project.com
- * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
- * @license    LGPL http://www.gnu.org/copyleft/lesser.html
- */
-
-/**
- * @tag tree:item
- * @package macro
- * @version $Id$
- */
-class lmbMacroTreeItemTag extends lmbMacroTag
-{
-}
-

Copied: 3.x/trunk/limb/macro/src/tags/tree/tree_item.tag.php (from rev 6775, 3.x/trunk/limb/macro/src/tags/tree/tree_branch.tag.php)
===================================================================
--- 3.x/trunk/limb/macro/src/tags/tree/tree_item.tag.php	                        (rev 0)
+++ 3.x/trunk/limb/macro/src/tags/tree/tree_item.tag.php	2008-02-08 09:55:05 UTC (rev 6777)
@@ -0,0 +1,18 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+/**
+ * @tag tree:item
+ * @package macro
+ * @version $Id$
+ */
+class lmbMacroTreeItemTag extends lmbMacroTag
+{
+}
+

Copied: 3.x/trunk/limb/macro/src/tags/tree/tree_next_level.tag.php (from rev 6775, 3.x/trunk/limb/macro/src/tags/tree/tree_item.tag.php)
===================================================================
--- 3.x/trunk/limb/macro/src/tags/tree/tree_next_level.tag.php	                        (rev 0)
+++ 3.x/trunk/limb/macro/src/tags/tree/tree_next_level.tag.php	2008-02-08 09:55:05 UTC (rev 6777)
@@ -0,0 +1,43 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+/**
+ * @tag tree:nextlevel
+ * @package macro
+ * @version $Id$
+ */
+class lmbMacroTreeNextLevelTag extends lmbMacroTag
+{
+  protected function _generateContent($code)
+  {
+    parent :: _generateContent($code);
+    
+    $tree_tag = $this->findParentByClass('lmbMacroTreeTag');
+    
+    if(!$level = $tree_tag->get('level'))
+      $level = '$level';
+
+    if(!$as = $tree_tag->get('as'))
+      $as = '$item';
+
+    if(!$kids_prop = $tree_tag->get('kids_prop'))
+      $kids_prop = 'kids';
+    
+    $arg_str = $this->attributesIntoArrayString();
+
+    $code->writePHP('if(isset(' . $as . '["' . $kids_prop . '"])) {');
+    
+    $method = $tree_tag->getRecursionMethod();
+    
+    $code->writePHP('$this->' . $method . '(' . $as . '["' . $kids_prop . '"], ' . $level . ' + 1, ' . $arg_str . ");\n");
+
+    $code->writePHP('}');
+  }  
+}
+

Modified: 3.x/trunk/limb/macro/tests/cases/tags/tree/lmbMacroTreeTagTest.class.php
===================================================================
--- 3.x/trunk/limb/macro/tests/cases/tags/tree/lmbMacroTreeTagTest.class.php	2008-02-08 08:43:19 UTC (rev 6776)
+++ 3.x/trunk/limb/macro/tests/cases/tags/tree/lmbMacroTreeTagTest.class.php	2008-02-08 09:55:05 UTC (rev 6777)
@@ -13,9 +13,9 @@
   {
     $content = '{{tree using="$#tree" as="$item" kids_prop="kids"}}' . 
                   '<ul>' . 
-                  '{{tree:branch}}' . 
-                    '<li>{{tree:item}}{$item.title}{{/tree:item}}</li>' . 
-                  '{{/tree:branch}}' .
+                  '{{tree:node}}' . 
+                    '<li>{$item.title}{{tree:nextlevel/}}</li>' . 
+                  '{{/tree:node}}' .
                   '</ul>' . 
                 '{{/tree}}';
 
@@ -30,5 +30,51 @@
     $out = $macro->render();
     $this->assertEqual($out, '<ul><li>foo</li><li>bar<ul><li>bar1</li><li>bar2</li></ul></li><li>hey</li></ul>');
   }
+  
+  function testCounter()
+  {
+    $content = '{{tree using="$#tree" as="$item" kids_prop="kids" counter="$counter" prefix="1"}}' . 
+                  '<ul>' . 
+                  '{{tree:node}}' . 
+                  '<li>{$counter}){$item.title}'.
+                  '{{tree:nextlevel/}}</li>' . 
+                  '{{/tree:node}}' .
+                  '</ul>' . 
+                '{{/tree}}';
+
+    $tpl = $this->_createTemplate($content, 'tree.html');
+
+    $macro = $this->_createMacro($tpl);
+    $macro->set('tree', array(array('title' => 'foo'), 
+                              array('title' => 'bar', 'kids' => array(array('title' => 'bar1'),
+                                                                      array('title' => 'bar2'))), 
+                              array('title' => 'hey')));
+
+    $out = $macro->render();
+    $this->assertEqual($out, '<ul><li>1)foo</li><li>2)bar<ul><li>1)bar1</li><li>2)bar2</li></ul></li><li>3)hey</li></ul>');
+  }
+  
+  function testPassExtraParamsIntoTreeMethod()
+  {
+    $content = '{{tree using="$#tree" as="$item" kids_prop="kids" counter="$counter" prefix="1"}}' . 
+                  '<ul>' . 
+                  '{{tree:node}}' . 
+                  '<li>{$prefix}.{$counter}){$item.title}'.
+                  '{{tree:nextlevel prefix="$new_prefix"}}<?php $new_prefix = $prefix . "." . $counter; ?>{{/tree:nextlevel}}</li>' . 
+                  '{{/tree:node}}' .
+                  '</ul>' . 
+                '{{/tree}}';
+
+    $tpl = $this->_createTemplate($content, 'tree.html');
+
+    $macro = $this->_createMacro($tpl);
+    $macro->set('tree', array(array('title' => 'foo'), 
+                              array('title' => 'bar', 'kids' => array(array('title' => 'bar1'),
+                                                                      array('title' => 'bar2'))), 
+                              array('title' => 'hey')));
+
+    $out = $macro->render();
+    $this->assertEqual($out, '<ul><li>1.1)foo</li><li>1.2)bar<ul><li>1.2.1)bar1</li><li>1.2.2)bar2</li></ul></li><li>1.3)hey</li></ul>');
+  }
 }
 



More information about the limb-svn mailing list