[2.0] Small changes to ClassLoader and add basic unit test for it
This commit is contained in:
parent
554adc32a4
commit
9dfab03ee0
@ -16,47 +16,71 @@ namespace Doctrine\Common;
|
||||
*
|
||||
* 3) DO NOT setCheckFileExists(true). Doing so is expensive in terms of performance.
|
||||
* 4) Use an opcode-cache (i.e. APC) (STRONGLY RECOMMENDED).
|
||||
*
|
||||
*
|
||||
* @since 2.0
|
||||
* @author romanb <roman@code-factory.org>
|
||||
* @author Roman S. Borschel <roman@code-factory.org>
|
||||
*/
|
||||
class ClassLoader
|
||||
{
|
||||
private $_namespaceSeparator = '\\';
|
||||
private $_fileExtension = '.php';
|
||||
private $_checkFileExists = false;
|
||||
private $_basePaths = array();
|
||||
|
||||
{
|
||||
private
|
||||
$_namespaceSeparator = '\\',
|
||||
$_fileExtension = '.php',
|
||||
$_checkFileExists = false,
|
||||
$_basePaths = array();
|
||||
|
||||
/**
|
||||
* Constructor registers the autoloader automatically
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
spl_autoload_register(array($this, 'loadClass'));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set check file exists
|
||||
*
|
||||
* @param boolean $bool
|
||||
* @return void
|
||||
*/
|
||||
public function setCheckFileExists($bool)
|
||||
{
|
||||
$this->_checkFileExists = $bool;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set class file extension
|
||||
*
|
||||
* @param string $extension
|
||||
* @return void
|
||||
*/
|
||||
public function setClassFileExtension($extension)
|
||||
{
|
||||
$this->_fileExtension = $extension;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set namespace separator
|
||||
*
|
||||
* @param string $separator
|
||||
* @return void
|
||||
*/
|
||||
public function setNamespaceSeparator($separator)
|
||||
{
|
||||
$this->_namespaceSeparator = $separator;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets a static base path for classes with a certain prefix that is prepended
|
||||
* to the path derived from the class itself.
|
||||
*
|
||||
* @param string $classPrefix
|
||||
* @param string $basePath
|
||||
*/
|
||||
public function setBasePath($classPrefix, $basePath)
|
||||
{
|
||||
$this->_basePaths[$classPrefix] = $basePath;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Loads the given class or interface.
|
||||
*
|
||||
@ -86,20 +110,7 @@ class ClassLoader
|
||||
}
|
||||
|
||||
require $class;
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers this class loader using spl_autoload_register().
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
spl_autoload_register(array($this, 'loadClass'));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
?>
|
||||
}
|
@ -22,6 +22,7 @@ class AllTests
|
||||
$suite = new \Doctrine\Tests\DoctrineTestSuite('Doctrine Common Tests');
|
||||
|
||||
$suite->addTestSuite('Doctrine\Tests\Common\EventManagerTest');
|
||||
$suite->addTestSuite('Doctrine\Tests\Common\ClassLoaderTest');
|
||||
|
||||
$suite->addTest(Collections\AllTests::suite());
|
||||
|
||||
|
29
tests/Doctrine/Tests/Common/ClassLoaderTest.php
Normal file
29
tests/Doctrine/Tests/Common/ClassLoaderTest.php
Normal file
@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace Doctrine\Tests\Common;
|
||||
|
||||
use Doctrine\Common\ClassLoader;
|
||||
|
||||
class ClassLoaderTest extends \Doctrine\Tests\DoctrineTestCase
|
||||
{
|
||||
public function testCustomFileExtensionAndNamespaceSeparator()
|
||||
{
|
||||
$classLoader = new \Doctrine\Common\ClassLoader();
|
||||
$classLoader->setBasePath('ClassLoaderTest', __DIR__);
|
||||
$classLoader->setClassFileExtension('.class.php');
|
||||
$classLoader->setNamespaceSeparator('_');
|
||||
|
||||
$this->assertEquals($classLoader->loadClass('ClassLoaderTest_ClassA'), true);
|
||||
$this->assertEquals($classLoader->loadClass('ClassLoaderTest_ClassB'), true);
|
||||
}
|
||||
|
||||
public function testClassLoaderCheckFileExists()
|
||||
{
|
||||
$classLoader = new \Doctrine\Common\ClassLoader();
|
||||
$classLoader->setBasePath('ClassLoaderTest', __DIR__);
|
||||
$classLoader->setCheckFileExists(true);
|
||||
|
||||
// This would return a fatal error without check file exists true
|
||||
$this->assertEquals($classLoader->loadClass('SomeInvalidClass'), false);
|
||||
}
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
<?php
|
||||
class ClassLoaderTest_ClassA
|
||||
{
|
||||
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
<?php
|
||||
class ClassLoaderTest_ClassB
|
||||
{
|
||||
|
||||
}
|
@ -9,9 +9,6 @@ require_once 'PHPUnit/TextUI/TestRunner.php';
|
||||
require_once '../lib/Doctrine/Common/ClassLoader.php';
|
||||
|
||||
$classLoader = new \Doctrine\Common\ClassLoader();
|
||||
// checking for existance should not be necessary, remove as soon as possible
|
||||
//$classLoader->setCheckFileExists(true);
|
||||
$classLoader->register();
|
||||
|
||||
$modelDir = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'models';
|
||||
set_include_path(
|
||||
|
Loading…
Reference in New Issue
Block a user