1 | <?php |
||
23 | class Autoloader |
||
24 | { |
||
25 | /** |
||
26 | * @var string The namespace prefix for this instance. |
||
27 | */ |
||
28 | protected $namespace = ''; |
||
29 | |||
30 | /** |
||
31 | * @var string The filesystem prefix to use for this instance |
||
32 | */ |
||
33 | protected $path = ''; |
||
34 | |||
35 | /** |
||
36 | * Build the instance of the autoloader |
||
37 | * |
||
38 | * @param string $namespace The prefixed namespace this instance will load |
||
39 | * @param string $path The filesystem path to the root of the namespace |
||
40 | */ |
||
41 | 9 | public function __construct($namespace, $path) |
|
46 | |||
47 | /** |
||
48 | * Try to load a class |
||
49 | * |
||
50 | * @param string $class The class name to load |
||
51 | * |
||
52 | * @return boolean If the loading was successful |
||
53 | */ |
||
54 | 9 | public function load($class) |
|
55 | { |
||
56 | 9 | $class = ltrim($class, '\\'); |
|
57 | 9 | if (strpos($class, $this->namespace) === 0) { |
|
58 | 8 | $nsparts = explode('\\', $class); |
|
59 | 8 | $class = array_pop($nsparts); |
|
60 | 8 | $nsparts[] = ''; |
|
61 | 8 | $path = $this->path . implode(DIRECTORY_SEPARATOR, $nsparts); |
|
62 | 8 | $path .= str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php'; |
|
63 | 8 | if (file_exists($path)) { |
|
64 | 8 | require $path; |
|
65 | |||
66 | 8 | return true; |
|
67 | } |
||
68 | } |
||
69 | |||
70 | 2 | return false; |
|
71 | } |
||
72 | |||
73 | /** |
||
74 | * Register the autoloader to PHP |
||
75 | * |
||
76 | * @return boolean The status of the registration |
||
77 | */ |
||
78 | 8 | public function register() |
|
82 | |||
83 | /** |
||
84 | * Unregister the autoloader to PHP |
||
85 | * |
||
86 | * @return boolean The status of the unregistration |
||
87 | */ |
||
88 | 1 | public function unregister() |
|
92 | } |
||
93 |