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

svn at limb-project.com svn at limb-project.com
Fri Sep 7 02:21:52 MSD 2007


Author: pachanga
Date: 2007-09-07 02:21:52 +0400 (Fri, 07 Sep 2007)
New Revision: 6269
URL: http://fisheye.limb-project.com/changelog/limb/?cs=6269

Added:
   3.x/trunk/limb/macro/src/tags/
   3.x/trunk/limb/macro/src/tags/include.tag.php
   3.x/trunk/limb/macro/tests/cases/tags/
   3.x/trunk/limb/macro/tests/cases/tags/lmbMacroIncludeTagTest.class.php
Modified:
   3.x/trunk/limb/macro/src/lmbMacroBaseParsingState.class.php
   3.x/trunk/limb/macro/src/lmbMacroCodeWriter.class.php
   3.x/trunk/limb/macro/src/lmbMacroParser.class.php
   3.x/trunk/limb/macro/src/lmbMacroTemplate.class.php
   3.x/trunk/limb/macro/src/lmbMacroTemplateLocator.class.php
   3.x/trunk/limb/macro/src/lmbMacroTokenizer.class.php
   3.x/trunk/limb/macro/src/lmbMacroTokenizerListener.interface.php
   3.x/trunk/limb/macro/tests/cases/lmbMacroTokenizerTest.class.php
Log:
-- adding first <%include%> tag implementation and fixing lots of stuff(there's still a weird bug in tests)

Modified: 3.x/trunk/limb/macro/src/lmbMacroBaseParsingState.class.php
===================================================================
--- 3.x/trunk/limb/macro/src/lmbMacroBaseParsingState.class.php	2007-09-06 13:06:28 UTC (rev 6268)
+++ 3.x/trunk/limb/macro/src/lmbMacroBaseParsingState.class.php	2007-09-06 22:21:52 UTC (rev 6269)
@@ -36,9 +36,9 @@
     $this->locator = $locator;
   }
 
-  function invalidAttributeSyntax()
+  function invalidAttributeSyntax($data)
   {
-    throw Exception('Fix this...');
+    throw new lmbException('Invalid attribute syntax starting from: ' . $data);
   }
 
   function getAttributeString($attrs)

Modified: 3.x/trunk/limb/macro/src/lmbMacroCodeWriter.class.php
===================================================================
--- 3.x/trunk/limb/macro/src/lmbMacroCodeWriter.class.php	2007-09-06 13:06:28 UTC (rev 6268)
+++ 3.x/trunk/limb/macro/src/lmbMacroCodeWriter.class.php	2007-09-06 22:21:52 UTC (rev 6269)
@@ -151,7 +151,7 @@
 
   function endFunction()
   {
-    $this->writePHP(" }\n");
+    $this->writePHP("\n}\n");
   }
 
   function beginMethod($name, $param_list = array())
@@ -159,12 +159,15 @@
     $this->methods_stack[] = array($this->current_method, $this->current_mode);
     $this->current_method = $name;
 
-    return $this->beginFunction($name, $param_list);
+    //we don't need to switch to PHP, since methods can be declared inside PHP only
+    $this->writeRaw('function ' . $name . '(' . implode(',', $param_list) .") {\n");
+    $this->current_mode = self :: MODE_PHP;
+    return $name;
   }
 
   function endMethod()
   {
-    $this->writePHP(" }\n");
+    $this->writePHP("\n}\n");
     list($this->current_method, $this->current_mode) = array_pop($this->methods_stack);
   }
 

Modified: 3.x/trunk/limb/macro/src/lmbMacroParser.class.php
===================================================================
--- 3.x/trunk/limb/macro/src/lmbMacroParser.class.php	2007-09-06 13:06:28 UTC (rev 6268)
+++ 3.x/trunk/limb/macro/src/lmbMacroParser.class.php	2007-09-06 22:21:52 UTC (rev 6269)
@@ -150,9 +150,9 @@
     $this->active_parsing_state->invalidEntitySyntax($text);
   }
 
-  function invalidAttributeSyntax()
+  function invalidAttributeSyntax($text)
   {
-    $this->active_parsing_state->invalidAttributeSyntax();
+    $this->active_parsing_state->invalidAttributeSyntax($text);
   }
 }
 

Modified: 3.x/trunk/limb/macro/src/lmbMacroTemplate.class.php
===================================================================
--- 3.x/trunk/limb/macro/src/lmbMacroTemplate.class.php	2007-09-06 13:06:28 UTC (rev 6268)
+++ 3.x/trunk/limb/macro/src/lmbMacroTemplate.class.php	2007-09-06 22:21:52 UTC (rev 6269)
@@ -55,9 +55,8 @@
 
   protected function _createCompiler()
   {
-    $template_locator = new lmbMacroTemplateLocator();
     $tag_dictionary = lmbMacroTagDictionary :: instance();
-    $compiler = new lmbMacroCompiler($tag_dictionary, $template_locator);
+    $compiler = new lmbMacroCompiler($tag_dictionary, $this->locator);
     return $compiler;
   }
 }

Modified: 3.x/trunk/limb/macro/src/lmbMacroTemplateLocator.class.php
===================================================================
--- 3.x/trunk/limb/macro/src/lmbMacroTemplateLocator.class.php	2007-09-06 13:06:28 UTC (rev 6268)
+++ 3.x/trunk/limb/macro/src/lmbMacroTemplateLocator.class.php	2007-09-06 22:21:52 UTC (rev 6269)
@@ -16,10 +16,15 @@
 class lmbMacroTemplateLocator
 {
   protected $cache_dir;
+  protected $base_dir;
 
-  function __construct()
+  function __construct($base_dir = null, $cache_dir = null)
   {
-    $this->cache_dir = LIMB_VAR_DIR . '/compiled';
+    if(!$cache_dir)
+      $cache_dir = LIMB_VAR_DIR . '/compiled';
+
+    $this->cache_dir = $cache_dir;
+    $this->base_dir = $base_dir;
   }
 
   function locateCompiledTemplate($file_name)
@@ -32,11 +37,13 @@
     //fix this later
     if(lmbFs :: isPathAbsolute($file_name))
       return $file_name;
+    else
+      return $this->base_dir . '/' . $file_name;
   }
 
   function readTemplateFile($file_name)
   {    
-    return file_get_contents($file_name, 1);
+    return file_get_contents($file_name);
   }
 }
 

Modified: 3.x/trunk/limb/macro/src/lmbMacroTokenizer.class.php
===================================================================
--- 3.x/trunk/limb/macro/src/lmbMacroTokenizer.class.php	2007-09-06 13:06:28 UTC (rev 6268)
+++ 3.x/trunk/limb/macro/src/lmbMacroTokenizer.class.php	2007-09-06 22:21:52 UTC (rev 6269)
@@ -181,9 +181,8 @@
                   return;
                 }
 
-                if(strpos("% \n\r\t", $this->rawtext{$this->position}) === false)
-                  $this->observer->invalidAttributeSyntax();
-
+                if(strpos("/% \n\r\t", $this->rawtext{$this->position}) === false)
+                  $this->observer->invalidAttributeSyntax(substr($this->rawtext, $this->position));
               }
               else
               {

Modified: 3.x/trunk/limb/macro/src/lmbMacroTokenizerListener.interface.php
===================================================================
--- 3.x/trunk/limb/macro/src/lmbMacroTokenizerListener.interface.php	2007-09-06 13:06:28 UTC (rev 6268)
+++ 3.x/trunk/limb/macro/src/lmbMacroTokenizerListener.interface.php	2007-09-06 22:21:52 UTC (rev 6269)
@@ -21,7 +21,7 @@
   function characters($data);
   function unexpectedEOF($data);
   function invalidEntitySyntax($data);
-  function invalidAttributeSyntax();
+  function invalidAttributeSyntax($data);
   function setTemplateLocator($locator);
 }
 

Added: 3.x/trunk/limb/macro/src/tags/include.tag.php
===================================================================
--- 3.x/trunk/limb/macro/src/tags/include.tag.php	                        (rev 0)
+++ 3.x/trunk/limb/macro/src/tags/include.tag.php	2007-09-06 22:21:52 UTC (rev 6269)
@@ -0,0 +1,74 @@
+<?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 
+ */
+
+//temporary includes, make it more flexible later
+lmb_require('limb/macro/src/lmbMacroTagDictionary.class.php');
+lmb_require('limb/macro/src/lmbMacroTagInfo.class.php');
+lmb_require('limb/macro/src/lmbMacroTag.class.php');
+
+lmbMacroTagDictionary :: instance()->register(new lmbMacroTagInfo('include', 'lmbMacroIncludeTag'), __FILE__);
+
+/**
+ * class lmbMacroIncludeTag.
+ *
+ * @package macro
+ * @version $Id$
+ */
+class lmbMacroIncludeTag extends lmbMacroTag
+{
+  function preParse($compiler)
+  {
+    parent :: preParse($compiler);
+
+    $locator = $compiler->getTemplateLocator();
+
+    if(!$file = $this->get('file'))
+      $this->raiseRequiredAttributeError($file);
+
+    $source_file = $locator->locateSourceTemplate($file);
+    if(empty($source_file))
+      $this->raise('Template source file not found', array('file_name' => $file));
+
+    $compiler->parseTemplate($file, $this);
+  }
+
+  function generateContents($code)
+  {
+    static $counter = 1;
+
+    list($keys, $vals) = $this->_getLocalVars();
+
+    $method = $code->beginMethod('__include' . ($counter++), $keys);
+    parent :: generateContents($code);
+    $code->endMethod();
+
+    $code->writePHP('$this->' . $method . '(' . implode(', ', $vals) . ');');
+  }
+
+  protected function _getLocalVars()
+  {
+    $keys = array();
+    $vals = array();
+    foreach($this->attributes as $k => $v)
+    {
+      $keys[] = '$' . $k;
+      $vals[] = $this->_sanitizeValue($v);
+    }
+    return array($keys, $vals);
+  }
+
+  protected function _sanitizeValue($v)
+  {
+    if(is_numeric($v) || $v{0} == '$')
+      return $v;
+    else //make it smarter
+      return "'$v'";
+  }
+}
+

Modified: 3.x/trunk/limb/macro/tests/cases/lmbMacroTokenizerTest.class.php
===================================================================
--- 3.x/trunk/limb/macro/tests/cases/lmbMacroTokenizerTest.class.php	2007-09-06 13:06:28 UTC (rev 6268)
+++ 3.x/trunk/limb/macro/tests/cases/lmbMacroTokenizerTest.class.php	2007-09-06 22:21:52 UTC (rev 6269)
@@ -86,7 +86,6 @@
     $this->parser->parse('<%tag attribute=\'"\'%>');
   }
 
-
   function testEmptyClose()
   {
     $this->listener->expectOnce('endElement', array(''));
@@ -117,10 +116,18 @@
     $this->parser->parse('<%br/%>{$str}');
   }
 
+  function testSelfClosingTagWithArgumentsAndNoSpaceBeforeClosing()
+  {
+    $this->listener->expectOnce('emptyElement', array('tag', array('str' => 'abcdefgh')));
+    $this->listener->expectNever('invalidAttributeSyntax');
+    $this->parser->parse('<%tag str="abcdefgh"/%>');
+  }
+
   function testExpressionAfterTagWithArguments()
   {
     $this->listener->expectOnce('emptyElement', array('tag', array('str' => 'abcdefgh')));
     $this->listener->expectOnce('characters', array('{$str}'));
+    $this->listener->expectNever('invalidAttributeSyntax');
     $this->parser->parse('<%tag str="abcdefgh" /%>{$str}');
   }
 

Added: 3.x/trunk/limb/macro/tests/cases/tags/lmbMacroIncludeTagTest.class.php
===================================================================
--- 3.x/trunk/limb/macro/tests/cases/tags/lmbMacroIncludeTagTest.class.php	                        (rev 0)
+++ 3.x/trunk/limb/macro/tests/cases/tags/lmbMacroIncludeTagTest.class.php	2007-09-06 22:21:52 UTC (rev 6269)
@@ -0,0 +1,69 @@
+<?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 
+ */
+
+lmb_require('limb/macro/src/lmbMacroTemplate.class.php');
+lmb_require('limb/macro/src/tags/include.tag.php');
+lmb_require('limb/fs/src/lmbFs.class.php');
+
+class lmbMacroTagIncludeTest extends UnitTestCase
+{
+  function setUp()
+  {
+    lmbFs :: rm(LIMB_VAR_DIR . '/tpl');
+    lmbFs :: mkdir(LIMB_VAR_DIR . '/tpl/compiled');
+  }
+
+  function testSimpleInclude()
+  {
+    $bar = '<body><%include file="foo.html"/%></body>';
+    $foo = '<p>Hello, <%include file="name.html"/%></p>';
+    $name = "Bob";
+
+    $bar_tpl = $this->_createTemplate($bar, 'bar.html');
+    $foo_tpl = $this->_createTemplate($foo, 'foo.html');
+    $name_tpl = $this->_createTemplate($name, 'name.html');
+
+    $macro = $this->_createMacro($bar_tpl);
+
+    $out = $macro->render();
+    $this->assertEqual($out, '<body><p>Hello, Bob</p></body>');
+  }
+
+  function testIncludePassVariables()
+  {
+    $bar = '<body><?php $var2=2;?><%include file="foo.html" var1="1" var2="$var2"/%></body>';
+    $foo = '<p>Numbers: <?=$var1?> <?=$var2?></p>';
+
+    $bar_tpl = $this->_createTemplate($bar, 'bar.html');
+    $foo_tpl = $this->_createTemplate($foo, 'foo.html');
+
+    $macro = $this->_createMacro($bar_tpl);
+
+    $out = $macro->render();
+    $this->assertEqual($out, '<body><p>Numbers: 1 2</p></body>');
+  }
+
+  protected function _createMacro($file)
+  {
+    $base_dir = LIMB_VAR_DIR . '/tpl';
+    $cache_dir = LIMB_VAR_DIR . '/tpl/compiled';
+    $macro = new lmbMacroTemplate($file,
+                                  $cache_dir,
+                                  new lmbMacroTemplateLocator($base_dir, $cache_dir));
+    return $macro;
+  }
+
+  protected function _createTemplate($code, $name)
+  {
+    $file = LIMB_VAR_DIR . '/tpl/' . $name;
+    file_put_contents($file, $code);
+    return $file;
+  }
+}
+



More information about the limb-svn mailing list