1: <?php namespace Illuminate\Support;
2:
3: class ClassLoader {
4:
5: /**
6: * The registered directories.
7: *
8: * @var array
9: */
10: protected static $directories = array();
11:
12: /**
13: * Indicates if a ClassLoader has been registered.
14: *
15: * @var bool
16: */
17: protected static $registered = false;
18:
19: /**
20: * Load the given class file.
21: *
22: * @param string $class
23: * @return void
24: */
25: public static function load($class)
26: {
27: $class = static::normalizeClass($class);
28:
29: foreach (static::$directories as $directory)
30: {
31: if (file_exists($path = $directory.DIRECTORY_SEPARATOR.$class))
32: {
33: require_once $path;
34:
35: return true;
36: }
37: }
38: }
39:
40: /**
41: * Get the normal file name for a class.
42: *
43: * @param string $class
44: * @return string
45: */
46: public static function normalizeClass($class)
47: {
48: if ($class[0] == '\\') $class = substr($class, 1);
49:
50: return str_replace(array('\\', '_'), DIRECTORY_SEPARATOR, $class).'.php';
51: }
52:
53: /**
54: * Register the given class loader on the auto-loader stack.
55: *
56: * @return void
57: */
58: public static function register()
59: {
60: if ( ! static::$registered)
61: {
62: spl_autoload_register(array('\Illuminate\Support\ClassLoader', 'load'));
63:
64: static::$registered = true;
65: }
66: }
67:
68: /**
69: * Add directories to the class loader.
70: *
71: * @param string|array $directories
72: * @return void
73: */
74: public static function addDirectories($directories)
75: {
76: static::$directories = array_merge(static::$directories, (array) $directories);
77:
78: static::$directories = array_unique(static::$directories);
79: }
80:
81: /**
82: * Remove directories from the class loader.
83: *
84: * @param string|array $directories
85: * @return void
86: */
87: public static function removeDirectories($directories = null)
88: {
89: if (is_null($directories))
90: {
91: static::$directories = array();
92: }
93: else
94: {
95: $directories = (array) $directories;
96:
97: static::$directories = array_filter(static::$directories, function($directory) use ($directories)
98: {
99: return ( ! in_array($directory, $directories));
100: });
101: }
102: }
103:
104: /**
105: * Gets all the directories registered with the loader.
106: *
107: * @return array
108: */
109: public static function getDirectories()
110: {
111: return static::$directories;
112: }
113:
114: }
115: