Skip to content

Commit

Permalink
[ClassLoader] added the possibility to define more than one directory…
Browse files Browse the repository at this point in the history
… for a namespace or a prefix
  • Loading branch information
fabpot committed Feb 3, 2011
1 parent c05fb03 commit 2d69369
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 100 deletions.
95 changes: 53 additions & 42 deletions src/Symfony/Component/ClassLoader/UniversalClassLoader.php
Expand Up @@ -31,13 +31,14 @@
*
* // register classes with namespaces
* $loader->registerNamespaces(array(
* 'Symfony\Component' => __DIR__.'/component',
* 'Symfony' => __DIR__.'/framework',
* 'Symfony\Component' => __DIR__.'/component',
* 'Symfony' => __DIR__.'/framework',
* 'Sensio' => array(__DIR__.'/src', __DIR__.'/vendor'),
* ));
*
* // register a library using the PEAR naming convention
* $loader->registerPrefixes(array(
* 'Swift_' => __DIR__.'/Swift',
* 'Swift_' => __DIR__.'/Swift',
* ));
*
* // activate the autoloader
Expand All @@ -55,8 +56,8 @@ class UniversalClassLoader
{
protected $namespaces = array();
protected $prefixes = array();
protected $namespaceFallback;
protected $prefixFallback;
protected $namespaceFallback = array();
protected $prefixFallback = array();

/**
* Gets the configured namespaces.
Expand All @@ -79,19 +80,19 @@ public function getPrefixes()
}

/**
* Gets the directory to use as a fallback for namespaces.
* Gets the directory(ies) to use as a fallback for namespaces.
*
* @return string A directory path
* @return array An array of directories
*/
public function getNamespaceFallback()
{
return $this->namespaceFallback;
}

/**
* Gets the directory to use as a fallback for class prefixes.
* Gets the directory(ies) to use as a fallback for class prefixes.
*
* @return string A directory path
* @return array An array of directories
*/
public function getPrefixFallback()
{
Expand All @@ -101,21 +102,21 @@ public function getPrefixFallback()
/**
* Registers the directory to use as a fallback for namespaces.
*
* @return string $dir A directory path
* @return string|array $dirs A directory path or an array of directories
*/
public function registerNamespaceFallback($dir)
public function registerNamespaceFallback($dirs)
{
$this->namespaceFallback = $dir;
$this->namespaceFallback = (array) $dirs;
}

/**
* Registers the directory to use as a fallback for class prefixes.
*
* @param string $dir A directory path
* @return string|array $dirs A directory path or an array of directories
*/
public function registerPrefixFallback($dir)
public function registerPrefixFallback($dirs)
{
$this->prefixFallback = $dir;
$this->prefixFallback = (array) $dirs;
}

/**
Expand All @@ -125,18 +126,20 @@ public function registerPrefixFallback($dir)
*/
public function registerNamespaces(array $namespaces)
{
$this->namespaces = array_merge($this->namespaces, $namespaces);
foreach ($namespaces as $namespace => $locations) {
$this->namespaces[$namespace] = (array) $locations;
}
}

/**
* Registers a namespace.
*
* @param string $namespace The namespace
* @param string $path The location of the namespace
* @param string $namespace The namespace
* @param array|string $paths The location(s) of the namespace
*/
public function registerNamespace($namespace, $path)
public function registerNamespace($namespace, $paths)
{
$this->namespaces[$namespace] = $path;
$this->namespaces[$namespace] = (array) $paths;
}

/**
Expand All @@ -146,18 +149,20 @@ public function registerNamespace($namespace, $path)
*/
public function registerPrefixes(array $classes)
{
$this->prefixes = array_merge($this->prefixes, $classes);
foreach ($classes as $prefix => $locations) {
$this->prefixes[$prefix] = (array) $locations;
}
}

/**
* Registers a set of classes using the PEAR naming convention.
*
* @param string $prefix The classes prefix
* @param string $path The location of the classes
* @param string $prefix The classes prefix
* @param array|string $paths The location(s) of the classes
*/
public function registerPrefix($prefix, $path)
public function registerPrefix($prefix, $paths)
{
$this->prefixes[$prefix] = $path;
$this->prefixes[$prefix] = (array) $paths;
}

/**
Expand All @@ -182,39 +187,45 @@ public function loadClass($class)
if (false !== ($pos = strripos($class, '\\'))) {
// namespaced class name
$namespace = substr($class, 0, $pos);
foreach ($this->namespaces as $ns => $dir) {
if (0 === strpos($namespace, $ns)) {
$className = substr($class, $pos + 1);
$file = $dir.DIRECTORY_SEPARATOR.str_replace('\\', DIRECTORY_SEPARATOR, $namespace).DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $className).'.php';
if (file_exists($file)) {
require $file;
return;
foreach ($this->namespaces as $ns => $dirs) {
foreach ($dirs as $dir) {
if (0 === strpos($namespace, $ns)) {
$className = substr($class, $pos + 1);
$file = $dir.DIRECTORY_SEPARATOR.str_replace('\\', DIRECTORY_SEPARATOR, $namespace).DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $className).'.php';
if (file_exists($file)) {
require $file;
return;
}
}
}
}

if (null !== $this->namespaceFallback) {
$file = $this->namespaceFallback.DIRECTORY_SEPARATOR.str_replace('\\', DIRECTORY_SEPARATOR, $class).'.php';
foreach ($this->namespaceFallback as $dir) {
$file = $dir.DIRECTORY_SEPARATOR.str_replace('\\', DIRECTORY_SEPARATOR, $class).'.php';
if (file_exists($file)) {
require $file;
return;
}
}
} else {
// PEAR-like class name
foreach ($this->prefixes as $prefix => $dir) {
if (0 === strpos($class, $prefix)) {
$file = $dir.DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $class).'.php';
if (file_exists($file)) {
require $file;
return;
foreach ($this->prefixes as $prefix => $dirs) {
foreach ($dirs as $dir) {
if (0 === strpos($class, $prefix)) {
$file = $dir.DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $class).'.php';
if (file_exists($file)) {
require $file;
return;
}
}
}
}

if (null !== $this->prefixFallback) {
$file = $this->prefixFallback.DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $class).'.php';
foreach ($this->prefixFallback as $dir) {
$file = $dir.DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $class).'.php';
if (file_exists($file)) {
require $file;
return;
}
}
}
Expand Down
68 changes: 39 additions & 29 deletions src/Symfony/Component/HttpKernel/bootstrap.php
Expand Up @@ -1918,8 +1918,8 @@ class UniversalClassLoader
{
protected $namespaces = array();
protected $prefixes = array();
protected $namespaceFallback;
protected $prefixFallback;
protected $namespaceFallback = array();
protected $prefixFallback = array();
public function getNamespaces()
{
return $this->namespaces;
Expand All @@ -1936,29 +1936,33 @@ public function getPrefixFallback()
{
return $this->prefixFallback;
}
public function registerNamespaceFallback($dir)
public function registerNamespaceFallback($dirs)
{
$this->namespaceFallback = $dir;
$this->namespaceFallback = (array) $dirs;
}
public function registerPrefixFallback($dir)
public function registerPrefixFallback($dirs)
{
$this->prefixFallback = $dir;
$this->prefixFallback = (array) $dirs;
}
public function registerNamespaces(array $namespaces)
{
$this->namespaces = array_merge($this->namespaces, $namespaces);
foreach ($namespaces as $namespace => $locations) {
$this->namespaces[$namespace] = (array) $locations;
}
}
public function registerNamespace($namespace, $path)
public function registerNamespace($namespace, $paths)
{
$this->namespaces[$namespace] = $path;
$this->namespaces[$namespace] = (array) $paths;
}
public function registerPrefixes(array $classes)
{
$this->prefixes = array_merge($this->prefixes, $classes);
foreach ($classes as $prefix => $locations) {
$this->prefixes[$prefix] = (array) $locations;
}
}
public function registerPrefix($prefix, $path)
public function registerPrefix($prefix, $paths)
{
$this->prefixes[$prefix] = $path;
$this->prefixes[$prefix] = (array) $paths;
}
public function register($prepend = false)
{
Expand All @@ -1969,36 +1973,42 @@ public function loadClass($class)
$class = ltrim($class, '\\');
if (false !== ($pos = strripos($class, '\\'))) {
$namespace = substr($class, 0, $pos);
foreach ($this->namespaces as $ns => $dir) {
if (0 === strpos($namespace, $ns)) {
$className = substr($class, $pos + 1);
$file = $dir.DIRECTORY_SEPARATOR.str_replace('\\', DIRECTORY_SEPARATOR, $namespace).DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $className).'.php';
if (file_exists($file)) {
require $file;
return;
foreach ($this->namespaces as $ns => $dirs) {
foreach ($dirs as $dir) {
if (0 === strpos($namespace, $ns)) {
$className = substr($class, $pos + 1);
$file = $dir.DIRECTORY_SEPARATOR.str_replace('\\', DIRECTORY_SEPARATOR, $namespace).DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $className).'.php';
if (file_exists($file)) {
require $file;
return;
}
}
}
}
if (null !== $this->namespaceFallback) {
$file = $this->namespaceFallback.DIRECTORY_SEPARATOR.str_replace('\\', DIRECTORY_SEPARATOR, $class).'.php';
foreach ($this->namespaceFallback as $dir) {
$file = $dir.DIRECTORY_SEPARATOR.str_replace('\\', DIRECTORY_SEPARATOR, $class).'.php';
if (file_exists($file)) {
require $file;
return;
}
}
} else {
foreach ($this->prefixes as $prefix => $dir) {
if (0 === strpos($class, $prefix)) {
$file = $dir.DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $class).'.php';
if (file_exists($file)) {
require $file;
return;
foreach ($this->prefixes as $prefix => $dirs) {
foreach ($dirs as $dir) {
if (0 === strpos($class, $prefix)) {
$file = $dir.DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $class).'.php';
if (file_exists($file)) {
require $file;
return;
}
}
}
}
if (null !== $this->prefixFallback) {
$file = $this->prefixFallback.DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $class).'.php';
foreach ($this->prefixFallback as $dir) {
$file = $dir.DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $class).'.php';
if (file_exists($file)) {
require $file;
return;
}
}
}
Expand Down

0 comments on commit 2d69369

Please sign in to comment.