added module support!

This commit is contained in:
Taylor Otwell
2011-08-03 22:10:07 -05:00
parent 927c522e67
commit f0f3dffc11
34 changed files with 363 additions and 203 deletions

View File

@@ -16,6 +16,13 @@ class Loader {
*/
private static $aliases = array();
/**
* All of the active modules.
*
* @var array
*/
private static $modules = array();
/**
* Bootstrap the auto-loader.
*
@@ -23,16 +30,15 @@ class Loader {
*/
public static function bootstrap()
{
static::$aliases = require CONFIG_PATH.'aliases'.EXT;
static::$aliases = Config::get('aliases');
static::$modules = Config::get('application.modules');
}
/**
* Load a class file for a given class name.
*
* This function is registered on the SPL auto-loader stack by the front controller during each request.
*
* All Laravel class names follow a namespace to directory convention. So, if a class exists in
* application/libraries/user, it shouold be placed in the "User" namespace.
* All Laravel class names follow a namespace to directory convention.
*
* @param string $class
* @return void
@@ -46,13 +52,60 @@ class Loader {
return class_alias(static::$aliases[$class], $class);
}
if ( ! static::load_from_registered($file))
{
static::load_from_module($file);
}
}
/**
* Load a class that is stored in the registered directories.
*
* @param string $file
* @return bool
*/
private static function load_from_registered($file)
{
foreach (static::$paths as $directory)
{
if (file_exists($path = $directory.$file.EXT))
{
require $path;
return;
return true;
}
}
}
/**
* Load a class that is stored in a module.
*
* @param string $file
* @return void
*/
private static function load_from_module($file)
{
// Since all module models and libraries must be namespaced to the
// module name, we'll extract the module name from the file.
$module = substr($file, 0, strpos($file, '/'));
if (in_array($module, static::$modules))
{
$module = MODULE_PATH.$module.'/';
// Slice the module name off of the filename. Even though module libraries
// and models are namespaced under the module, there will obviously not be
// a folder matching that namespace in the libraries or models directories
// of the module. Slicing it off will allow us to make a clean search for
// the relevant class file.
$file = substr($file, strpos($file, '/') + 1);
foreach (array($module.'models', $module.'libraries') as $directory)
{
if (file_exists($path = $directory.'/'.$file.EXT))
{
return require $path;
}
}
}
}