[limb-svn] r6256 - in 3.x/trunk/limb/macro: src tests/cases

svn at limb-project.com svn at limb-project.com
Sun Sep 2 23:00:07 MSD 2007


Author: pachanga
Date: 2007-09-02 23:00:06 +0400 (Sun, 02 Sep 2007)
New Revision: 6256
URL: http://fisheye.limb-project.com/changelog/limb/?cs=6256

Added:
   3.x/trunk/limb/macro/src/lmbMacroTemplateExecutor.class.php
Modified:
   3.x/trunk/limb/macro/src/lmbMacroCodeWriter.class.php
   3.x/trunk/limb/macro/src/lmbMacroCompiler.class.php
   3.x/trunk/limb/macro/src/lmbMacroTemplate.class.php
   3.x/trunk/limb/macro/tests/cases/lmbMacroCodeWriterTest.class.php
   3.x/trunk/limb/macro/tests/cases/lmbMacroTemplateTest.class.php
Log:
-- making all initial tests pass

Modified: 3.x/trunk/limb/macro/src/lmbMacroCodeWriter.class.php
===================================================================
--- 3.x/trunk/limb/macro/src/lmbMacroCodeWriter.class.php	2007-09-01 14:53:36 UTC (rev 6255)
+++ 3.x/trunk/limb/macro/src/lmbMacroCodeWriter.class.php	2007-09-02 19:00:06 UTC (rev 6256)
@@ -22,12 +22,9 @@
 
   protected $code = '';
 
-  protected $function_prefix = '';
-  protected $function_suffix = 1;
-
   protected $include_list = array();
 
-  protected $tempVarName = 1;
+  protected $temp_var_name = 1;
 
   function reset()
   {
@@ -137,16 +134,10 @@
     return $this->include_list;
   }
 
-  /**
-  * Begins writing a PHP function to the compiled template, using the
-  * function_prefix and the function_suffix, the latter being post incremented
-  * by one.
-  */
-  function beginFunction($param_list)
+  function beginFunction($name, $param_list = array())
   {
-      $func_name = 'tpl' . $this->function_prefix . $this->function_suffix++;
-      $this->writePHP('function ' . $func_name . $param_list ." {\n");
-      return $func_name;
+      $this->writePHP('function ' . $name . '(' . implode(',', $param_list) .") {\n");
+      return $name;
   }
 
   function endFunction()
@@ -154,17 +145,23 @@
     $this->writePHP(" }\n");
   }
 
-  function setFunctionPrefix($prefix)
+  function beginClass($name, $parent = null)
   {
-    $this->function_prefix = $prefix;
+    $this->writePHP("class $name " . ($parent ? "extends $parent " : '') . "{\n");
+    return $name;
   }
 
+  function endClass()
+  {
+    $this->writePHP(" }\n");
+  }
+
   /**
   * Utility method, which generates a unique variable name
   */
   function getTempVariable()
   {
-    $var = $this->tempVarName++;
+    $var = $this->temp_var_name++;
     if($var > 675)
       return chr(65 + ($var/26)/26) . chr(65 + ($var/26)%26) . chr(65 + $var%26);
     elseif($var > 26)

Modified: 3.x/trunk/limb/macro/src/lmbMacroCompiler.class.php
===================================================================
--- 3.x/trunk/limb/macro/src/lmbMacroCompiler.class.php	2007-09-01 14:53:36 UTC (rev 6255)
+++ 3.x/trunk/limb/macro/src/lmbMacroCompiler.class.php	2007-09-02 19:00:06 UTC (rev 6256)
@@ -7,8 +7,6 @@
  * @license    LGPL http://www.gnu.org/copyleft/lesser.html 
  */
 
-lmb_require('limb/macro/src/lmbMacroTagDictionary.class.php');
-lmb_require('limb/macro/src/lmbMacroTemplateLocator.class.php');
 lmb_require('limb/macro/src/lmbMacroTreeBuilder.class.php');
 lmb_require('limb/macro/src/lmbMacroNode.class.php');
 lmb_require('limb/macro/src/lmbMacroParser.class.php');
@@ -43,16 +41,10 @@
   protected $tag_dictionary;
 
 
-  function __construct($tag_dictionary = null, $template_locator = null)
+  function __construct($tag_dictionary, $template_locator)
   {
     $this->template_locator = $template_locator;
-    if(!$this->template_locator)
-      $this->template_locator = new lmbMacroTemplateLocator();
-
-    $this->tag_dictionary = $tag_dictionary;
-    if(!$this->tag_dictionary)
-      $this->tag_dictionary = lmbMacroTagDictionary :: instance();
-
+    $this->tag_dictionary = lmbMacroTagDictionary :: instance();
     $this->tree_builder = new lmbMacroTreeBuilder($this);
   }
 
@@ -66,19 +58,21 @@
     $root_node->prepare();
 
     $compiled_file_path = $this->template_locator->locateCompiledTemplate($file_name);
-    list($render_func, $generated_code) = $this->_generateTemplateCode(md5($compiled_file_path), $root_node);
+    list($class, $render_func, $generated_code) = $this->_generateTemplateCode(md5($compiled_file_path), $root_node);
     self :: writeFile($compiled_file_path, $generated_code);
-    return array($render_func, $compiled_file_path);
+    return array($class, $render_func, $compiled_file_path);
   }
 
   function _generateTemplateCode($prefix, $root_node)
   {
     $code_writer = new lmbMacroCodeWriter();
-    $code_writer->setFunctionPrefix($prefix);
-    $render_func = $code_writer->beginFunction('($root)');
+    $code_writer->registerInclude('limb/macro/src/lmbMacroTemplateExecutor.class.php');
+    $class = $code_writer->beginClass('TemplateExecutor' . $prefix, 'lmbMacroTemplateExecutor');
+    $render_func = $code_writer->beginFunction('render');
     $root_node->generate($code_writer);
     $code_writer->endFunction();
-    return array($render_func, $code_writer->renderCode());
+    $code_writer->endClass();
+    return array($class, $render_func, $code_writer->renderCode());
   }
 
   function parseTemplate($source_file_path, $root_node)

Modified: 3.x/trunk/limb/macro/src/lmbMacroTemplate.class.php
===================================================================
--- 3.x/trunk/limb/macro/src/lmbMacroTemplate.class.php	2007-09-01 14:53:36 UTC (rev 6255)
+++ 3.x/trunk/limb/macro/src/lmbMacroTemplate.class.php	2007-09-02 19:00:06 UTC (rev 6256)
@@ -9,6 +9,7 @@
 
 lmb_require('limb/macro/src/lmbMacroTemplateLocator.class.php');
 lmb_require('limb/macro/src/lmbMacroCompiler.class.php');
+lmb_require('limb/macro/src/lmbMacroTagDictionary.class.php');
 
 /**
  * class lmbMacroTemplate.
@@ -40,19 +41,24 @@
   {
     ob_start();
 
-    $compiler = new lmbMacroCompiler();
-    list($func, $compiled_file) = $compiler->compile($this->file);
+    $compiler = $this->_createCompiler();
+    list($class, $func, $compiled_file) = $compiler->compile($this->file);
 
     include_once($compiled_file);
-    $func($this);
+    $executor = new $class($this->vars);
+    $executor->$func();
 
     $out = ob_get_contents();
     ob_end_clean();
     return $out;
   }
 
-  function createCompiler()
+  protected function _createCompiler()
   {
+    $template_locator = new lmbMacroTemplateLocator();
+    $tag_dictionary = lmbMacroTagDictionary :: instance();
+    $compiler = new lmbMacroCompiler($tag_dictionary, $template_locator);
+    return $compiler;
   }
 }
 

Added: 3.x/trunk/limb/macro/src/lmbMacroTemplateExecutor.class.php
===================================================================
--- 3.x/trunk/limb/macro/src/lmbMacroTemplateExecutor.class.php	                        (rev 0)
+++ 3.x/trunk/limb/macro/src/lmbMacroTemplateExecutor.class.php	2007-09-02 19:00:06 UTC (rev 6256)
@@ -0,0 +1,31 @@
+<?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 
+ */
+
+/**
+ * class lmbMacroTemplateExecutor.
+ *
+ * @package macro
+ * @version $Id$
+ */
+class lmbMacroTemplateExecutor
+{
+  function __construct($vars)
+  {
+    foreach($vars as $name => $value)
+      $this->name = $value;
+  }
+
+  function set($name, $value)
+  {
+    $this->$name = $value;
+  }
+
+  function render(){}
+}
+

Modified: 3.x/trunk/limb/macro/tests/cases/lmbMacroCodeWriterTest.class.php
===================================================================
--- 3.x/trunk/limb/macro/tests/cases/lmbMacroCodeWriterTest.class.php	2007-09-01 14:53:36 UTC (rev 6255)
+++ 3.x/trunk/limb/macro/tests/cases/lmbMacroCodeWriterTest.class.php	2007-09-02 19:00:06 UTC (rev 6256)
@@ -64,11 +64,23 @@
     $this->assertEqual($this->writer->renderCode(), '');
   }
 
+  function testBeginClass()
+  {
+    $this->writer->beginClass('Foo', 'Bar');
+    $this->assertEqual($this->writer->renderCode(), "<?php class Foo extends Bar {\n ?>");
+  }
+
+  function testEndClass()
+  {
+    $this->writer->endClass();
+    $this->assertEqual($this->writer->renderCode(),'<?php '." }\n".' ?>');
+  }
+
   function testBeginFunction()
   {
-    $params = '($a,$b,$c)';
-    $this->writer->beginFunction($params);
-    $this->assertEqual($this->writer->renderCode(),'<?php function tpl1'.$params ." {\n ?>");
+    $params = array('$a', '$b', '$c');
+    $this->writer->beginFunction('tpl1', $params);
+    $this->assertEqual($this->writer->renderCode(),"<?php function tpl1(\$a,\$b,\$c) {\n ?>");
   }
 
   function testEndFunction()
@@ -77,14 +89,6 @@
     $this->assertEqual($this->writer->renderCode(),'<?php '." }\n".' ?>');
   }
 
-  function testSetFunctionPrefix()
-  {
-    $this->writer->setFunctionPrefix('Test');
-    $params = '($a,$b,$c)';
-    $this->writer->beginFunction($params);
-    $this->assertEqual($this->writer->renderCode(),'<?php function tplTest1'.$params ." {\n ?>");
-  }
-
   function testGetTempVariable()
   {
     $var = $this->writer->getTempVariable();

Modified: 3.x/trunk/limb/macro/tests/cases/lmbMacroTemplateTest.class.php
===================================================================
--- 3.x/trunk/limb/macro/tests/cases/lmbMacroTemplateTest.class.php	2007-09-01 14:53:36 UTC (rev 6255)
+++ 3.x/trunk/limb/macro/tests/cases/lmbMacroTemplateTest.class.php	2007-09-02 19:00:06 UTC (rev 6256)
@@ -21,32 +21,11 @@
 
   function testRenderTemplateVar()
   {
-    $view = $this->_createView('Hello, <?$name = "Jack"?><?=@$name?>');
+    $view = $this->_createView('Hello, <?=$this->name?>');
     $view->set('name', 'Bob');
     $this->assertEqual($view->render(), 'Hello, Bob');
   }
 
-  function testRenderLocalVar()
-  {
-    $view = $this->_createView('Hello, <?$name = "Jack"?><?=$name?>');
-    $view->set('name', 'Bob');
-    $this->assertEqual($view->render(), 'Hello, Jack');
-  }
-
-  function testEchoVarSyntaxSugar()
-  {
-    $view = $this->_createView('Hello, <?$name = "Jack"?>{$name}');
-    $view->set('name', 'Bob');
-    $this->assertEqual($view->render(), 'Hello, Jack');
-  }
-
-  function testEchoFunctionSyntaxSugar()
-  {
-    $rnd = mt_rand();
-    $view = $this->_createView("Hello, <?function f_$rnd(){return 'Jack';}?>{f_$rnd()}");
-    $this->assertEqual($view->render(), 'Hello, Jack');
-  }
-
   function _createView($tpl)
   {
     $file = $this->_createTemplate($tpl);



More information about the limb-svn mailing list