2008-09-13 16:40:17 +04:00
|
|
|
<?php
|
2009-01-22 22:38:10 +03:00
|
|
|
|
|
|
|
namespace Doctrine\Common;
|
|
|
|
|
2008-09-13 16:40:17 +04:00
|
|
|
/**
|
|
|
|
* A class loader used to load class files on demand.
|
|
|
|
*
|
|
|
|
* Usage recommendation:
|
|
|
|
* 1) Use only 1 class loader instance.
|
2009-01-03 22:50:13 +03:00
|
|
|
* 2) Set the base paths to your class libraries (including Doctrine's) through
|
|
|
|
* $classLoader->setBasePath($prefix, $basePath);
|
|
|
|
* Example:
|
|
|
|
* $classLoader->setBasePath('Doctrine', '/usr/local/phplibs/doctrine/lib');
|
|
|
|
* Then, when trying to load the class Doctrine\ORM\EntityManager, for example
|
|
|
|
* the classloader will look for /usr/local/phplibs/doctrine/lib/Doctrine/ORM/EntityManager.php
|
|
|
|
*
|
2008-12-18 17:08:11 +03:00
|
|
|
* 3) DO NOT setCheckFileExists(true). Doing so is expensive in terms of performance.
|
2009-01-03 22:50:13 +03:00
|
|
|
* 4) Use an opcode-cache (i.e. APC) (STRONGLY RECOMMENDED).
|
2009-02-18 01:30:13 +03:00
|
|
|
*
|
2008-09-13 16:40:17 +04:00
|
|
|
* @since 2.0
|
2009-02-18 01:30:13 +03:00
|
|
|
* @author Roman S. Borschel <roman@code-factory.org>
|
2008-09-13 16:40:17 +04:00
|
|
|
*/
|
2009-01-22 22:38:10 +03:00
|
|
|
class ClassLoader
|
2009-02-18 01:30:13 +03:00
|
|
|
{
|
|
|
|
private
|
|
|
|
$_namespaceSeparator = '\\',
|
|
|
|
$_fileExtension = '.php',
|
|
|
|
$_checkFileExists = false,
|
|
|
|
$_basePaths = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor registers the autoloader automatically
|
|
|
|
*/
|
2008-09-13 16:40:17 +04:00
|
|
|
public function __construct()
|
|
|
|
{
|
2009-02-18 01:30:13 +03:00
|
|
|
spl_autoload_register(array($this, 'loadClass'));
|
2008-09-13 16:40:17 +04:00
|
|
|
}
|
2009-02-18 01:30:13 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set check file exists
|
|
|
|
*
|
|
|
|
* @param boolean $bool
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-09-13 16:40:17 +04:00
|
|
|
public function setCheckFileExists($bool)
|
|
|
|
{
|
|
|
|
$this->_checkFileExists = $bool;
|
|
|
|
}
|
2009-02-18 01:30:13 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set class file extension
|
|
|
|
*
|
|
|
|
* @param string $extension
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-09-13 16:40:17 +04:00
|
|
|
public function setClassFileExtension($extension)
|
|
|
|
{
|
|
|
|
$this->_fileExtension = $extension;
|
|
|
|
}
|
2009-02-18 01:30:13 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set namespace separator
|
|
|
|
*
|
|
|
|
* @param string $separator
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-09-13 16:40:17 +04:00
|
|
|
public function setNamespaceSeparator($separator)
|
|
|
|
{
|
|
|
|
$this->_namespaceSeparator = $separator;
|
|
|
|
}
|
2009-02-18 01:30:13 +03:00
|
|
|
|
2008-09-13 16:40:17 +04:00
|
|
|
/**
|
2009-01-03 22:50:13 +03:00
|
|
|
* Sets a static base path for classes with a certain prefix that is prepended
|
|
|
|
* to the path derived from the class itself.
|
2008-09-13 16:40:17 +04:00
|
|
|
*
|
2009-02-18 01:30:13 +03:00
|
|
|
* @param string $classPrefix
|
2008-09-13 16:40:17 +04:00
|
|
|
* @param string $basePath
|
|
|
|
*/
|
2009-01-03 22:50:13 +03:00
|
|
|
public function setBasePath($classPrefix, $basePath)
|
2008-09-13 16:40:17 +04:00
|
|
|
{
|
2009-01-03 22:50:13 +03:00
|
|
|
$this->_basePaths[$classPrefix] = $basePath;
|
2008-09-13 16:40:17 +04:00
|
|
|
}
|
2009-02-18 01:30:13 +03:00
|
|
|
|
2008-09-13 16:40:17 +04:00
|
|
|
/**
|
|
|
|
* Loads the given class or interface.
|
|
|
|
*
|
2009-01-03 22:50:13 +03:00
|
|
|
* @param string $classname The name of the class to load.
|
2008-09-13 16:40:17 +04:00
|
|
|
* @return boolean TRUE if the class has been successfully loaded, FALSE otherwise.
|
|
|
|
*/
|
|
|
|
public function loadClass($className)
|
|
|
|
{
|
|
|
|
if (class_exists($className, false) || interface_exists($className, false)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-01-03 22:50:13 +03:00
|
|
|
$prefix = substr($className, 0, strpos($className, $this->_namespaceSeparator));
|
2008-09-13 16:40:17 +04:00
|
|
|
$class = '';
|
2009-05-11 14:43:27 +04:00
|
|
|
|
2009-01-03 22:50:13 +03:00
|
|
|
if (isset($this->_basePaths[$prefix])) {
|
|
|
|
$class .= $this->_basePaths[$prefix] . DIRECTORY_SEPARATOR;
|
2008-09-13 16:40:17 +04:00
|
|
|
}
|
2009-01-22 22:38:10 +03:00
|
|
|
|
2009-05-13 19:19:27 +04:00
|
|
|
$class .= str_replace($this->_namespaceSeparator, DIRECTORY_SEPARATOR, $className)
|
2008-09-13 16:40:17 +04:00
|
|
|
. $this->_fileExtension;
|
|
|
|
|
|
|
|
if ($this->_checkFileExists) {
|
|
|
|
if (!$fh = @fopen($class, 'r', true)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
@fclose($fh);
|
|
|
|
}
|
|
|
|
|
|
|
|
require $class;
|
2009-02-18 01:30:13 +03:00
|
|
|
|
2008-09-13 16:40:17 +04:00
|
|
|
return true;
|
|
|
|
}
|
2009-02-18 01:30:13 +03:00
|
|
|
}
|