2010-01-12 15:00:32 +00:00
|
|
|
<?php
|
|
|
|
|
2010-07-09 09:12:56 +01:00
|
|
|
namespace Symfony\Framework;
|
2010-01-12 15:00:32 +00:00
|
|
|
|
|
|
|
/*
|
2010-04-07 01:51:29 +01:00
|
|
|
* This file is part of the Symfony package.
|
2010-01-12 15:00:32 +00:00
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-02-26 02:14:17 +00:00
|
|
|
* UniversalClassLoader implements a "universal" autoloader for PHP 5.3.
|
2010-01-12 15:00:32 +00:00
|
|
|
*
|
|
|
|
* It is able to load classes that use either:
|
|
|
|
*
|
|
|
|
* * The technical interoperability standards for PHP 5.3 namespaces and
|
2010-04-04 18:07:18 +01:00
|
|
|
* class names (http://groups.google.com/group/php-standards/web/psr-0-final-proposal);
|
2010-01-12 15:00:32 +00:00
|
|
|
*
|
|
|
|
* * The PEAR naming convention for classes (http://pear.php.net/).
|
|
|
|
*
|
|
|
|
* Classes from a sub-namespace or a sub-hierarchy of PEAR classes can be
|
|
|
|
* looked for in a list of locations to ease the vendoring of a sub-set of
|
|
|
|
* classes for large projects.
|
|
|
|
*
|
|
|
|
* Example usage:
|
|
|
|
*
|
2010-03-09 10:33:01 +00:00
|
|
|
* $loader = new UniversalClassLoader();
|
2010-01-12 15:00:32 +00:00
|
|
|
*
|
|
|
|
* // register classes with namespaces
|
|
|
|
* $loader->registerNamespaces(array(
|
|
|
|
* 'Symfony\Components' => __DIR__.'/components',
|
|
|
|
* 'Symfony' => __DIR__.'/framework',
|
|
|
|
* ));
|
|
|
|
*
|
|
|
|
* // register a library using the PEAR naming convention
|
2010-01-19 19:18:51 +00:00
|
|
|
* $loader->registerPrefixes(array(
|
2010-01-12 15:00:32 +00:00
|
|
|
* 'Swift_' => __DIR__.'/Swift',
|
|
|
|
* ));
|
|
|
|
*
|
|
|
|
* // activate the autoloader
|
|
|
|
* $loader->register();
|
|
|
|
*
|
|
|
|
* In this example, if you try to use a class in the Symfony\Components
|
|
|
|
* namespace or one of its children (Symfony\Components\Console for instance),
|
|
|
|
* the autoloader will first look for the class under the components/
|
|
|
|
* directory, and it will then fallback to the framework/ directory if not
|
|
|
|
* found before giving up.
|
|
|
|
*
|
2010-04-07 01:51:29 +01:00
|
|
|
* @package Symfony
|
2010-07-09 09:01:36 +01:00
|
|
|
* @subpackage Framework
|
2010-01-12 15:00:32 +00:00
|
|
|
* @author Fabien Potencier <fabien.potencier@symfony-project.org>
|
|
|
|
*/
|
|
|
|
class UniversalClassLoader
|
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
protected $namespaces = array();
|
|
|
|
protected $prefixes = array();
|
2010-01-12 15:00:32 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
public function getNamespaces()
|
|
|
|
{
|
|
|
|
return $this->namespaces;
|
|
|
|
}
|
2010-03-01 18:10:47 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
public function getPrefixes()
|
|
|
|
{
|
|
|
|
return $this->prefixes;
|
|
|
|
}
|
2010-03-01 18:10:47 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Registers an array of namespaces
|
|
|
|
*
|
|
|
|
* @param array $namespaces An array of namespaces (namespaces as keys and locations as values)
|
|
|
|
*/
|
|
|
|
public function registerNamespaces(array $namespaces)
|
|
|
|
{
|
|
|
|
$this->namespaces = array_merge($this->namespaces, $namespaces);
|
|
|
|
}
|
2010-01-12 15:00:32 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Registers a namespace.
|
|
|
|
*
|
|
|
|
* @param string $namespace The namespace
|
|
|
|
* @param string $path The location of the namespace
|
|
|
|
*/
|
|
|
|
public function registerNamespace($namespace, $path)
|
|
|
|
{
|
|
|
|
$this->namespaces[$namespace] = $path;
|
|
|
|
}
|
2010-01-12 15:00:32 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Registers an array of classes using the PEAR naming convention.
|
|
|
|
*
|
|
|
|
* @param array $classes An array of classes (prefixes as keys and locations as values)
|
|
|
|
*/
|
|
|
|
public function registerPrefixes(array $classes)
|
|
|
|
{
|
|
|
|
$this->prefixes = array_merge($this->prefixes, $classes);
|
|
|
|
}
|
2010-01-12 15:00:32 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Registers a set of classes using the PEAR naming convention.
|
|
|
|
*
|
|
|
|
* @param string $prefix The classes prefix
|
|
|
|
* @param string $path The location of the classes
|
|
|
|
*/
|
|
|
|
public function registerPrefix($prefix, $path)
|
|
|
|
{
|
|
|
|
$this->prefixes[$prefix] = $path;
|
|
|
|
}
|
2010-01-12 15:00:32 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Registers this instance as an autoloader.
|
|
|
|
*/
|
|
|
|
public function register()
|
|
|
|
{
|
|
|
|
spl_autoload_register(array($this, 'loadClass'));
|
|
|
|
}
|
2010-01-12 15:00:32 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Loads the given class or interface.
|
|
|
|
*
|
|
|
|
* @param string $class The name of the class
|
|
|
|
*/
|
|
|
|
public function loadClass($class)
|
2010-01-12 15:00:32 +00:00
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
if (false !== ($pos = strripos($class, '\\'))) {
|
2010-05-06 12:25:53 +01:00
|
|
|
// namespaced class name
|
|
|
|
$namespace = substr($class, 0, $pos);
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($this->namespaces as $ns => $dir) {
|
2010-05-08 14:32:30 +01:00
|
|
|
if (0 === strpos($namespace, $ns)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$class = substr($class, $pos + 1);
|
|
|
|
$file = $dir.DIRECTORY_SEPARATOR.str_replace('\\', DIRECTORY_SEPARATOR, $namespace).DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $class).'.php';
|
2010-05-07 15:09:11 +01:00
|
|
|
if (file_exists($file)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
require $file;
|
|
|
|
}
|
2010-01-12 15:00:32 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2010-05-07 15:09:11 +01:00
|
|
|
} else {
|
2010-05-06 12:25:53 +01:00
|
|
|
// PEAR-like class name
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($this->prefixes as $prefix => $dir) {
|
2010-05-08 14:32:30 +01:00
|
|
|
if (0 === strpos($class, $prefix)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$file = $dir.DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $class).'.php';
|
2010-05-07 15:09:11 +01:00
|
|
|
if (file_exists($file)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
require $file;
|
|
|
|
}
|
2010-01-12 15:00:32 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2010-01-12 15:00:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|