refactoring the autoloader.

This commit is contained in:
Taylor Otwell 2011-10-31 23:00:13 -05:00
parent f824cc0f43
commit d44c076ebf
5 changed files with 24 additions and 52 deletions

0
application/models/.gitignore vendored Normal file
View File

View File

@ -2,37 +2,19 @@
class Autoloader { class Autoloader {
/**
* The class alises defined for the application.
*
* @var array
*/
protected $aliases = array();
/** /**
* The PSR-0 compliant libraries registered with the auto-loader. * The PSR-0 compliant libraries registered with the auto-loader.
* *
* @var array * @var array
*/ */
protected $libraries = array(); protected static $libraries = array();
/** /**
* The paths to be searched by the auto-loader. * The paths to be searched by the auto-loader.
* *
* @var array * @var array
*/ */
protected $paths = array(BASE_PATH, CLASS_PATH); protected static $paths = array(BASE_PATH, MODEL_PATH, LIBRARY_PATH);
/**
* Create a new auto-loader instance.
*
* @param array $aliases
* @return void
*/
public function __construct($aliases = array())
{
$this->aliases = $aliases;
}
/** /**
* Load the file corresponding to a given class. * Load the file corresponding to a given class.
@ -40,30 +22,30 @@ public function __construct($aliases = array())
* @param string $class * @param string $class
* @return void * @return void
*/ */
public function load($class) public static function load($class)
{ {
// Most of the core classes are aliases for convenient access in spite // Most of the core classes are aliases for convenient access in spite of
// of the namespace. If an alias is defined for the class, we will load // the namespace. If an alias is defined for the class, we will load the
// the alias and bail out of the auto-load method. // alias and bail out of the auto-load method.
if (array_key_exists($class, $this->aliases)) if (array_key_exists($class, Config::$items['application']['aliases']))
{ {
return class_alias($this->aliases[$class], $class); return class_alias(Config::$items['application']['aliases'][$class], $class);
} }
$file = str_replace('\\', '/', $class); $file = str_replace('\\', '/', $class);
$namespace = substr($class, 0, strpos($class, '\\')); $namespace = substr($class, 0, strpos($class, '\\'));
// If the class namespace exists in the libraries array, it means that // If the class namespace exists in the libraries array, it means that the
// the library is PSR-0 compliant, and we will load it following those // library is PSR-0 compliant, and we will load it following those standards.
// standards. This allows us to add many third-party libraries to an // This allows us to add many third-party libraries to an application and be
// application and be able to auto-load them automatically. // able to auto-load them automatically.
if (array_key_exists($namespace, $this->libraries)) if (array_key_exists($namespace, static::$libraries))
{ {
require CLASS_PATH.$this->psr($file); require LIBRARY_PATH.str_replace('_', '/', $file);
} }
foreach ($this->paths as $path) foreach (static::$paths as $path)
{ {
if (file_exists($path = $path.strtolower($file).EXT)) if (file_exists($path = $path.strtolower($file).EXT))
{ {
@ -73,26 +55,15 @@ public function load($class)
} }
} }
// If the namespace exists in the classes directory, we will assume the // If the namespace exists in the libraries directory, we will assume the
// library is PSR-0 compliant, and will add the namespace to the array // library is PSR-0 compliant, and will add the namespace to the array of
// of libraries and load the class accordingly. // libraries and load the class accordingly.
if (is_dir(CLASS_PATH.$namespace)) if (is_dir(LIBRARY_PATH.$namespace))
{ {
$this->libraries[] = $namespace; static::$libraries[] = $namespace;
require CLASS_PATH.$this->psr($file); require LIBRARY_PATH.str_replace('_', '/', $file);
} }
} }
/**
* Format a path for PSR-0 compliant auto-loading.
*
* @param string $file
* @return string
*/
protected function psr($file)
{
return str_replace('_', '/', $file);
}
} }

View File

@ -26,11 +26,12 @@ function constants($constants)
$constants = array( $constants = array(
'CACHE_PATH' => STORAGE_PATH.'cache/', 'CACHE_PATH' => STORAGE_PATH.'cache/',
'CLASS_PATH' => APP_PATH.'classes/',
'CONFIG_PATH' => APP_PATH.'config/', 'CONFIG_PATH' => APP_PATH.'config/',
'CONTROLLER_PATH' => APP_PATH.'controllers/', 'CONTROLLER_PATH' => APP_PATH.'controllers/',
'DATABASE_PATH' => STORAGE_PATH.'database/', 'DATABASE_PATH' => STORAGE_PATH.'database/',
'LANG_PATH' => APP_PATH.'language/', 'LANG_PATH' => APP_PATH.'language/',
'LIBRARY_PATH' => APP_PATH.'libraries/',
'MODEL_PATH' => APP_PATH.'models/',
'ROUTE_PATH' => APP_PATH.'routes/', 'ROUTE_PATH' => APP_PATH.'routes/',
'SESSION_PATH' => STORAGE_PATH.'sessions/', 'SESSION_PATH' => STORAGE_PATH.'sessions/',
'SYS_CONFIG_PATH' => SYS_PATH.'config/', 'SYS_CONFIG_PATH' => SYS_PATH.'config/',

View File

@ -14,7 +14,7 @@
IoC::bootstrap(); IoC::bootstrap();
spl_autoload_register(array(IoC::container()->core('autoloader'), 'load')); spl_autoload_register(array('Laravel\\Autoloader', 'load'));
function e($value) function e($value)
{ {