loader.php 2.7 KB
Newer Older
Taylor Otwell committed
1 2 3 4 5 6 7 8 9
<?php namespace System;

class Loader {

	/**
	 * The paths to be searched by the loader.
	 *
	 * @var array
	 */
10
	public static $paths = array(BASE_PATH, MODEL_PATH, LIBRARY_PATH);
Taylor Otwell committed
11 12 13 14 15 16

	/**
	 * All of the class aliases.
	 *
	 * @var array
	 */
17
	public static $aliases = array();
Taylor Otwell committed
18 19

	/**
Taylor Otwell committed
20 21 22 23
	 * All of the active modules.
	 *
	 * @var array
	 */
24
	public static $modules = array();
Taylor Otwell committed
25 26

	/**
Taylor Otwell committed
27 28 29 30 31 32
	 * Bootstrap the auto-loader.
	 *
	 * @return void
	 */
	public static function bootstrap()
	{
Taylor Otwell committed
33 34
		static::$aliases = Config::get('aliases');
		static::$modules = Config::get('application.modules');
Taylor Otwell committed
35 36 37 38 39 40
	}

	/**
	 * Load a class file for a given class name.
	 *
	 * This function is registered on the SPL auto-loader stack by the front controller during each request.
Taylor Otwell committed
41
	 * All Laravel class names follow a namespace to directory convention.
Taylor Otwell committed
42 43 44 45 46 47 48 49
	 *
	 * @param  string  $class
	 * @return void
	 */
	public static function load($class)
	{
		$file = strtolower(str_replace('\\', '/', $class));

50
		if (array_key_exists($class, static::$aliases)) return class_alias(static::$aliases[$class], $class);
Taylor Otwell committed
51

52
		(static::load_from_registered($file)) or static::load_from_module($file);
Taylor Otwell committed
53 54 55 56 57 58 59 60 61 62
	}

	/**
	 * Load a class that is stored in the registered directories.
	 *
	 * @param  string  $file
	 * @return bool
	 */
	private static function load_from_registered($file)
	{
Taylor Otwell committed
63 64 65 66 67 68
		foreach (static::$paths as $directory)
		{
			if (file_exists($path = $directory.$file.EXT))
			{
				require $path;

Taylor Otwell committed
69 70 71
				return true;
			}
		}
72 73

		return false;
Taylor Otwell committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
	}

	/**
	 * Load a class that is stored in a module.
	 *
	 * @param  string  $file
	 * @return void
	 */
	private static function load_from_module($file)
	{
		// Since all module models and libraries must be namespaced to the
		// module name, we'll extract the module name from the file.
		$module = substr($file, 0, strpos($file, '/'));

		if (in_array($module, static::$modules))
		{
			$module = MODULE_PATH.$module.'/';

			// Slice the module name off of the filename. Even though module libraries
			// and models are namespaced under the module, there will obviously not be
			// a folder matching that namespace in the libraries or models directories
			// of the module. Slicing it off will allow us to make a clean search for
			// the relevant class file.
			$file = substr($file, strpos($file, '/') + 1);

			foreach (array($module.'models', $module.'libraries') as $directory)
			{
101
				if (file_exists($path = $directory.'/'.$file.EXT)) return require $path;
Taylor Otwell committed
102 103 104 105 106
			}
		}
	}

	/**
107 108
	 * Register a path with the auto-loader. After registering the path, it will be
	 * checked similarly to the models and libraries directories.
Taylor Otwell committed
109 110 111 112 113 114
	 *
	 * @param  string  $path
	 * @return void
	 */
	public static function register($path)
	{
115
		static::$paths[] = rtrim($path, '/').'/';
Taylor Otwell committed
116 117 118
	}

}