[limb-svn] r6559 - in 3.x/trunk/limb/core: . tests/cases
svn at limb-project.com
svn at limb-project.com
Mon Dec 3 14:14:22 MSK 2007
Author: pachanga
Date: 2007-12-03 14:14:21 +0300 (Mon, 03 Dec 2007)
New Revision: 6559
URL: http://fisheye.limb-project.com/changelog/limb/?cs=6559
Modified:
3.x/trunk/limb/core/common.inc.php
3.x/trunk/limb/core/tests/cases/lmbRequireTest.class.php
Log:
-- lmb_require second argument is not $optional any more, this stuff completely moved into lmb_require_optional
-- lmb_require second argument now can be a class or interface name which gives possibility to use lmb_require lazy loading for classes and interfaces which are stored in modules
-- lmb_require internal cruft refactoring
Modified: 3.x/trunk/limb/core/common.inc.php
===================================================================
--- 3.x/trunk/limb/core/common.inc.php 2007-12-03 10:13:36 UTC (rev 6558)
+++ 3.x/trunk/limb/core/common.inc.php 2007-12-03 11:14:21 UTC (rev 6559)
@@ -57,12 +57,12 @@
if(!$path)
return false;
- //very trivial check, is more comprehensive required?
+ //very trivial check, is more comprehensive one required?
return (($path{0} == '/' || $path{0} == '\\') ||
(strlen($path) > 2 && $path{1} == ':'));
}
-function lmb_require($file_path, $optional = false)
+function lmb_require($file_path, $class = null)
{
static $tried = array();
@@ -71,42 +71,54 @@
else
$tried[$file_path] = true;
+ //do we really need this stuff here?
if(strpos($file_path, '*') !== false)
{
foreach(lmb_glob($file_path) as $path)
- lmb_require($path, $optional);
+ lmb_require($path);
return;
}
- if($optional && !lmb_is_readable($file_path))
- return;
+ if(!$class)
+ {
+ //autoguessing class or interface name by file
+ $file = basename($file_path);
+ $items = explode('.', $file);
- $file = basename($file_path);
- $items = explode('.', $file);
-
- if(isset($items[1]))
- {
- if($items[1] == 'class' || $items[1] == 'interface')
+ if(isset($items[1]))
{
- $GLOBALS['LIMB_LAZY_CLASS_PATHS'][$items[0]] = $file_path;
- return;
+ if($items[1] == 'class' || $items[1] == 'interface')
+ $class = $items[0];
}
}
- else
+
+ if($class)
{
- if($items[1] == 'class' && class_exists($items[0], false))
- return;
- if($items[1] == 'interface' && interface_exists($items[0], false))
- return;
+ $GLOBALS['LIMB_LAZY_CLASS_PATHS'][$class] = $file_path;
+ return;
}
if(!include_once($file_path))
throw new lmbException("Could not include source file '$file_path'");
}
+function lmb_glob_require($file_path)
+{
+ if(strpos($file_path, '*') !== false)
+ {
+ foreach(lmb_glob($file_path) as $path)
+ lmb_require($path);
+ }
+ else
+ lmb_require($path);
+}
+
function lmb_require_optional($file_path)
{
- lmb_require($file_path, true);
+ if(!lmb_is_readable($file_path))
+ return;
+
+ lmb_require($file_path);
}
function lmb_autoload($name)
@@ -114,6 +126,7 @@
if(isset($GLOBALS['LIMB_LAZY_CLASS_PATHS'][$name]))
{
$file_path = $GLOBALS['LIMB_LAZY_CLASS_PATHS'][$name];
+ //is it safe to use include here instead of include_once?
if(!include($file_path))
throw new lmbException("Could not include source file '$file_path'");
}
Modified: 3.x/trunk/limb/core/tests/cases/lmbRequireTest.class.php
===================================================================
--- 3.x/trunk/limb/core/tests/cases/lmbRequireTest.class.php 2007-12-03 10:13:36 UTC (rev 6558)
+++ 3.x/trunk/limb/core/tests/cases/lmbRequireTest.class.php 2007-12-03 11:14:21 UTC (rev 6559)
@@ -1,249 +1,288 @@
-<?php
-/*
- * Limb PHP Framework
- *
- * @link http://limb-project.com
- * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com)
- * @license LGPL http://www.gnu.org/copyleft/lesser.html
- */
-
-class lmbRequireTest extends UnitTestCase
-{
- var $tmp_dir;
-
- function setUp()
- {
- if(!is_dir(LIMB_VAR_DIR))
- mkdir(LIMB_VAR_DIR);
-
- $this->tmp_dir = LIMB_VAR_DIR . '/lmb_require/';
- $this->_rm($this->tmp_dir);
- mkdir($this->tmp_dir);
- }
-
- function tearDown()
- {
- $this->_rm($this->tmp_dir);
- }
-
- function testLazyLoadClass()
- {
- $name1 = $this->_rndName();
- $path1 = $this->_writeClassFile($name1);
-
- $name2 = $this->_rndName();
- $path2 = $this->_writeClassFile($name2);
-
- lmb_require($path1);
- lmb_require($path2);
-
- $this->assertFalse(in_array($name1, get_declared_classes()));
- $this->assertFalse(in_array($name2, get_declared_classes()));
-
- $this->assertTrue(class_exists($name1, true));//triggers autoload
-
- $this->assertTrue(in_array($name1, get_declared_classes()));
- $this->assertFalse(in_array($name2, get_declared_classes()));
- }
-
- function testLazyLoadInterface()
- {
- $name1 = $this->_rndName();
- $path1 = $this->_writeInterfaceFile($name1);
- $name2 = $this->_rndName();
- $path2 = $this->_writeInterfaceFile($name2);
-
- lmb_require($path1);
- lmb_require($path2);
-
- $this->assertFalse(in_array($name1, get_declared_interfaces()));
- $this->assertFalse(in_array($name2, get_declared_interfaces()));
-
- $this->assertTrue(interface_exists($name1, true));//triggers autoload
-
- $this->assertTrue(in_array($name1, get_declared_interfaces()));
- $this->assertFalse(in_array($name2, get_declared_interfaces()));
- }
-
- function testRecursionProtection()
- {
- $name = $this->_rndName();
- $path = $this->_writeModule("$name.class.php", "<?php \$foo = new $name(); class $name {} ?>");
-
- lmb_require($path);
-
- $foo = new $name();
- }
-
- function testNoLazyLoadForModule()
- {
- $name = $this->_rndName();
- $path = $this->_writeModule("$name.inc.php", "<?php class $name {} ?>");
-
- lmb_require($path);
- $this->assertTrue(in_array($name, get_declared_classes()));
- }
-
- function testGlobRequireWithAutoload()
- {
- //creating new unique directory
- $old_dir = $this->tmp_dir;
- $this->tmp_dir = $this->tmp_dir . $this->_rndName() . '/';
- mkdir($this->tmp_dir);
-
- $c1 = $this->_rndName();
- $c2 = $this->_rndName();
- $c3 = $this->_rndName();
-
- $path1 = $this->_writeClassFile($c1);
- $path2 = $this->_writeClassFile($c2);
- $path3 = $this->_writeClassFile($c3);
-
- lmb_require($this->tmp_dir . '/*.class.php');
-
- foreach(array($c1, $c2, $c3) as $c)
- {
- $this->assertFalse(in_array($c, get_declared_interfaces()));
- $this->assertTrue(class_exists($c, true));
- }
-
- $this->tmp_dir = $old_dir;
- }
-
- function testRequireFileCacheHit()
- {
- if(!function_exists('xdebug_start_code_coverage'))
- return;
-
- //we need to prevent collisions with external code coverage analyzing tools
- if(xdebug_get_code_coverage())
- return;
-
- $name = $this->_rndName();
- $path = $this->_writeModule("$name.inc.php", "<?php class $name {} ?>");
-
- $func = new ReflectionFunction('lmb_require');
- $file = $func->getFileName();
-
- $line = $this->_locateIncludeOnceLine($file, $func->getStartLine());
-
- xdebug_start_code_coverage();
- lmb_require($path);
- $cov1 = xdebug_get_code_coverage();
- xdebug_stop_code_coverage();
-
- xdebug_start_code_coverage();
- lmb_require($path);
- $cov2 = xdebug_get_code_coverage();
- xdebug_stop_code_coverage();
-
- $this->assertEqual($cov1[$file][$line], 1);
- $this->assertFalse(isset($cov2[$file][$line]));
- }
-
- function testRequireThrowsExceptionForNonExistingFile()
- {
- try
- {
- @lmb_require($file = 'foo_' . mt_rand() . '.inc.php');
- }
- catch(lmbException $e)
- {
- $this->assertPattern('~' . preg_quote($file) . '~', $e->getMessage());
- }
- }
-
- function testRequireOptional()
- {
- $name = $this->_rndName();
- $path = $this->_writeModule("$name.class.php", "<?php class $name {} ?>");
-
- lmb_require_optional($path);
-
- $foo = new $name();
- }
-
- function testRequireOptionalDoesntThrowExceptionForNonExistingFile()
- {
- lmb_require_optional($file = 'foo_' . mt_rand() . '.inc.php');
- }
-
- function testRequireOptionalGlobDoesntThrowExceptionForNonExistingFiles()
- {
- lmb_require_optional($file = 'foo_' . mt_rand() . '*.php');
- }
-
- function _locateIncludeOnceLine($file, $start_line)
- {
- $c = 0;
- foreach(file($file) as $line)
- {
- $c++;
- if($c >= $start_line && strpos($line, 'include_once') !== false)
- return $c;
- }
- }
-
- function _writeClassFile($name, $body = null)
- {
- $path = $this->tmp_dir . $name . '.class.php';
- $this->_write($path, $body ? $body : $this->_classCode($name));
- return $path;
- }
-
- function _writeInterfaceFile($name)
- {
- $path = $this->tmp_dir . $name . '.interface.php';
- $this->_write($path, $this->_faceCode($name));
- return $path;
- }
-
- function _writeModule($name, $contents)
- {
- $path = $this->tmp_dir . $name;
- $this->_write($path, $contents);
- return $path;
- }
-
- function _classCode($name)
- {
- return "<?php class $name {} ?>";
- }
-
- function _faceCode($name)
- {
- return "<?php interface $name {} ?>";
- }
-
- function _rndName()
- {
- return 'Foo' . mt_rand(1, 1000);
- }
-
- function _rnd()
- {
- return mt_rand(1, 1000);
- }
-
- function _write($file, $contents='')
- {
- file_put_contents($file, $contents);
- }
-
- function _rm($path)
- {
- if(!is_dir($path))
- return;
- $dir = opendir($path);
- while($entry = readdir($dir))
- {
- if(is_file("$path/$entry"))
- unlink("$path/$entry");
- elseif(is_dir("$path/$entry") && $entry != '.' && $entry != '..')
- $this->_rm("$path/$entry");
- }
- closedir($dir);
- return rmdir($path);
- }
-}
-
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com)
+ * @license LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+class lmbRequireTest extends UnitTestCase
+{
+ var $tmp_dir;
+
+ function setUp()
+ {
+ if(!is_dir(LIMB_VAR_DIR))
+ mkdir(LIMB_VAR_DIR);
+
+ $this->tmp_dir = LIMB_VAR_DIR . '/lmb_require/';
+ $this->_rm($this->tmp_dir);
+ mkdir($this->tmp_dir);
+ }
+
+ function tearDown()
+ {
+ $this->_rm($this->tmp_dir);
+ }
+
+ function testLazyLoadClass()
+ {
+ $name1 = $this->_rndName();
+ $path1 = $this->_writeClassFile($name1, '.php');
+
+ $name2 = $this->_rndName();
+ $path2 = $this->_writeClassFile($name2, '.php');
+
+ lmb_require($path1, $name1);
+ lmb_require($path2, $name2);
+
+ $this->assertFalse(in_array($name1, get_declared_classes()));
+ $this->assertFalse(in_array($name2, get_declared_classes()));
+
+ $this->assertTrue(class_exists($name1, true));//triggers autoload
+
+ $this->assertTrue(in_array($name1, get_declared_classes()));
+ $this->assertFalse(in_array($name2, get_declared_classes()));
+ }
+
+ function testLazyLoadClassGuessNameFromFile()
+ {
+ $name1 = $this->_rndName();
+ $path1 = $this->_writeClassFile($name1, '.class.php');
+
+ $name2 = $this->_rndName();
+ $path2 = $this->_writeClassFile($name2, '.class.php');
+
+ lmb_require($path1);
+ lmb_require($path2);
+
+ $this->assertFalse(in_array($name1, get_declared_classes()));
+ $this->assertFalse(in_array($name2, get_declared_classes()));
+
+ $this->assertTrue(class_exists($name1, true));//triggers autoload
+
+ $this->assertTrue(in_array($name1, get_declared_classes()));
+ $this->assertFalse(in_array($name2, get_declared_classes()));
+ }
+
+ function testLazyLoadInterface()
+ {
+ $name1 = $this->_rndName();
+ $path1 = $this->_writeInterfaceFile($name1, '.php');
+ $name2 = $this->_rndName();
+ $path2 = $this->_writeInterfaceFile($name2, '.php');
+
+ lmb_require($path1, $name1);
+ lmb_require($path2, $name2);
+
+ $this->assertFalse(in_array($name1, get_declared_interfaces()));
+ $this->assertFalse(in_array($name2, get_declared_interfaces()));
+
+ $this->assertTrue(interface_exists($name1, true));//triggers autoload
+
+ $this->assertTrue(in_array($name1, get_declared_interfaces()));
+ $this->assertFalse(in_array($name2, get_declared_interfaces()));
+ }
+
+ function testLazyLoadInterfaceGuessNameFromFile()
+ {
+ $name1 = $this->_rndName();
+ $path1 = $this->_writeInterfaceFile($name1, '.interface.php');
+ $name2 = $this->_rndName();
+ $path2 = $this->_writeInterfaceFile($name2, '.interface.php');
+
+ lmb_require($path1);
+ lmb_require($path2);
+
+ $this->assertFalse(in_array($name1, get_declared_interfaces()));
+ $this->assertFalse(in_array($name2, get_declared_interfaces()));
+
+ $this->assertTrue(interface_exists($name1, true));//triggers autoload
+
+ $this->assertTrue(in_array($name1, get_declared_interfaces()));
+ $this->assertFalse(in_array($name2, get_declared_interfaces()));
+ }
+
+ function testPossibleRecursiveInclude()
+ {
+ $name = $this->_rndName();
+ $path = $this->_writeModule("$name.class.php", "<?php \$foo = new $name(); class $name {} ?>");
+
+ lmb_require($path);
+
+ $foo = new $name();
+ }
+
+ function testNoLazyLoadForModule()
+ {
+ $name = $this->_rndName();
+ $path = $this->_writeModule("$name.inc.php", "<?php class $name {} ?>");
+
+ lmb_require($path);
+ $this->assertTrue(in_array($name, get_declared_classes()));
+ }
+
+ function testGlobRequireWithAutoload()
+ {
+ //creating new unique directory
+ $old_dir = $this->tmp_dir;
+ $this->tmp_dir = $this->tmp_dir . $this->_rndName() . '/';
+ mkdir($this->tmp_dir);
+
+ $c1 = $this->_rndName();
+ $c2 = $this->_rndName();
+ $c3 = $this->_rndName();
+
+ $path1 = $this->_writeClassFile($c1);
+ $path2 = $this->_writeClassFile($c2);
+ $path3 = $this->_writeClassFile($c3);
+
+ lmb_require($this->tmp_dir . '/*.class.php');
+
+ foreach(array($c1, $c2, $c3) as $c)
+ {
+ $this->assertFalse(in_array($c, get_declared_interfaces()));
+ $this->assertTrue(class_exists($c, true));
+ }
+
+ $this->tmp_dir = $old_dir;
+ }
+
+ function testRequireFileCacheHit()
+ {
+ if(!function_exists('xdebug_start_code_coverage'))
+ return;
+
+ //we need to prevent collisions with external code coverage analyzing tools
+ if(xdebug_get_code_coverage())
+ return;
+
+ $name = $this->_rndName();
+ $path = $this->_writeModule("$name.inc.php", "<?php class $name {} ?>");
+
+ $func = new ReflectionFunction('lmb_require');
+ $file = $func->getFileName();
+
+ $line = $this->_locateIncludeOnceLine($file, $func->getStartLine());
+
+ xdebug_start_code_coverage();
+ lmb_require($path);
+ $cov1 = xdebug_get_code_coverage();
+ xdebug_stop_code_coverage();
+
+ xdebug_start_code_coverage();
+ lmb_require($path);
+ $cov2 = xdebug_get_code_coverage();
+ xdebug_stop_code_coverage();
+
+ $this->assertEqual($cov1[$file][$line], 1);
+ $this->assertFalse(isset($cov2[$file][$line]));
+ }
+
+ function testRequireThrowsExceptionForNonExistingFile()
+ {
+ try
+ {
+ @lmb_require($file = 'foo_' . mt_rand() . '.inc.php');
+ }
+ catch(lmbException $e)
+ {
+ $this->assertPattern('~' . preg_quote($file) . '~', $e->getMessage());
+ }
+ }
+
+ function testRequireOptionalOk()
+ {
+ $name = $this->_rndName();
+ $path = $this->_writeModule("$name.class.php", "<?php class $name {} ?>");
+
+ lmb_require_optional($path);
+
+ $foo = new $name();
+ }
+
+ function testRequireOptionalDoesntThrowExceptionForNonExistingFile()
+ {
+ lmb_require_optional($file = 'foo_' . mt_rand() . '.inc.php');
+ }
+
+ function testRequireOptionalGlobDoesntThrowExceptionForNonExistingFiles()
+ {
+ lmb_require_optional($file = 'foo_' . mt_rand() . '*.php');
+ }
+
+ function _locateIncludeOnceLine($file, $start_line)
+ {
+ $c = 0;
+ foreach(file($file) as $line)
+ {
+ $c++;
+ if($c >= $start_line && strpos($line, 'include_once') !== false)
+ return $c;
+ }
+ }
+
+ function _writeClassFile($name, $ext = '.class.php')
+ {
+ $path = $this->tmp_dir . $name . $ext;
+ $this->_write($path, $this->_classCode($name));
+ return $path;
+ }
+
+ function _writeInterfaceFile($name, $ext = '.interface.php')
+ {
+ $path = $this->tmp_dir . $name . $ext;
+ $this->_write($path, $this->_faceCode($name));
+ return $path;
+ }
+
+ function _writeModule($name, $contents)
+ {
+ $path = $this->tmp_dir . $name;
+ $this->_write($path, $contents);
+ return $path;
+ }
+
+ function _classCode($name)
+ {
+ return "<?php class $name {} ?>";
+ }
+
+ function _faceCode($name)
+ {
+ return "<?php interface $name {} ?>";
+ }
+
+ function _rndName()
+ {
+ return 'Foo' . mt_rand(1, 1000);
+ }
+
+ function _rnd()
+ {
+ return mt_rand(1, 1000);
+ }
+
+ function _write($file, $contents='')
+ {
+ file_put_contents($file, $contents);
+ }
+
+ function _rm($path)
+ {
+ if(!is_dir($path))
+ return;
+ $dir = opendir($path);
+ while($entry = readdir($dir))
+ {
+ if(is_file("$path/$entry"))
+ unlink("$path/$entry");
+ elseif(is_dir("$path/$entry") && $entry != '.' && $entry != '..')
+ $this->_rm("$path/$entry");
+ }
+ closedir($dir);
+ return rmdir($path);
+ }
+}
+
More information about the limb-svn
mailing list