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

svn at limb-project.com svn at limb-project.com
Thu Sep 20 00:42:01 MSD 2007


Author: pachanga
Date: 2007-09-20 00:42:00 +0400 (Thu, 20 Sep 2007)
New Revision: 6317
URL: http://fisheye.limb-project.com/changelog/limb/?cs=6317

Added:
   3.x/trunk/limb/macro/tests/cases/lmbMacroTagInfoTest.class.php
Modified:
   3.x/trunk/limb/macro/src/lmbMacroTagInfo.class.php
   3.x/trunk/limb/macro/src/tags/include.tag.php
   3.x/trunk/limb/macro/src/tags/wrap.tag.php
   3.x/trunk/limb/macro/tests/cases/lmbMacroTagDictionaryTest.class.php
Log:
-- adding initial infrastructure for describing tags with annotations and adding them into dictionary

Modified: 3.x/trunk/limb/macro/src/lmbMacroTagInfo.class.php
===================================================================
--- 3.x/trunk/limb/macro/src/lmbMacroTagInfo.class.php	2007-09-19 17:51:10 UTC (rev 6316)
+++ 3.x/trunk/limb/macro/src/lmbMacroTagInfo.class.php	2007-09-19 20:42:00 UTC (rev 6317)
@@ -6,6 +6,8 @@
  * @copyright  Copyright © 2004-2007 BIT(http://bit-creative.com)
  * @license    LGPL http://www.gnu.org/copyleft/lesser.html 
  */
+lmb_require('limb/core/src/lmbPHPTokenizer.class.php');
+lmb_require('limb/macro/src/lmbMacroException.class.php');
 
 /**
  * class lmbMacroTagInfo.
@@ -21,15 +23,64 @@
   protected $req_attributes = array();
   protected $parent_class;
   protected $restrict_self_nesting = false;
-  protected $forbid_parsing = false;
-  protected $forbid_endtag = false;
+  protected $require_endtag = true;
 
-  function __construct($tag, $class, $forbid_endtag = false)
+  function __construct($tag, $class, $require_endtag = true)
   {
     $this->tag = $tag;
     $this->class = $class;
-    $this->forbid_endtag = $forbid_endtag;
+    $this->require_endtag = $require_endtag;
   }
+
+  static function extractFromFile($file)
+  {
+    $infos = array();
+    $tokenizer = new lmbPHPTokenizer(file_get_contents($file));
+    while($token = $tokenizer->next())
+    {
+      if(!is_array($token))
+        continue;
+
+      //found class token
+      if($token[0] == T_CLASS)
+      {
+        //fetching class name
+        $token = $tokenizer->next();
+        $class = $token[1]; 
+
+        //now checking prev token for /**/
+        if(!is_array($prev_token) || $prev_token[0] != T_DOC_COMMENT)
+          throw new lmbMacroException('Invalid token, doc comment is expected');
+
+        //now parsing annotations
+        $annotations = self :: _extractAnnotations($prev_token[1]);
+        if(!$annotations)
+          throw new lmbMacroException("No annotations found in doc comment '{$prev_token[1]}' in file $file");
+
+        $infos[] = self :: createByAnnotations($class, $annotations);
+      }
+
+      $prev_token = $token;
+    }
+
+    return $infos;
+  }
+
+  static function createByAnnotations($class, $annotations)
+  {
+    $tag = $annotations['tag'];
+    return new lmbMacroTagInfo($class, $tag);
+  }
+
+  static protected function _extractAnnotations($content)
+  {
+    if(!preg_match_all('~@(\S+)([^\n]+)?\n~', $content, $matches))
+      return false;
+    $annotations = array();
+    for($i=0;$i<count($matches[0]);$i++)
+      $annotations[trim($matches[1][$i])] = trim($matches[2][$i]);
+    return $annotations;
+  }
   
   function getTag()
   {
@@ -48,12 +99,12 @@
 
   function setForbidEndtag($flag = true)
   {
-    $this->forbid_endtag = $flag;
+    $this->require_endtag = !$flag;
   }
 
   function isEndtagForbidden()
   {
-    return $this->forbid_endtag;
+    return !$this->require_endtag;
   }
 
   function setRequiredAttributes($attributes)

Modified: 3.x/trunk/limb/macro/src/tags/include.tag.php
===================================================================
--- 3.x/trunk/limb/macro/src/tags/include.tag.php	2007-09-19 17:51:10 UTC (rev 6316)
+++ 3.x/trunk/limb/macro/src/tags/include.tag.php	2007-09-19 20:42:00 UTC (rev 6317)
@@ -12,7 +12,7 @@
 lmb_require('limb/macro/src/lmbMacroTagInfo.class.php');
 lmb_require('limb/macro/src/lmbMacroTag.class.php');
 
-lmbMacroTagDictionary :: instance()->register(new lmbMacroTagInfo('include', 'lmbMacroIncludeTag', true), __FILE__);
+lmbMacroTagDictionary :: instance()->register(new lmbMacroTagInfo('include', 'lmbMacroIncludeTag', false), __FILE__);
 
 /**
  * class lmbMacroIncludeTag.

Modified: 3.x/trunk/limb/macro/src/tags/wrap.tag.php
===================================================================
--- 3.x/trunk/limb/macro/src/tags/wrap.tag.php	2007-09-19 17:51:10 UTC (rev 6316)
+++ 3.x/trunk/limb/macro/src/tags/wrap.tag.php	2007-09-19 20:42:00 UTC (rev 6317)
@@ -12,7 +12,7 @@
 lmb_require('limb/macro/src/lmbMacroTagInfo.class.php');
 lmb_require('limb/macro/src/lmbMacroTag.class.php');
 
-lmbMacroTagDictionary :: instance()->register(new lmbMacroTagInfo('wrap', 'lmbMacroWrapTag', false), __FILE__);
+lmbMacroTagDictionary :: instance()->register(new lmbMacroTagInfo('wrap', 'lmbMacroWrapTag', true), __FILE__);
 
 /**
  * class lmbMacroWrapTag.

Modified: 3.x/trunk/limb/macro/tests/cases/lmbMacroTagDictionaryTest.class.php
===================================================================
--- 3.x/trunk/limb/macro/tests/cases/lmbMacroTagDictionaryTest.class.php	2007-09-19 17:51:10 UTC (rev 6316)
+++ 3.x/trunk/limb/macro/tests/cases/lmbMacroTagDictionaryTest.class.php	2007-09-19 20:42:00 UTC (rev 6317)
@@ -13,25 +13,19 @@
 
 class lmbMacroTagDictionaryTest extends UnitTestCase
 {
-  protected $dictionary;
-  protected $tag_info;
-
   function setUp()
   {
-    $this->tag_info = new lmbMacroTagInfo('testtag', 'SomeTagClass');
-    $this->dictionary = new lmbMacroTagDictionary();
-    $this->dictionary->register($this->tag_info, $file = 'whaever');
+    lmbFs :: rm(LIMB_VAR_DIR . '/tags/');
+    lmbFs :: mkdir(LIMB_VAR_DIR . '/tags/');
   }
 
-  protected function _createParentTag()
+  function testFindTagInfo()
   {
-    $tag_info = new lmbMacroTagInfo('some_tag', 'SomeTagClass');
-    return new lmbMacroTag(new lmbMacroSourceLocation('file', '10'), 'some_tag', $tag_info);
-  }
+    $tag_info = new lmbMacroTagInfo('testtag', 'SomeTagClass');
+    $dictionary = new lmbMacroTagDictionary();
+    $dictionary->register($tag_info, $file = 'whatever');
 
-  function testFindTagInfo()
-  {
-    $this->assertIsA($this->dictionary->findTagInfo('testtag'), 'lmbMacroTagInfo');
+    $this->assertIsA($dictionary->findTagInfo('testtag'), 'lmbMacroTagInfo');
   }
 
   function testRegisterTagInfoOnceOnly()
@@ -39,18 +33,46 @@
     $dictionary = new lmbMacroTagDictionary();
     $tag_info1 = new lmbMacroTagInfo('some_tag', 'SomeTagClass');
     $tag_info2 = new lmbMacroTagInfo('some_tag', 'SomeTagClass');
-    $dictionary->register($tag_info1, $file1 = 'whaever1');
-    $dictionary->register($tag_info2, $file2 = 'whaever2');
+    $dictionary->register($tag_info1, $file1 = 'whatever1');
+    $dictionary->register($tag_info2, $file2 = 'whatever2');
 
     $this->assertEqual($dictionary->findTagInfo('some_tag'), $tag_info1);
   }
 
-  function testNotATag()
+  function testTagNotFound()
   {
-    $parent = $this->_createParentTag();
-    $tag = 'notatag';
-    $attrs = array();
-    $this->assertFalse($this->dictionary->findTagInfo($tag, $attrs, FALSE, $parent));
+    $tag_info = new lmbMacroTagInfo('testtag', 'SomeTagClass');
+    $dictionary = new lmbMacroTagDictionary();
+    $dictionary->register($tag_info, $file = 'whatever');
+
+    $this->assertFalse($dictionary->findTagInfo('junk'));
   }
+
+  function _testRegisterFromFile()
+  {
+    $rnd = mt_rand();
+    $contents = <<<EOD
+<?php
+/**
+ * @tag foo_{$rnd}
+ */
+class Foo{$rnd}Tag extends lmbMacroTag{}
+
+/**
+ * @tag bar_{$rnd}
+ */
+class Bar{$rnd}Tag extends lmbMacroTag{}
+EOD;
+    file_put_contents($file = LIMB_VAR_DIR . '/tags/' . $rnd . '.tag.php', $contents);
+
+    $tag_info1 = new lmbMacroTagInfo("foo_$rnd", "Foo{$rnd}Tag");
+    $tag_info2 = new lmbMacroTagInfo("bar_$rnd", "Bar{$rnd}Tag");
+
+    $dictionary = new lmbMacroTagDictionary();
+    $dictionary->registerFromFile($file);
+
+    $this->assertEqual($dictionary->findTagInfo("foo_$rnd"), $tag_info1);
+    $this->assertEqual($dictionary->findTagInfo("bar_$rnd"), $tag_info2);
+  }
 }
 

Added: 3.x/trunk/limb/macro/tests/cases/lmbMacroTagInfoTest.class.php
===================================================================
--- 3.x/trunk/limb/macro/tests/cases/lmbMacroTagInfoTest.class.php	                        (rev 0)
+++ 3.x/trunk/limb/macro/tests/cases/lmbMacroTagInfoTest.class.php	2007-09-19 20:42:00 UTC (rev 6317)
@@ -0,0 +1,63 @@
+<?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/fs/src/lmbFs.class.php');
+lmb_require('limb/macro/src/lmbMacroTagInfo.class.php');
+
+class lmbMacroTagInfoTest extends UnitTestCase
+{
+  function setUp()
+  {
+    lmbFs :: rm(LIMB_VAR_DIR . '/tags/');
+    lmbFs :: mkdir(LIMB_VAR_DIR . '/tags/');
+  }
+
+  function testExtractOneFromFile()
+  {
+    $rnd = mt_rand();
+    $contents = <<<EOD
+<?php
+/**
+ * @tag foo_{$rnd}
+ */
+class Foo{$rnd}Tag extends lmbMacroTag{}
+EOD;
+    file_put_contents($file = LIMB_VAR_DIR . '/tags/' . $rnd . '.tag.php', $contents);
+
+    $info = lmbMacroTagInfo :: extractFromFile($file);
+
+    $this->assertEqual(sizeof($info), 1);
+    $this->assertEqual($info[0], new lmbMacroTagInfo("Foo{$rnd}Tag", "foo_$rnd"));
+  }
+
+  function testExtractSeveralFromFile()
+  {
+    $rnd = mt_rand();
+    $contents = <<<EOD
+<?php
+/**
+ * @tag foo_{$rnd}
+ */
+class Foo{$rnd}Tag extends lmbMacroTag{}
+
+/**
+ * @tag bar_{$rnd}
+ */
+class Bar{$rnd}Tag extends lmbMacroTag{}
+EOD;
+    file_put_contents($file = LIMB_VAR_DIR . '/tags/' . $rnd . '.tag.php', $contents);
+
+    $info = lmbMacroTagInfo :: extractFromFile($file);
+
+    $this->assertEqual(sizeof($info), 2);
+    $this->assertEqual($info[0], new lmbMacroTagInfo("Foo{$rnd}Tag", "foo_$rnd"));
+    $this->assertEqual($info[1], new lmbMacroTagInfo("Bar{$rnd}Tag", "bar_$rnd"));
+  }
+}
+



More information about the limb-svn mailing list