Browse files

[ClassLoader] added the possibility to define more than one directory…

… for a namespace or a prefix
  • Loading branch information...
1 parent 5fe18c1 commit 6a144c7ca6702c950de4294b92a5e636a4f6b00a @fabpot fabpot committed Feb 3, 2011
Showing with 53 additions and 42 deletions.
  1. +53 −42 UniversalClassLoader.php
View
95 UniversalClassLoader.php
@@ -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
@@ -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.
@@ -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()
{
@@ -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;
}
/**
@@ -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;
}
/**
@@ -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;
}
/**
@@ -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;
}
}
}

0 comments on commit 6a144c7

Please sign in to comment.