2008-09-13 16:40:17 +04:00
|
|
|
<?php
|
2009-08-09 03:16:13 +04:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This software consists of voluntary contributions made by many individuals
|
|
|
|
* and is licensed under the LGPL. For more information, see
|
|
|
|
* <http://www.doctrine-project.org>.
|
|
|
|
*/
|
|
|
|
|
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.
|
|
|
|
*
|
2009-09-01 17:20:51 +04:00
|
|
|
* IMPORTANT:
|
|
|
|
*
|
|
|
|
* This class loader is NOT meant to be put into a "chain" of autoloaders.
|
|
|
|
* It is not meant to load only Doctrine class file. It is a one-classloader-for-all-classes
|
|
|
|
* solution. It may not be useable with frameworks that do not follow basic pear/zend
|
|
|
|
* conventions where the namespace+class name reflects the physical location of the source
|
|
|
|
* file. This is not a bug. This class loader is, however, compatible with the
|
|
|
|
* old namespace separator '_' (underscore), so any classes using that convention
|
|
|
|
* instead of the 5.3 builtin namespaces can be loaded as well.
|
|
|
|
*
|
|
|
|
* The only way to put this classloader into an autoloader chain is to use
|
|
|
|
* setCheckFileExists(true) which is not recommended.
|
|
|
|
*
|
|
|
|
* Here is the recommended usage:
|
2008-09-13 16:40:17 +04:00
|
|
|
* 1) Use only 1 class loader instance.
|
2009-09-01 17:20:51 +04:00
|
|
|
* 2) Reduce the include_path to only the path to the PEAR packages.
|
|
|
|
* 2) Set the base paths to any non-pear class libraries through
|
2009-01-03 22:50:13 +03:00
|
|
|
* $classLoader->setBasePath($prefix, $basePath);
|
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-09-01 17:20:51 +04:00
|
|
|
*
|
|
|
|
* The "prefix" is the part of a class name before the very first namespace separator
|
|
|
|
* character. If the class is named "Foo_Bar_Baz" then the prefix is "Foo".
|
|
|
|
*
|
|
|
|
* If no base path is configured for a certain class prefix, the classloader relies on
|
|
|
|
* the include_path. That's why classes of any pear packages can be loaded without
|
|
|
|
* registering their base paths. However, since a long include_path has a negative effect
|
|
|
|
* on performance it is recommended to have only the path to the pear packages in the
|
|
|
|
* include_path.
|
|
|
|
*
|
|
|
|
* For any other class libraries you always have the choice between registering the base
|
|
|
|
* path on the classloader or putting the base path into the include_path. The former
|
|
|
|
* should be preferred but the latter is fine also.
|
2009-02-18 01:30:13 +03:00
|
|
|
*
|
2009-08-09 03:16:13 +04:00
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
|
* @link www.doctrine-project.org
|
|
|
|
* @since 2.0
|
|
|
|
* @version $Revision: 3938 $
|
|
|
|
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
|
|
|
|
* @author Jonathan Wage <jonwage@gmail.com>
|
|
|
|
* @author Roman 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
|
|
|
{
|
2009-08-11 01:36:57 +04:00
|
|
|
/**
|
|
|
|
* @var string Namespace separator
|
|
|
|
*/
|
2009-09-01 17:00:05 +04:00
|
|
|
private $_namespaceSeparator = '\\';
|
2009-08-11 01:36:57 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string File extension used for classes
|
|
|
|
*/
|
|
|
|
private $_fileExtension = '.php';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var boolean Flag to inspect if file exists in codebase before include it
|
|
|
|
*/
|
|
|
|
private $_checkFileExists = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array Hashmap of base paths that Autoloader will look into
|
|
|
|
*/
|
|
|
|
private $_basePaths = array();
|
2009-02-18 01:30:13 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor registers the autoloader automatically
|
2009-08-11 01:36:57 +04:00
|
|
|
*
|
2009-02-18 01:30:13 +03:00
|
|
|
*/
|
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
|
|
|
|
|
|
|
/**
|
2009-06-01 20:14:11 +04:00
|
|
|
* Sets the namespace separator to use.
|
2009-02-18 01:30:13 +03:00
|
|
|
*
|
|
|
|
* @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-09-01 17:00:05 +04:00
|
|
|
|
|
|
|
$prefix = '';
|
|
|
|
$separator = $this->_namespaceSeparator;
|
|
|
|
if (($pos = strpos($className, $this->_namespaceSeparator)) !== false) {
|
|
|
|
$prefix = substr($className, 0, strpos($className, $this->_namespaceSeparator));
|
|
|
|
} else if (($pos = strpos($className, '_')) !== false) {
|
|
|
|
// Support for '_' namespace separator for compatibility with Zend/PEAR/...
|
|
|
|
$prefix = substr($className, 0, strpos($className, '_'));
|
|
|
|
$separator = '_';
|
|
|
|
}
|
2009-08-11 01:36:57 +04:00
|
|
|
|
|
|
|
// If we have a custom path for namespace, use it
|
|
|
|
$class = ((isset($this->_basePaths[$prefix])) ? $this->_basePaths[$prefix] . DIRECTORY_SEPARATOR : '')
|
2009-09-01 17:00:05 +04:00
|
|
|
. str_replace($separator, DIRECTORY_SEPARATOR, $className) . $this->_fileExtension;
|
2008-09-13 16:40:17 +04:00
|
|
|
|
2009-08-11 01:36:57 +04:00
|
|
|
// Assure file exists in codebase before require if flag is active
|
2008-09-13 16:40:17 +04:00
|
|
|
if ($this->_checkFileExists) {
|
2009-08-11 01:36:57 +04:00
|
|
|
if (($fh = @fopen($class, 'r', true)) === false) {
|
2008-09-13 16:40:17 +04:00
|
|
|
return false;
|
|
|
|
}
|
2009-08-11 01:36:57 +04:00
|
|
|
|
2008-09-13 16:40:17 +04:00
|
|
|
@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
|
|
|
}
|