autoloader.php 5.33 KB
Newer Older
Taylor Otwell committed
1
<?php namespace Laravel;
2 3 4 5

class Autoloader {

	/**
6
	 * The mappings from class names to file paths.
7 8 9
	 *
	 * @var array
	 */
10
	public static $mappings = array();
11 12

	/**
13
	 * The directories that use the PSR-0 naming convention.
Taylor Otwell committed
14 15 16
	 *
	 * @var array
	 */
17
	public static $directories = array();
Taylor Otwell committed
18 19

	/**
20
	 * The mappings for namespaces to directories.
21 22 23
	 *
	 * @var array
	 */
24
	public static $namespaces = array();
25 26

	/**
27 28 29 30 31 32 33
	 * The mappings for underscored libraries to directories.
	 *
	 * @var array
	 */
	public static $underscored = array();

	/**
34
	 * All of the class aliases registered with the auto-loader.
35 36 37
	 *
	 * @var array
	 */
38
	public static $aliases = array();
39 40

	/**
41 42
	 * Load the file corresponding to a given class.
	 *
43
	 * This method is registered in the bootstrap file as an SPL auto-loader.
Taylor Otwell committed
44
	 *
45 46 47
	 * @param  string  $class
	 * @return void
	 */
48
	public static function load($class)
49
	{
50 51
		// First, we will check to see if the class has been aliased. If it has,
		// we will register the alias, which may cause the auto-loader to be
52
		// called again for the "real" class name to load its file.
53
		if (isset(static::$aliases[$class]))
54
		{
Taylor Otwell committed
55
			return class_alias(static::$aliases[$class], $class);
56 57
		}

58
		// All classes in Laravel are statically mapped. There is no crazy search
59 60 61
		// routine that digs through directories. It's just a simple array of
		// class to file path maps for ultra-fast file loading.
		elseif (isset(static::$mappings[$class]))
62
		{
63
			require static::$mappings[$class];
64 65

			return;
66
		}
67

68
		// If the class namespace is mapped to a directory, we will load the
69
		// class using the PSR-0 standards from that directory accounting
70
		// for the root of the namespace by trimming it off.
71
		foreach (static::$namespaces as $namespace => $directory)
72
		{
73 74 75 76
			if (starts_with($class, $namespace))
			{
				return static::load_namespaced($class, $namespace, $directory);
			}
77 78
		}

79
		static::load_psr($class);
80
	}
81

82
	/**
83 84 85 86 87 88 89 90 91 92 93 94 95
	 * Load a namespaced class from a given directory.
	 *
	 * @param  string  $class
	 * @param  string  $namespace
	 * @param  string  $directory
	 * @return void
	 */
	protected static function load_namespaced($class, $namespace, $directory)
	{
		return static::load_psr(substr($class, strlen($namespace)), $directory);
	}

	/**
96
	 * Attempt to resolve a class using the PSR-0 standard.
97 98
	 *
	 * @param  string  $class
99
	 * @param  string  $directory
100
	 * @return void
101
	 */
102
	protected static function load_psr($class, $directory = null)
103
	{
104
		// The PSR-0 standard indicates that class namespaces and underscores
105
		// should be used to indicate the directory tree in which the class
106
		// resides, so we'll convert them to slashes.
107 108
		$file = str_replace(array('\\', '_'), '/', $class);

109
		$directories = $directory ?: static::$directories;
110

111 112
		$lower = strtolower($file);

113 114 115 116
		// Once we have formatted the class name, we'll simply spin through
		// the registered PSR-0 directories and attempt to locate and load
		// the class file into the script.
		foreach ((array) $directories as $directory)
117
		{
118
			if (file_exists($path = $directory.$lower.EXT))
119 120 121 122 123 124 125
			{
				return require $path;
			}
			elseif (file_exists($path = $directory.$file.EXT))
			{
				return require $path;
			}
126 127 128 129
		}
	}

	/**
130
	 * Register an array of class to path mappings.
131
	 *
132 133
	 * @param  array  $mappings
	 * @return void
134
	 */
135
	public static function map($mappings)
136
	{
137
		static::$mappings = array_merge(static::$mappings, $mappings);
138 139 140
	}

	/**
141
	 * Register a class alias with the auto-loader.
142
	 *
143 144
	 * @param  string  $class
	 * @param  string  $alias
145 146
	 * @return void
	 */
147
	public static function alias($class, $alias)
148
	{
149
		static::$aliases[$alias] = $class;
150 151
	}

152
	/**
153
	 * Register directories to be searched as a PSR-0 library.
154
	 *
155
	 * @param  string|array  $directory
156 157
	 * @return void
	 */
158
	public static function directories($directory)
159
	{
160
		$directories = static::format($directory);
161

162
		static::$directories = array_unique(array_merge(static::$directories, $directories));
163 164
	}

165
	/**
Taylor Otwell committed
166
	 * Map namespaces to directories.
167
	 *
Taylor Otwell committed
168 169
	 * @param  array   $mappings
	 * @param  string  $append
Phill Sparks committed
170
	 * @return void
171
	 */
Taylor Otwell committed
172
	public static function namespaces($mappings, $append = '\\')
173
	{
Taylor Otwell committed
174
		$mappings = static::format_mappings($mappings, $append);
175

Taylor Otwell committed
176
		static::$namespaces = array_merge($mappings, static::$namespaces);
177
	}
178

179
	/**
Taylor Otwell committed
180
	 * Register underscored "namespaces" to directory mappings.
181
	 *
182 183 184
	 * @param  array  $mappings
	 * @return void
	 */
Taylor Otwell committed
185
	public static function underscored($mappings)
186
	{
Taylor Otwell committed
187
		static::namespaces($mappings, '_');
188 189 190 191 192
	}

	/**
	 * Format an array of namespace to directory mappings.
	 *
Taylor Otwell committed
193 194
	 * @param  array   $mappings
	 * @param  string  $append
195
	 * @return array
196
	 */
197
	protected static function format_mappings($mappings, $append)
198 199 200
	{
		foreach ($mappings as $namespace => $directory)
		{
Taylor Otwell committed
201 202 203 204 205 206 207 208
			// When adding new namespaces to the mappings, we will unset the previously
			// mapped value if it existed. This allows previously registered spaces to
			// be mapped to new directories on the fly.
			$namespace = trim($namespace, $append).$append;

			unset(static::$namespaces[$namespace]);

			$namespaces[$namespace] = head(static::format($directory));
209
		}
Taylor Otwell committed
210

211
		return $namespaces;
212 213
	}

214 215 216 217 218 219 220 221 222 223 224
	/**
	 * Format an array of directories with the proper trailing slashes.
	 *
	 * @param  array  $directories
	 * @return array
	 */
	protected static function format($directories)
	{
		return array_map(function($directory)
		{
			return rtrim($directory, DS).DS;
225
		
226 227 228
		}, (array) $directories);
	}

229
}