17 const REG_KEY_PRIORITY = 0;
18 const REG_KEY_INDEX = 1;
19 const REG_KEY_HANDLER = 2;
24 private $next_index = 0;
29 private $registrations = [];
34 private $backups = [];
48 $this->logger = $logger;
61 if (empty(
$name) || empty(
$type) || !is_callable($callback,
true)) {
67 self::REG_KEY_INDEX => $this->next_index,
68 self::REG_KEY_HANDLER => $callback,
90 if (empty($this->registrations[
$name][
$type])) {
94 $matcher = $this->getMatcher($callback);
96 foreach ($this->registrations[
$name][$type] as $i => $registration) {
99 if (!$matcher->matches($registration[self::REG_KEY_HANDLER])) {
103 if ($registration[self::REG_KEY_HANDLER] != $callback) {
108 unset($this->registrations[
$name][$type][$i]);
144 foreach ($this->registrations as
$name => $types) {
145 foreach ($types as
$type => $registrations) {
146 foreach ($registrations as $registration) {
147 $priority = $registration[self::REG_KEY_PRIORITY];
148 $handler = $registration[self::REG_KEY_HANDLER];
168 return !empty($this->registrations[
$name][
$type]);
184 if (!empty($this->registrations[
$name][
$type])) {
185 if (
$name !==
'all' && $type !==
'all') {
186 array_splice($registrations, count($registrations), 0, $this->registrations[
$name][$type]);
189 if (!empty($this->registrations[
'all'][$type])) {
190 if ($type !==
'all') {
191 array_splice($registrations, count($registrations), 0, $this->registrations[
'all'][$type]);
194 if (!empty($this->registrations[
$name][
'all'])) {
195 if ($name !==
'all') {
196 array_splice($registrations, count($registrations), 0, $this->registrations[$name][
'all']);
199 if (!empty($this->registrations[
'all'][
'all'])) {
200 array_splice($registrations, count($registrations), 0, $this->registrations[
'all'][
'all']);
203 usort($registrations,
function ($a, $b) {
205 if ($a[self::REG_KEY_PRIORITY] < $b[self::REG_KEY_PRIORITY]) {
208 if ($a[self::REG_KEY_PRIORITY] > $b[self::REG_KEY_PRIORITY]) {
212 return ($a[self::REG_KEY_INDEX] < $b[self::REG_KEY_INDEX]) ? -1 : 1;
216 foreach ($registrations as $registration) {
217 $handlers[] = $registration[self::REG_KEY_HANDLER];
231 if (is_string($spec) &&
false !== strpos($spec,
'::')) {
236 if (!is_array($spec) || empty($spec[0]) || empty($spec[1]) || !is_string($spec[1])) {
240 if (is_object($spec[0])) {
241 $spec[0] = get_class($spec[0]);
244 if (!is_string($spec[0])) {
264 $this->backups[] = $this->registrations;
265 $this->registrations = [];
277 $backup = array_pop($this->backups);
278 if (is_array($backup)) {
279 $this->registrations = $backup;
clearHandlers($name, $type)
Clears all handlers for a specific hook.
hasHandler($name, $type)
Is a handler registered for this specific name and type? "all" handlers are not considered.
setLogger(\Elgg\Logger $logger=null)
Set a logger instance, e.g.
if($guid==elgg_get_logged_in_user_guid()) $name
registerHandler($name, $type, $callback, $priority=500)
Registers a handler.
backup()
Temporarily remove all event/hook registrations (before tests)
Identify a static/dynamic method callable, even if contains an object to which you don't have a refer...
getMatcher($spec)
Create a matcher for the given callable (if it's for a static or dynamic method)
_elgg_services(\Elgg\Di\ServiceProvider $services=null)
Get the global service provider.
getAllHandlers()
Returns all registered handlers as array( $name => array( $type => array( $priority => array( callbac...
unregisterHandler($name, $type, $callback)
Unregister a handler.
restore()
Restore backed up event/hook registrations (after tests)
getOrderedHandlers($name, $type)
Returns an ordered array of handlers registered for $name and $type.