1 <?php
2
3 namespace Docolight\Support;
4
5 6 7 8 9
10 class ClassLoader
11 {
12 13 14 15 16
17 protected static $directories = array();
18
19 20 21 22 23
24 protected static $registered = false;
25
26 27 28 29 30 31 32
33 public static function load($class)
34 {
35 $class = static::normalizeClass($class);
36
37 foreach (static::$directories as $directory) {
38 if (file_exists($path = $directory.DIRECTORY_SEPARATOR.$class)) {
39 require_once $path;
40
41 return true;
42 }
43 }
44
45 return false;
46 }
47
48 49 50 51 52 53 54
55 public static function normalizeClass($class)
56 {
57 if ($class[0] == '\\') {
58 $class = substr($class, 1);
59 }
60
61 return str_replace(array('\\', '_'), DIRECTORY_SEPARATOR, $class).'.php';
62 }
63
64 65 66
67 public static function register()
68 {
69 if (!static::$registered) {
70 static::$registered = spl_autoload_register(array('\Docolight\Support\ClassLoader', 'load'));
71 }
72 }
73
74 75 76 77 78
79 public static function addDirectories($directories)
80 {
81 static::$directories = array_unique(array_merge(static::$directories, (array) $directories));
82 }
83
84 85 86 87 88
89 public static function removeDirectories($directories = null)
90 {
91 if (is_null($directories)) {
92 static::$directories = array();
93 } else {
94 static::$directories = array_diff(static::$directories, (array) $directories);
95 }
96 }
97
98 99 100 101 102
103 public static function getDirectories()
104 {
105 return static::$directories;
106 }
107 }
108