1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* this file is part of magerun |
4
|
|
|
* |
5
|
|
|
* @author Tom Klingenberg <https://github.com/ktomk> |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace N98\Util; |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Autloader with self-registration, de-registration, muting and implementation switching |
13
|
|
|
* |
14
|
|
|
* @package N98\Util |
15
|
|
|
*/ |
16
|
|
|
final class AutoloadHandler |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Throw exception if the autoload implementation is not callable (default). If no exception is thrown, |
20
|
|
|
* autoload callback is just ignored |
21
|
|
|
*/ |
22
|
|
|
const NO_EXCEPTION = 1; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* |
26
|
|
|
*/ |
27
|
|
|
const NO_AUTO_REGISTER = 2; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var integer |
31
|
|
|
*/ |
32
|
|
|
private $flags; |
33
|
|
|
|
34
|
|
|
private $callback; |
35
|
|
|
|
36
|
|
|
private $splRegistered; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var bool |
40
|
|
|
*/ |
41
|
|
|
private $enabled; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param $callback |
45
|
|
|
* @param integer $flags [optional] |
46
|
|
|
* @return AutoloadHandler |
47
|
|
|
*/ |
48
|
|
|
public static function create($callback, $flags = null) |
49
|
|
|
{ |
50
|
|
|
return new self($callback, $flags); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* AutoloadHandler constructor. |
55
|
|
|
* |
56
|
|
|
* @param $callback |
57
|
|
|
* @param integer $flags [optional] |
58
|
|
|
*/ |
59
|
|
|
public function __construct($callback, $flags = null) |
60
|
|
|
{ |
61
|
|
|
if (null === $flags) { |
62
|
|
|
$flags = 0; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$this->flags = $flags; |
66
|
|
|
$this->enabled = true; |
67
|
|
|
$this->callback = $callback; |
68
|
|
|
$this->flags & self::NO_AUTO_REGISTER || $this->register(); |
69
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function register() |
73
|
|
|
{ |
74
|
|
|
spl_autoload_register($this); |
75
|
|
|
$this->splRegistered = true; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function unregister() |
79
|
|
|
{ |
80
|
|
|
spl_autoload_unregister($this); |
81
|
|
|
$this->splRegistered = false; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function __invoke($className) |
85
|
|
|
{ |
86
|
|
|
if (!$this->splRegistered) { |
87
|
|
|
return false; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if (!$this->enabled) { |
91
|
|
|
return false; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if (!is_callable($this->callback)) { |
95
|
|
|
if ($this->flags & self::NO_EXCEPTION) { |
96
|
|
|
return false; |
97
|
|
|
} |
98
|
|
|
throw new \BadMethodCallException('Autoload callback is not callable'); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return call_user_func($this->callback, $className); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function getCleanupCallback() |
105
|
|
|
{ |
106
|
|
|
$self = (object) array('ref' => $this); |
107
|
|
|
|
108
|
|
|
return function () use ($self) { |
109
|
|
|
if (isset($self->ref)) { |
110
|
|
|
$self->ref->reset(); |
111
|
|
|
unset($self->ref); |
112
|
|
|
} |
113
|
|
|
}; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Unregister from SPL Stack and destroy callback reference. |
118
|
|
|
*/ |
119
|
|
|
public function reset() |
120
|
|
|
{ |
121
|
|
|
$this->unregister(); |
122
|
|
|
$this->callback = null; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return boolean |
127
|
|
|
*/ |
128
|
|
|
public function isEnabled() |
129
|
|
|
{ |
130
|
|
|
return $this->enabled; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param boolean $flagEnabled |
135
|
|
|
*/ |
136
|
|
|
public function setEnabled($flagEnabled) |
137
|
|
|
{ |
138
|
|
|
$this->enabled = (bool) $flagEnabled; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function disable() |
142
|
|
|
{ |
143
|
|
|
$this->enabled = false; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function enable() |
147
|
|
|
{ |
148
|
|
|
$this->enabled = true; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param mixed $callback |
153
|
|
|
*/ |
154
|
|
|
public function setCallback($callback) |
155
|
|
|
{ |
156
|
|
|
$this->callback = $callback; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|