[limb-svn] r5785 - in 3.x/trunk/limb/js: . src tests tests/cases

svn at limb-project.com svn at limb-project.com
Tue May 1 12:38:46 MSD 2007


Author: tony
Date: 2007-05-01 12:38:46 +0400 (Tue, 01 May 2007)
New Revision: 5785
URL: http://fisheye.limb-project.com/changelog/limb/?cs=5785

Added:
   3.x/trunk/limb/js/common.inc.php
   3.x/trunk/limb/js/src/
   3.x/trunk/limb/js/src/lmbFilesBundler.class.php
   3.x/trunk/limb/js/src/lmbJsDependencyExtractor.class.php
   3.x/trunk/limb/js/tests/
   3.x/trunk/limb/js/tests/cases/
   3.x/trunk/limb/js/tests/cases/.init.php
   3.x/trunk/limb/js/tests/cases/FilesBundlerTest.class.php
Log:
-- lmbFilesBundler and lmbJsDependencyExtractor ported from 2.x branch

Added: 3.x/trunk/limb/js/common.inc.php
===================================================================
--- 3.x/trunk/limb/js/common.inc.php	                        (rev 0)
+++ 3.x/trunk/limb/js/common.inc.php	2007-05-01 08:38:46 UTC (rev 5785)
@@ -0,0 +1,14 @@
+<?php
+/**
+ * Limb Web Application Framework
+ *
+ * @link http://limb-project.com
+ *
+ * @copyright  Copyright &copy; 2004-2007 BIT
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
+ * @version    $Id: common.inc.php 4997 2007-02-08 15:36:22Z pachanga $
+ * @package    filter_chain
+ */
+require_once('limb/core/common.inc.php');
+
+?>
\ No newline at end of file

Added: 3.x/trunk/limb/js/src/lmbFilesBundler.class.php
===================================================================
--- 3.x/trunk/limb/js/src/lmbFilesBundler.class.php	                        (rev 0)
+++ 3.x/trunk/limb/js/src/lmbFilesBundler.class.php	2007-05-01 08:38:46 UTC (rev 5785)
@@ -0,0 +1,122 @@
+<?php
+/**
+ * Limb Web Application Framework
+ *
+ * @link http://limb-project.com
+ *
+ * @copyright  Copyright &copy; 2004-2007 BIT
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
+ * @version    $Id$
+ * @package    js
+ */
+lmb_require('limb/fs/src/lmbFs.class.php');
+
+class lmbFilesBundler
+{
+  var $extractor;
+  var $regex_match;
+  var $root_files = array();
+  var $bundled = array();
+  var $file_stack = array();
+  var $filters = array();
+
+  function __construct($extractor = null)
+  {
+    $this->setDependecyExtractor($extractor);
+  }
+
+  function addRootFile($file)
+  {
+    $this->root_files[] = $file;
+  }
+
+  function setDependecyExtractor($extractor)
+  {
+    $this->extractor = $extractor;
+  }
+
+  function addFilter($filter)
+  {
+    $this->filters[] = $filter;
+  }
+
+  function createBundle()
+  {
+    $contents = '';
+    $this->bundled = array();
+
+    foreach($this->root_files as $root_file)
+      $contents .= $this->_collectDependencies($root_file) . "\n";
+
+    $this->_applyFilters($contents);
+    return $contents;
+  }
+
+  function createBundleFile($bundle_file)
+  {
+    lmbFs :: mkdir(dirname($bundle_file));
+    lmbFs :: safeWrite($bundle_file, $this->createBundle());
+  }
+
+  protected function _applyFilters(&$contents)
+  {
+    foreach(array_keys($this->filters) as $key)
+      $this->filters[$key]->apply($contents);
+  }
+
+  protected function _collectDependencies($file)
+  {
+    $file = lmbFs :: normalizePath($file);
+
+    if($this->_isBundled($file))
+      return '';
+
+    $contents = file_get_contents($file);
+    $this->_markAsBundled($file);
+    $this->_pushCurrentFile($file);
+
+    $result = $contents;
+    if($this->extractor)
+    {
+      $result = preg_replace_callback($this->extractor->getRegex() . 'm',
+                                 array(&$this, '_replaceBundleEntry'),
+                                 $contents);
+    }
+
+    $this->_popCurrentFile();
+    return $result;
+  }
+
+  protected function _replaceBundleEntry($matches)
+  {
+    $dependency = $this->extractor->extractDependency($matches);
+    return trim($this->_collectDependencies($dependency));
+  }
+
+  protected function _markAsBundled($file)
+  {
+    $this->bundled[$file] = 1;
+  }
+
+  protected function _isBundled($file)
+  {
+    return isset($this->bundled[$file]);
+  }
+
+  protected function _pushCurrentFile($file)
+  {
+    $this->file_stack[] =  $file;
+  }
+
+  protected function _popCurrentFile()
+  {
+    array_pop($this->file_stack);
+  }
+
+  protected function _getCurrentFile()
+  {
+    return end($this->file_stack);
+  }
+}
+
+?>

Added: 3.x/trunk/limb/js/src/lmbJsDependencyExtractor.class.php
===================================================================
--- 3.x/trunk/limb/js/src/lmbJsDependencyExtractor.class.php	                        (rev 0)
+++ 3.x/trunk/limb/js/src/lmbJsDependencyExtractor.class.php	2007-05-01 08:38:46 UTC (rev 5785)
@@ -0,0 +1,33 @@
+<?php
+/**
+ * Limb Web Application Framework
+ *
+ * @link http://limb-project.com
+ *
+ * @copyright  Copyright &copy; 2004-2007 BIT
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
+ * @version    $Id$
+ * @package    js
+ */
+
+class lmbJsDependencyExtractor
+{
+  var $base_dir;
+
+  function __construct($base_dir)
+  {
+    $this->base_dir = $base_dir;
+  }
+
+  function getRegex()
+  {
+    return "~^Limb\.require\(('|\")([^'\"]+)('|\")\)\s*;?~";
+  }
+
+  function extractDependency($matches)
+  {
+    return $this->base_dir . '/' . str_replace('.', '/', $matches[2]) . '.js';
+  }
+}
+
+?>

Added: 3.x/trunk/limb/js/tests/cases/.init.php
===================================================================
--- 3.x/trunk/limb/js/tests/cases/.init.php	                        (rev 0)
+++ 3.x/trunk/limb/js/tests/cases/.init.php	2007-05-01 08:38:46 UTC (rev 5785)
@@ -0,0 +1,4 @@
+<?php
+require_once(dirname(__FILE__) . '/../../common.inc.php');
+
+?>

Added: 3.x/trunk/limb/js/tests/cases/FilesBundlerTest.class.php
===================================================================
--- 3.x/trunk/limb/js/tests/cases/FilesBundlerTest.class.php	                        (rev 0)
+++ 3.x/trunk/limb/js/tests/cases/FilesBundlerTest.class.php	2007-05-01 08:38:46 UTC (rev 5785)
@@ -0,0 +1,233 @@
+<?php
+/**
+ * Limb Web Application Framework
+ *
+ * @link http://limb-project.com
+ *
+ * @copyright  Copyright &copy; 2004-2007 BIT
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
+ * @version    $Id$
+ * @package    js
+ */
+lmb_require('limb/js/src/lmbFilesBundler.class.php');
+lmb_require('limb/js/src/lmbJsDependencyExtractor.class.php');
+lmb_require('limb/fs/src/lmbFs.class.php');
+
+class BundleStubFilter
+{
+  function BundleStubFilter($wrap)
+  {
+    $this->wrap = $wrap;
+  }
+
+  function apply(&$content)
+  {
+    $content = $this->wrap . $content . $this->wrap;
+  }
+}
+
+class FilesBundlerTest extends UnitTestCase
+{
+  var $created_files = array();
+
+  function setUp()
+  {
+    $this->_cleanup();
+  }
+
+  function tearDown()
+  {
+    $this->_cleanup();
+  }
+
+  function testSimpleBundle()
+  {
+    $this->_createFile($root_file = LIMB_VAR_DIR . '/foo.js',
+<<<EOD
+Limb.require('bar');
+foo
+EOD
+);
+
+    $this->_createFile(LIMB_VAR_DIR . '/bar.js',
+<<<EOD
+bar
+
+EOD
+);
+
+    $bundler = new lmbFilesBundler(new lmbJsDependencyExtractor(LIMB_VAR_DIR));
+    $bundler->addRootFile($root_file);
+    $this->assertEqual($bundler->createBundle(),
+<<<EOD
+bar
+foo
+
+EOD
+);
+  }
+
+  function testCreateBundleFile()
+  {
+    $this->_createFile($root_file = LIMB_VAR_DIR . '/foo.js',
+<<<EOD
+Limb.require('bar');
+foo
+EOD
+);
+
+    $this->_createFile(LIMB_VAR_DIR . '/bar.js',
+<<<EOD
+bar
+EOD
+);
+
+    $bundle_file = LIMB_VAR_DIR . '/bundle';
+
+    $bundler = new lmbFilesBundler(new lmbJsDependencyExtractor(LIMB_VAR_DIR));
+    $bundler->addRootFile($root_file);
+    $bundler->createBundleFile($bundle_file);
+    $this->assertEqual(file_get_contents($bundle_file),
+<<<EOD
+bar
+foo
+
+EOD
+);
+    unlink($bundle_file);
+  }
+
+  function testBundleSeveralRoots()
+  {
+    $this->_createFile($root_file1 = LIMB_VAR_DIR . '/foo.js',
+<<<EOD
+Limb.require('bar');
+foo
+EOD
+);
+
+    $this->_createFile($root_file2 = LIMB_VAR_DIR . '/bar.js',
+<<<EOD
+bar
+EOD
+);
+
+    $bundler = new lmbFilesBundler(new lmbJsDependencyExtractor(LIMB_VAR_DIR));
+    $bundler->addRootFile($root_file1);
+    $bundler->addRootFile($root_file2);
+    $this->assertEqual($bundler->createBundle(),
+<<<EOD
+bar
+foo
+
+
+EOD
+);
+  }
+
+  function testCyclicBundle()
+  {
+    $this->_createFile($root_file = LIMB_VAR_DIR . '/foo.js',
+<<<EOD
+Limb.require('bar');
+foo
+EOD
+);
+
+    $this->_createFile(LIMB_VAR_DIR . '/bar.js',
+<<<EOD
+Limb.require('foo');
+bar
+EOD
+);
+
+    $bundler = new lmbFilesBundler(new lmbJsDependencyExtractor(LIMB_VAR_DIR));
+    $bundler->addRootFile($root_file);
+    $this->assertEqual($bundler->createBundle(),
+<<<EOD
+bar
+foo
+
+EOD
+);
+  }
+
+  function testDeeperBundle()
+  {
+    $this->_createFile($root_file = LIMB_VAR_DIR . '/foo.js',
+<<<EOD
+Limb.require('bar');
+Limb.require('zoo');
+foo
+EOD
+);
+
+    $this->_createFile(LIMB_VAR_DIR . '/bar.js',
+<<<EOD
+Limb.require('baz');
+bar
+EOD
+);
+
+    $this->_createFile(LIMB_VAR_DIR . '/baz.js',
+<<<EOD
+baz
+EOD
+);
+
+    $this->_createFile(LIMB_VAR_DIR . '/zoo.js',
+<<<EOD
+zoo
+EOD
+);
+
+    $bundler = new lmbFilesBundler(new lmbJsDependencyExtractor(LIMB_VAR_DIR));
+    $bundler->addRootFile($root_file);
+    $this->assertEqual($bundler->createBundle(),
+<<<EOD
+baz
+bar
+zoo
+foo
+
+EOD
+);
+  }
+
+  function testApplyFilters()
+  {
+    $root_file = LIMB_VAR_DIR . '/foo.js';
+    $this->_createFile($root_file, $content = "foo");
+
+    $bundler = new lmbFilesBundler();
+    $bundler->addRootFile($root_file);
+
+    $f1 = new BundleStubFilter('--');
+    $f2 = new BundleStubFilter('||');
+
+    $bundler->addFilter($f1);
+    $bundler->addFilter($f2);
+
+    $this->assertEqual($bundler->createBundle(), "||--foo\n--||");
+  }
+
+  protected function _createFile($file, $content)
+  {
+    lmbFs :: mkdir(dirname($file));
+
+    $fh = fopen($file, 'w');
+    fwrite($fh, $content);
+    fclose($fh);
+    $this->created_files[] = $file;
+  }
+
+  protected function _cleanup()
+  {
+    foreach($this->created_files as $file)
+      unlink($file);
+
+    $this->created_files = array();
+  }
+}
+
+?>



More information about the limb-svn mailing list