refactoring.

This commit is contained in:
Taylor Otwell
2011-09-27 21:10:32 -05:00
parent ed3e3e73cc
commit c6f9734603
15 changed files with 253 additions and 178 deletions

View File

@@ -7,32 +7,56 @@ class Loader {
*
* @var array
*/
public static $paths = array();
protected $paths = array();
/**
* The class aliases defined for the application.
*
* @var array
*/
public static $aliases = array();
protected $aliases = array();
/**
* Create a new class loader instance.
*
* @param array $paths
* @param array $aliases
* @return void
*/
public function __construct($paths, $aliases = array())
{
$this->paths = $paths;
$this->aliases = $aliases;
}
/**
* Load the file for a given class.
*
* <code>
* // Load the file for the "User" class
* Loader::load('User');
*
* // Load the file for the "Repositories\User" class
* Loader::load('Repositories\\User');
* </code>
*
* @param string $class
* @return void
*/
public static function load($class)
public function load($class)
{
// All Laravel core classes follow a namespace to directory convention. So, we will
// replace all of the namespace slashes with directory slashes.
// All Laravel core classes follow a namespace to directory convention.
// We will replace all of the namespace slashes with directory slashes.
$file = strtolower(str_replace('\\', '/', $class));
// First, we'll check to determine if an alias exists. If it does, we will define the
// alias and bail out. Aliases are defined for most developer used core classes.
if (array_key_exists($class, static::$aliases)) return class_alias(static::$aliases[$class], $class);
// Check to determine if an alias exists. If it does, we will define the
// alias and bail out. Aliases are defined for most used core classes.
if (array_key_exists($class, $this->aliases))
{
return class_alias($this->aliases[$class], $class);
}
foreach (static::$paths as $path)
foreach ($this->paths as $path)
{
if (file_exists($path = $path.$file.EXT))
{
@@ -46,15 +70,13 @@ class Loader {
/**
* Register a class alias with the auto-loader.
*
* Note: Aliases are lazy-loaded, so the aliased class will not be included until it is needed.
*
* @param string $alias
* @param string $class
* @return void
*/
public static function alias($alias, $class)
public function alias($alias, $class)
{
static::$aliases[$alias] = $class;
$this->aliases[$alias] = $class;
}
/**
@@ -63,9 +85,9 @@ class Loader {
* @param string $path
* @return void
*/
public static function path($path)
public function path($path)
{
static::$paths[] = rtrim($path, '/').'/';
$this->paths[] = rtrim($path, '/').'/';
}
/**
@@ -74,9 +96,9 @@ class Loader {
* @param string $alias
* @return void
*/
public static function forget_alias($alias)
public function forget_alias($alias)
{
unset(static::$aliases[$alias]);
unset($this->aliases[$alias]);
}
}