Commit 618980c6 authored by Taylor Otwell's avatar Taylor Otwell

improvements to autoloader namespace handling.

parent bc1a7d07
...@@ -56,19 +56,21 @@ class Autoloader { ...@@ -56,19 +56,21 @@ class Autoloader {
require static::$mappings[$class]; require static::$mappings[$class];
} }
// If the class namespace is mapped to a directory, we will load the
// class using the PSR-0 standards from that directory; however, we
// will trim off the beginning of the namespace to account for
// the root of the mapped directory.
if ( ! is_null($info = static::namespaced($class)))
{
$class = substr($class, strlen($info['namespace']));
return static::load_psr($class, $info['directory']);
}
elseif (($slash = strpos($class, '\\')) !== false) elseif (($slash = strpos($class, '\\')) !== false)
{ {
$namespace = substr($class, 0, $slash); $namespace = substr($class, 0, $slash);
// If the class namespace is mapped to a directory, we will load the class
// using the PSR-0 standards from that directory; however, we will trim
// off the beginning of the namespace to account for files in the root
// of the mapped directory.
if ( ! is_null($directory = static::directory($class)))
{
return static::load_psr(substr($class, $slash + 1), $directory);
}
// If the class is namespaced to an existing bundle and the bundle has // If the class is namespaced to an existing bundle and the bundle has
// not been started, we will start the bundle and attempt to load the // not been started, we will start the bundle and attempt to load the
// class file again. If that fails, an error will be thrown by PHP. // class file again. If that fails, an error will be thrown by PHP.
...@@ -91,23 +93,6 @@ class Autoloader { ...@@ -91,23 +93,6 @@ class Autoloader {
static::load_psr($class); static::load_psr($class);
} }
/**
* Get the directory associated with a given namespaced class.
*
* @param string $class
* @return string
*/
protected static function directory($class)
{
foreach (static::$namespaces as $namespace => $directory)
{
if (starts_with($class, $namespace))
{
return $directory;
}
}
}
/** /**
* Attempt to resolve a class using the PSR-0 standard. * Attempt to resolve a class using the PSR-0 standard.
* *
...@@ -154,6 +139,26 @@ class Autoloader { ...@@ -154,6 +139,26 @@ class Autoloader {
} }
} }
/**
* Get the directory for a given namespaced class.
*
* @param string $class
* @return string
*/
protected static function namespaced($class)
{
foreach (static::$namespaces as $namespace => $directory)
{
// If the class begins with one of the registered namespaces,
// we'll return both the namespace and the directory, which
// will allow us to use PSR-0 to load the class.
if (starts_with($class, $namespace))
{
return compact('namespace', 'directory');
}
}
}
/** /**
* Register an array of class to path mappings. * Register an array of class to path mappings.
* *
...@@ -203,11 +208,26 @@ class Autoloader { ...@@ -203,11 +208,26 @@ class Autoloader {
*/ */
public static function namespaces($mappings) public static function namespaces($mappings)
{ {
$directories = static::format(array_values($mappings)); foreach ($mappings as $namespace => $directory)
{
$namespace = trim($namespace, '\\').'\\';
$mappings = array_combine(array_keys($mappings), $directories); static::$namespaces[$namespace] = head(static::format($directory));
}
}
static::$namespaces = array_merge(static::$namespaces, $mappings); /**
* Register underscored "namespaces" to directory mappings.
*
* @param array $mappings
* @return void
*/
public static function underscored($mappings)
{
foreach ($mappings as $namespace => $directory)
{
static::$namespaces[$namespace.'_'] = head(static::format($directory));
}
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment