Commit ca5dfa40 authored by Taylor Otwell's avatar Taylor Otwell

fixing underscored library auto-loading.

Signed-off-by: 's avatarTaylor Otwell <taylorotwell@gmail.com>
parent 2fdde4ad
...@@ -23,6 +23,13 @@ class Autoloader { ...@@ -23,6 +23,13 @@ class Autoloader {
*/ */
public static $namespaces = array(); public static $namespaces = array();
/**
* The mappings for underscored libraries to directories.
*
* @var array
*/
public static $underscored = array();
/** /**
* All of the class aliases registered with the auto-loader. * All of the class aliases registered with the auto-loader.
* *
...@@ -56,21 +63,49 @@ class Autoloader { ...@@ -56,21 +63,49 @@ class Autoloader {
require static::$mappings[$class]; require static::$mappings[$class];
} }
$namespace = root_namespace($class).'\\';
// If the class namespace is mapped to a directory, we will load the // If the class namespace is mapped to a directory, we will load the
// class using the PSR-0 standards from that directory accounting // class using the PSR-0 standards from that directory accounting
// for the root of the namespace by trimming it. // for the root of the namespace by trimming it off.
$namespace = root_namespace($class).'\\';
if (isset(static::$namespaces[$namespace])) if (isset(static::$namespaces[$namespace]))
{ {
$class = substr($class, strlen($namespace)); $directory = static::$namespaces[$namespace];
return static::load_psr($class, static::$namespaces[$namespace]); return static::load_namespaced($class, $namespace, $directory);
} }
// If the class uses PEARish style underscores for indicating its
// directory structure, we will load the class using the PSR-0
// standards from that directory, trimming the root.
$namespace = root_namespace($class, '_').'_';
if (isset(static::$underscored[$namespace]))
{
$directory = static::$underscored[$namespace];
return static::load_namespaced($class, $namespace, $directory);
}
// If all else fails we will just iterator through the mapped
// PSR-0 directories looking for the class. This is the last
// resort and slowest loading option for the class.
static::load_psr($class); static::load_psr($class);
} }
/**
* Load a namespaced class from a given directory.
*
* @param string $class
* @param string $namespace
* @param string $directory
* @return void
*/
protected static function load_namespaced($class, $namespace, $directory)
{
return static::load_psr(substr($class, strlen($namespace)), $directory);
}
/** /**
* Attempt to resolve a class using the PSR-0 standard. * Attempt to resolve a class using the PSR-0 standard.
* *
...@@ -149,17 +184,32 @@ class Autoloader { ...@@ -149,17 +184,32 @@ class Autoloader {
*/ */
public static function underscored($mappings) public static function underscored($mappings)
{ {
static::namespaces($mappings, '_'); $mappings = static::format_mappings($mappings, '_');
static::$underscored = array_merge($mappings, static::$underscored);
} }
/** /**
* Map namespaces to directories. * Map namespaces to directories.
* *
* @param array $mappings
* @return void
*/
public static function namespaces($mappings)
{
$mappings = static::format_mappings($mappings, '\\');
static::$namespaces = array_merge($mappings, static::$namespaces);
}
/**
* Format an array of namespace to directory mappings.
*
* @param array $mappings * @param array $mappings
* @param string $append * @param string $append
* @return void * @return array
*/ */
public static function namespaces($mappings, $append = '\\') protected static function format_mappings($mappings, $append)
{ {
foreach ($mappings as $namespace => $directory) foreach ($mappings as $namespace => $directory)
{ {
...@@ -173,11 +223,7 @@ class Autoloader { ...@@ -173,11 +223,7 @@ class Autoloader {
$namespaces[$namespace] = head(static::format($directory)); $namespaces[$namespace] = head(static::format($directory));
} }
// We'll array_merge the new mappings onto the front of the array so return $namespaces;
// derivative namespaces are not always shadowed by their parents.
// For instance, when mappings Laravel\Docs, we don't want the
// main Laravel namespace to always override it.
static::$namespaces = array_merge($namespaces, static::$namespaces);
} }
/** /**
......
...@@ -365,13 +365,14 @@ function str_finish($value, $cap) ...@@ -365,13 +365,14 @@ function str_finish($value, $cap)
* Get the root namespace of a given class. * Get the root namespace of a given class.
* *
* @param string $class * @param string $class
* @param string $separator
* @return string * @return string
*/ */
function root_namespace($class) function root_namespace($class, $separator = '\\')
{ {
if (str_contains($class, '\\')) if (str_contains($class, $separator))
{ {
return head(explode('\\', $class)); return head(explode($separator, $class));
} }
} }
......
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