1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PSR-0 autoloader |
4
|
|
|
* |
5
|
|
|
* PHP version 5.5 |
6
|
|
|
* |
7
|
|
|
* @category OpCacheGUI |
8
|
|
|
* @package Core |
9
|
|
|
* @author Pieter Hordijk <[email protected]> |
10
|
|
|
* @copyright Copyright (c) 2013 Pieter Hordijk <https://github.com/PeeHaa> |
11
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
12
|
|
|
* @version 1.0.0 |
13
|
|
|
*/ |
14
|
|
|
namespace OpCacheGUI\Core; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* PSR-0 autoloader |
18
|
|
|
* |
19
|
|
|
* @category OpCacheGUI |
20
|
|
|
* @package Core |
21
|
|
|
* @author Pieter Hordijk <[email protected]> |
22
|
|
|
*/ |
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) |
42
|
|
|
{ |
43
|
9 |
|
$this->namespace = ltrim($namespace, '\\'); |
44
|
9 |
|
$this->path = rtrim($path, '/\\') . DIRECTORY_SEPARATOR; |
45
|
9 |
|
} |
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() |
79
|
|
|
{ |
80
|
8 |
|
return spl_autoload_register([$this, 'load']); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Unregister the autoloader to PHP |
85
|
|
|
* |
86
|
|
|
* @return boolean The status of the unregistration |
87
|
|
|
*/ |
88
|
1 |
|
public function unregister() |
89
|
|
|
{ |
90
|
1 |
|
return spl_autoload_unregister([$this, 'load']); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|