1 | <?php |
||
25 | class Autoloader |
||
26 | { |
||
27 | /** |
||
28 | * @var string Constant for namespace separator |
||
29 | */ |
||
30 | const NAMESPACE_SEPARATOR = '\\'; |
||
31 | |||
32 | /** |
||
33 | * An array of paths that will be searched when attempting to load a class |
||
34 | * @var array |
||
35 | */ |
||
36 | protected $paths; |
||
37 | |||
38 | /** |
||
39 | * Autoloader Constructor |
||
40 | */ |
||
41 | 9 | public function __construct($paths = array()) |
|
42 | { |
||
43 | 9 | $this->paths = explode(PATH_SEPARATOR, get_include_path()); |
|
44 | 9 | foreach ($paths as $path) { |
|
45 | 2 | $this->addPath($path); |
|
46 | 9 | } |
|
47 | 9 | } |
|
48 | |||
49 | /** |
||
50 | * Add path to list of search paths |
||
51 | * |
||
52 | * Attempts to deduce the absolute (real) path from the path specified by the |
||
53 | * $path argument. If successful, the absolute path is added to the search |
||
54 | * path list and the method returns true. If one can't be found, it adds $path |
||
55 | * to the search path list, as-is and returns false |
||
56 | * |
||
57 | * @param string A path to add to the list of search paths |
||
58 | * @return boolean |
||
59 | */ |
||
60 | 5 | public function addPath($path) |
|
61 | { |
||
62 | 5 | $paths = $this->getPaths(); |
|
63 | 5 | if ($rp = realpath($path)) { |
|
64 | 4 | if (in_array($rp, $paths)) return true; |
|
65 | 4 | $this->paths []= $rp; |
|
66 | 4 | return true; |
|
67 | } |
||
68 | 2 | $this->paths []= $path; |
|
69 | 2 | return false; |
|
70 | } |
||
71 | |||
72 | /** |
||
73 | * Retrieve search path list (array) |
||
74 | * |
||
75 | * Simply returns the array containing all the paths that will be searched |
||
76 | * when attempting to load a class. |
||
77 | * |
||
78 | * @return array An array of paths to search for classes |
||
79 | */ |
||
80 | 7 | public function getPaths() |
|
84 | |||
85 | /** |
||
86 | * Register the autoloader |
||
87 | * |
||
88 | * Registers this library's autoload function with the SPL-provided autoload |
||
89 | * queue. This allows for CSVelte's autoloader to work its magic without |
||
90 | * having to worry about interfering with any other autoloader. |
||
91 | * |
||
92 | * Also adds all of this class's search paths to PHP's include path. |
||
93 | * |
||
94 | * @return boolean Whatever the return value of spl_autoload_register is |
||
95 | * @see spl_autoload_register |
||
96 | */ |
||
97 | 2 | public function register() |
|
102 | |||
103 | /** |
||
104 | * Load a class |
||
105 | * |
||
106 | * This is the function (or method in this case) used to autoload all of |
||
107 | * CSVelte's classes. It need not be called directly, but rather regestered |
||
108 | * with the SPL's autoload queue using this class's register method. |
||
109 | * |
||
110 | * @param string The fully qualified class name to load |
||
111 | * @return boolean |
||
112 | * @see \CSVelte\Autoloader::register() |
||
113 | */ |
||
114 | 4 | public function load($className) |
|
129 | } |
||
130 |