Files
lgp-admin-plus-api/laravel/autoloader.php

85 lines
2.1 KiB
PHP
Raw Normal View History

2011-10-31 22:15:47 -05:00
<?php namespace Laravel;
class Autoloader {
/**
* The PSR-0 compliant libraries registered with the auto-loader.
*
* @var array
*/
2011-10-31 23:58:45 -05:00
protected static $libraries = array();
2011-10-31 22:15:47 -05:00
/**
* The paths to be searched by the auto-loader.
*
* @var array
*/
2011-10-31 23:58:45 -05:00
protected static $paths = array(BASE_PATH, MODEL_PATH, LIBRARY_PATH);
2011-10-31 22:15:47 -05:00
/**
* 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
2011-11-01 19:41:35 -05:00
* 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.
*
2011-10-31 22:15:47 -05:00
* @param string $class
* @return void
*/
2011-10-31 23:58:45 -05:00
public static function load($class)
2011-10-31 22:15:47 -05:00
{
2011-10-31 23:00:13 -05:00
if (array_key_exists($class, Config::$items['application']['aliases']))
2011-10-31 22:15:47 -05:00
{
2011-10-31 23:00:13 -05:00
return class_alias(Config::$items['application']['aliases'][$class], $class);
2011-10-31 22:15:47 -05:00
}
2011-10-31 23:58:45 -05:00
if ( ! is_null($path = static::find($class)))
2011-10-31 23:46:45 -05:00
{
require $path;
}
}
/**
2011-11-01 19:41:35 -05:00
* Determine the file path associated with a given class name.
2011-10-31 23:46:45 -05:00
*
* @param string $class
* @return string
*/
2011-10-31 23:58:45 -05:00
protected static function find($class)
2011-10-31 23:46:45 -05:00
{
2011-10-31 22:15:47 -05:00
$file = str_replace('\\', '/', $class);
$namespace = substr($class, 0, strpos($class, '\\'));
2011-11-01 19:41:35 -05:00
// If the namespace has been detected as a PSR-0 compliant library,
// we will load the library according to those naming conventions.
2011-10-31 23:58:45 -05:00
if (array_key_exists($namespace, static::$libraries))
2011-10-31 22:15:47 -05:00
{
2011-11-01 19:41:35 -05:00
return str_replace('_', '/', $file).EXT;
2011-10-31 22:15:47 -05:00
}
2011-10-31 23:58:45 -05:00
foreach (static::$paths as $path)
2011-10-31 22:15:47 -05:00
{
if (file_exists($path = $path.strtolower($file).EXT))
{
2011-10-31 23:46:45 -05:00
return $path;
2011-10-31 22:15:47 -05:00
}
}
2011-11-01 19:41:35 -05:00
// If the file exists according to the PSR-0 naming conventions,
// we will add the namespace to the array of libraries and load
// the class according to the PSR-0 conventions.
if (file_exists($path = str_replace('_', '/', $file).EXT))
2011-10-31 22:15:47 -05:00
{
2011-10-31 23:58:45 -05:00
static::$libraries[] = $namespace;
2011-10-31 22:15:47 -05:00
return $path;
2011-10-31 22:15:47 -05:00
}
}
}