refactoring various classes.
This commit is contained in:
@@ -3,37 +3,30 @@
|
||||
class Autoloader {
|
||||
|
||||
/**
|
||||
* The PSR-0 compliant libraries registered with the auto-loader.
|
||||
* The mappings from class names to file paths.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected static $libraries = array();
|
||||
public static $mappings = array();
|
||||
|
||||
/**
|
||||
* The paths to be searched by the auto-loader.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected static $paths = array(BASE_PATH, MODEL_PATH, LIBRARY_PATH);
|
||||
protected static $paths = array(MODEL_PATH, LIBRARY_PATH);
|
||||
|
||||
/**
|
||||
* Load the file corresponding to a given class.
|
||||
*
|
||||
* Laravel loads classes out of three directories: the core "laravel" directory,
|
||||
* and the application "models" and "libraries" directories. All of the file
|
||||
* names are lower cased and the directory structure corresponds with the
|
||||
* class namespaces.
|
||||
*
|
||||
* The application "libraries" directory also supports the inclusion of PSR-0
|
||||
* compliant libraries. These libraries will be detected automatically and
|
||||
* will be loaded according to the PSR-0 naming conventions.
|
||||
* This method is registerd in the core bootstrap file as an SPL Autoloader.
|
||||
*
|
||||
* @param string $class
|
||||
* @return void
|
||||
*/
|
||||
public static function load($class)
|
||||
{
|
||||
if (array_key_exists($class, Config::$items['application']['aliases']))
|
||||
if (isset(Config::$items['application']['aliases'][$class]))
|
||||
{
|
||||
return class_alias(Config::$items['application']['aliases'][$class], $class);
|
||||
}
|
||||
@@ -52,27 +45,26 @@ class Autoloader {
|
||||
*/
|
||||
protected static function find($class)
|
||||
{
|
||||
// After PHP namespaces were introduced, most libaries ditched underscores for
|
||||
// namespaces to indicate the class directory hierarchy. We will check for the
|
||||
// presence of namespace slashes to determine the directory separator.
|
||||
$separator = (strpos($class, '\\') !== false) ? '\\' : '_';
|
||||
// First we will look for the class in the hard-coded class mappings, since
|
||||
// this is the fastest way to resolve a class name to its associated file.
|
||||
// This saves us from having to search through the file system manually.
|
||||
if (isset(static::$mappings[$class]))
|
||||
{
|
||||
return static::$mappings[$class];
|
||||
}
|
||||
|
||||
$library = substr($class, 0, strpos($class, $separator));
|
||||
|
||||
$file = str_replace('\\', '/', $class);
|
||||
|
||||
// If the namespace has been registered as a PSR-0 compliant library, we will
|
||||
// If the library has been registered as a PSR-0 compliant library, we will
|
||||
// load the library according to the PSR-0 naming standards, which state that
|
||||
// namespaces and underscores indicate the directory hierarchy of the class.
|
||||
if (isset(static::$libraries[$library]))
|
||||
if (isset(static::$libraries[static::library($class)]))
|
||||
{
|
||||
return LIBRARY_PATH.str_replace('_', '/', $file).EXT;
|
||||
return LIBRARY_PATH.str_replace(array('\\', '_'), '/', $class).EXT;
|
||||
}
|
||||
|
||||
// Next we will search through the common Laravel paths for the class file.
|
||||
// The Laravel framework path, along with the libraries and models paths
|
||||
// will be searched according to the Laravel class naming standard.
|
||||
$lower = strtolower($file);
|
||||
// The Laravel libraries and models directories will be searched according
|
||||
// to the Laravel class naming standards.
|
||||
$file = strtolower(str_replace('\\', '/', $class));
|
||||
|
||||
foreach (static::$paths as $path)
|
||||
{
|
||||
@@ -82,29 +74,105 @@ class Autoloader {
|
||||
}
|
||||
}
|
||||
|
||||
// If we could not find the class file in any of the auto-loaded locations
|
||||
// according to the Laravel naming standard, we will search the libraries
|
||||
// directory for the class according to the PSR-0 naming standard.
|
||||
if (file_exists($path = LIBRARY_PATH.str_replace('_', '/', $file).EXT))
|
||||
{
|
||||
static::$libraries[] = $library;
|
||||
|
||||
return $path;
|
||||
}
|
||||
|
||||
// Since not all controllers will be resolved by the controller resolver,
|
||||
// we will do a quick check in the controller directory for the class.
|
||||
// For instance, since base controllers would not be resolved by the
|
||||
// controller class, we will need to resolve them here.
|
||||
if (strpos($class, '_Controller') !== false)
|
||||
if (file_exists($path = static::controller($class)))
|
||||
{
|
||||
$controller = str_replace(array('_Controller', '_'), array('', '/'), $class);
|
||||
|
||||
if (file_exists($path = strtolower(CONTROLLER_PATH.$controller.EXT)))
|
||||
{
|
||||
return $path;
|
||||
}
|
||||
return $path;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract the "library" name from the given class.
|
||||
*
|
||||
* The library name is essentially the namespace, or the string that preceeds
|
||||
* the first PSR-0 separator. PSR-0 states that namespaces or undescores may
|
||||
* be used to indicate the directory structure in which the file resides.
|
||||
*
|
||||
* @param string $class
|
||||
* @return string
|
||||
*/
|
||||
protected static function library($class)
|
||||
{
|
||||
if (($separator = strpos($class, '\\')) !== false)
|
||||
{
|
||||
return substr($class, 0, $separator);
|
||||
}
|
||||
elseif (($separator = strpos($class, '_')) !== false)
|
||||
{
|
||||
return substr($class, 0, $separator);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Translate a given controller class name into the corresponding file name.
|
||||
*
|
||||
* The controller suffix will be removed, and the underscores will be translated
|
||||
* into directory slashes. Of course, the entire class name will be converted to
|
||||
* lower case as well.
|
||||
*
|
||||
* <code>
|
||||
* // Returns "user/profile"...
|
||||
* $file = static::controller('User_Profile_Controller');
|
||||
* </code>
|
||||
*
|
||||
* @param string $class
|
||||
* @return string
|
||||
*/
|
||||
protected static function controller($class)
|
||||
{
|
||||
$controller = str_replace(array('_', '_Controller'), array('/', ''), $class);
|
||||
|
||||
return CONTROLLER_PATH.strtolower($controller).EXT;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register an array of class to path mappings.
|
||||
*
|
||||
* The mappings will be used to resolve file paths from class names when
|
||||
* a class is lazy loaded through the Autoloader, providing a faster way
|
||||
* of resolving file paths than the typical file_exists method.
|
||||
*
|
||||
* <code>
|
||||
* // Register a class mapping with the Autoloader
|
||||
* Autoloader::maps(array('User' => MODEL_PATH.'user'.EXT));
|
||||
* </code>
|
||||
*
|
||||
* @param array $mappings
|
||||
* @return void
|
||||
*/
|
||||
public static function maps($mappings)
|
||||
{
|
||||
foreach ($mappings as $class => $path)
|
||||
{
|
||||
static::$mappings[$class] = $path;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register PSR-0 libraries with the Autoloader.
|
||||
*
|
||||
* The library names given to this method should match directories within
|
||||
* the application libraries directory. This method provides an easy way
|
||||
* to indicate that some libraries should be loaded using the PSR-0
|
||||
* naming conventions instead of the Laravel conventions.
|
||||
*
|
||||
* <code>
|
||||
* // Register the "Assetic" library with the Autoloader
|
||||
* Autoloader::libraries('Assetic');
|
||||
*
|
||||
* // Register several libraries with the Autoloader
|
||||
* Autoloader::libraries(array('Assetic', 'Twig'));
|
||||
* </code>
|
||||
*
|
||||
* @param array $libraries
|
||||
* @return void
|
||||
*/
|
||||
public static function libraries($libraries)
|
||||
{
|
||||
static::$libraries = array_merge(static::$libraries, (array) $libraries);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user