18 const REG_KEY_PRIORITY = 0;
19 const REG_KEY_INDEX = 1;
20 const REG_KEY_HANDLER = 2;
22 const OPTION_DEPRECATION_MESSAGE =
'deprecation_message';
23 const OPTION_DEPRECATION_VERSION =
'deprecation_version';
28 private $next_index = 0;
33 private $registrations = [];
38 private $backups = [];
55 if (empty(
$name) || empty(
$type) || !is_callable($callback,
true)) {
60 if (in_array($this->
getLogger()->getLevel(
false), [LogLevel::WARNING, LogLevel::NOTICE, LogLevel::INFO, LogLevel::DEBUG])) {
61 if (!$services->handlers->isCallable($callback)) {
62 $this->
getLogger()->warning(
'Handler: ' . $services->handlers->describeCallable($callback) .
' is not callable');
68 self::REG_KEY_INDEX => $this->next_index,
69 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) {
98 if (!$matcher->matches($registration[self::REG_KEY_HANDLER])) {
102 if ($registration[self::REG_KEY_HANDLER] != $callback) {
107 unset($this->registrations[
$name][$type][$i]);
143 foreach ($this->registrations as
$name => $types) {
144 foreach ($types as
$type => $registrations) {
145 foreach ($registrations as $registration) {
146 $priority = $registration[self::REG_KEY_PRIORITY];
147 $handler = $registration[self::REG_KEY_HANDLER];
167 return !empty($this->registrations[
$name][
$type]);
181 if (!empty($this->registrations[
$name][
$type])) {
182 if (
$name !==
'all' && $type !==
'all') {
183 array_splice($registrations,
count($registrations), 0, $this->registrations[
$name][$type]);
186 if (!empty($this->registrations[
'all'][$type])) {
187 if ($type !==
'all') {
188 array_splice($registrations,
count($registrations), 0, $this->registrations[
'all'][$type]);
191 if (!empty($this->registrations[
$name][
'all'])) {
192 if ($name !==
'all') {
193 array_splice($registrations,
count($registrations), 0, $this->registrations[$name][
'all']);
196 if (!empty($this->registrations[
'all'][
'all'])) {
197 array_splice($registrations,
count($registrations), 0, $this->registrations[
'all'][
'all']);
200 usort($registrations,
function ($a, $b) {
202 if ($a[self::REG_KEY_PRIORITY] < $b[self::REG_KEY_PRIORITY]) {
205 if ($a[self::REG_KEY_PRIORITY] > $b[self::REG_KEY_PRIORITY]) {
209 return ($a[self::REG_KEY_INDEX] < $b[self::REG_KEY_INDEX]) ? -1 : 1;
213 foreach ($registrations as $registration) {
214 $handlers[] = $registration[self::REG_KEY_HANDLER];
228 if (is_string($spec) &&
false !==
strpos($spec,
'::')) {
229 list (
$type, $method) = explode(
'::', $spec, 2);
233 if (!is_array($spec) || empty($spec[0]) || empty($spec[1]) || !is_string($spec[1])) {
237 if (is_object($spec[0])) {
238 $spec[0] = get_class($spec[0]);
241 if (!is_string($spec[0])) {
259 $this->backups[] = $this->registrations;
260 $this->registrations = [];
270 $backup = array_pop($this->backups);
271 if (is_array($backup)) {
272 $this->registrations = $backup;
287 self::OPTION_DEPRECATION_MESSAGE =>
'',
288 self::OPTION_DEPRECATION_VERSION =>
'',
292 if (!$handlers || !
$options[self::OPTION_DEPRECATION_MESSAGE]) {
297 $options[self::OPTION_DEPRECATION_MESSAGE],
298 $options[self::OPTION_DEPRECATION_VERSION]
clearHandlers($name, $type)
Clears all callback registrations for a plugin hook.
hasHandler($name, $type)
Is a handler registered for this specific name and type? "all" handlers are not considered.
if(!$user||!$user->canDelete()) $name
registerHandler($name, $type, $callback, $priority=500)
Register a callback as a plugin hook handler.
backup()
Temporarily remove all event/hook registrations (before tests)
trait Loggable
Enables adding a logger.
Base class for events and hooks.
getMatcher($spec)
Create a matcher for the given callable (if it's for a static or dynamic method)
checkDeprecation($name, $type, array $options=[])
Check if handlers are registered on a deprecated hook/event.
getLogger()
Returns logger.
Identify a static/dynamic method callable, even if contains an object to which you don't have a refer...
_elgg_services()
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 callback as a plugin hook of event handler.
logDeprecatedMessage(string $message, string $version)
Sends a message about deprecated use of a function, view, etc.
restore()
Restore backed up event/hook registrations (after tests)
getOrderedHandlers($name, $type)
Returns an ordered array of handlers registered for $name and $type.