autoloader.php 3.3 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php namespace Laravel;

class Autoloader {

	/**
	 * The PSR-0 compliant libraries registered with the auto-loader.
	 *
	 * @var array
	 */
10
	protected static $libraries = array();
11 12 13 14 15 16

	/**
	 * The paths to be searched by the auto-loader.
	 *
	 * @var array
	 */
17
	protected static $paths = array(BASE_PATH, MODEL_PATH, LIBRARY_PATH);
18 19 20 21

	/**
	 * Load the file corresponding to a given class.
	 *
22 23
	 * Laravel loads classes out of three directories: the core "laravel" directory,
	 * and the application "models" and "libraries" directories. All of the file
Taylor Otwell committed
24 25 26 27 28 29 30
	 * 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.
	 *
31 32 33
	 * @param  string  $class
	 * @return void
	 */
34
	public static function load($class)
35
	{
36
		if (array_key_exists($class, Config::$items['application']['aliases']))
37
		{
38
			return class_alias(Config::$items['application']['aliases'][$class], $class);
39 40
		}

41
		if ( ! is_null($path = static::find($class)))
42 43 44 45 46 47
		{
			require $path;
		}
	}

	/**
Taylor Otwell committed
48
	 * Determine the file path associated with a given class name.
49 50 51 52
	 *
	 * @param  string  $class
	 * @return string
	 */
53
	protected static function find($class)
54
	{
55
		// After PHP namespaces were introduced, most libaries ditched underscores for
Taylor Otwell committed
56
		// for namespaces to indicate the class directory hierarchy. We will check for
Taylor Otwell committed
57
		// the presence of namespace slashes to determine the directory separator.
Taylor Otwell committed
58 59 60
		$separator = (strpos($class, '\\') !== false) ? '\\' : '_';

		$library = substr($class, 0, strpos($class, $separator));
61

62
		$file = str_replace('\\', '/', $class);
63

64 65 66
		// If the namespace 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.
67
		if (isset(static::$libraries[$library]))
68
		{
Taylor Otwell committed
69
			return LIBRARY_PATH.str_replace('_', '/', $file).EXT;
70 71
		}

Taylor Otwell committed
72 73 74 75 76
		// 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);

77
		foreach (static::$paths as $path)
78
		{
Taylor Otwell committed
79
			if (file_exists($path = $path.$lower.EXT))
80
			{
81
				return $path;
82 83 84
			}
		}

85 86
		// 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
87
		// directory for the class according to the PSR-0 naming standard.
88
		if (file_exists($path = LIBRARY_PATH.str_replace('_', '/', $file).EXT))
89
		{
90
			static::$libraries[] = $library;
91

92
			return $path;
93
		}
94 95 96 97 98 99 100 101 102 103 104 105 106 107

		// 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)
		{
			$controller = str_replace(array('_Controller', '_'), array('', '/'), $class);

			if (file_exists($path = strtolower(CONTROLLER_PATH.$controller.EXT)))
			{
				return $path;
			}
		}
108 109 110
	}

}