added loader class.

This commit is contained in:
Taylor Otwell 2011-08-02 22:19:13 -05:00
parent c70f090465
commit fb984016e8
2 changed files with 74 additions and 18 deletions

View File

@ -67,31 +67,16 @@
// -------------------------------------------------------------- // --------------------------------------------------------------
// Load the classes used by the auto-loader. // Load the classes used by the auto-loader.
// -------------------------------------------------------------- // --------------------------------------------------------------
require SYS_PATH.'loader'.EXT;
require SYS_PATH.'config'.EXT; require SYS_PATH.'config'.EXT;
require SYS_PATH.'arr'.EXT; require SYS_PATH.'arr'.EXT;
// -------------------------------------------------------------- // --------------------------------------------------------------
// Register the auto-loader. // Register the auto-loader.
// -------------------------------------------------------------- // --------------------------------------------------------------
spl_autoload_register(function($class) System\Loader::bootstrap();
{
$file = strtolower(str_replace('\\', '/', $class));
if (array_key_exists($class, $aliases = System\Config::get('aliases'))) spl_autoload_register(array('System\\Loader', 'load'));
{
return class_alias($aliases[$class], $class);
}
foreach (array(BASE_PATH, MODEL_PATH, LIBRARY_PATH) as $directory)
{
if (file_exists($path = $directory.$file.EXT))
{
require $path;
return;
}
}
});
// -------------------------------------------------------------- // --------------------------------------------------------------
// Register the framework starting time with the Benchmarker. // Register the framework starting time with the Benchmarker.

71
system/loader.php Normal file
View File

@ -0,0 +1,71 @@
<?php namespace System;
class Loader {
/**
* The paths to be searched by the loader.
*
* @var array
*/
private static $paths = array(BASE_PATH, MODEL_PATH, LIBRARY_PATH);
/**
* All of the class aliases.
*
* @var array
*/
private static $aliases = array();
/**
* Bootstrap the auto-loader.
*
* @return void
*/
public static function bootstrap()
{
static::$aliases = require CONFIG_PATH.'aliases'.EXT;
}
/**
* 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.
*
* @param string $class
* @return void
*/
public static function load($class)
{
$file = strtolower(str_replace('\\', '/', $class));
if (array_key_exists($class, static::$aliases))
{
return class_alias(static::$aliases[$class], $class);
}
foreach (static::$paths as $directory)
{
if (file_exists($path = $directory.$file.EXT))
{
require $path;
return;
}
}
}
/**
* Register a path with the auto-loader.
*
* @param string $path
* @return void
*/
public static function register($path)
{
static::$paths[] = $path;
}
}