20 const REG_KEY_PRIORITY = 0;
21 const REG_KEY_INDEX = 1;
22 const REG_KEY_HANDLER = 2;
24 const OPTION_STOPPABLE =
'stoppable';
25 const OPTION_USE_TIMER =
'use_timer';
26 const OPTION_TIMER_KEYS =
'timer_keys';
27 const OPTION_BEGIN_CALLBACK =
'begin_callback';
28 const OPTION_END_CALLBACK =
'end_callback';
38 protected $next_index = 0;
43 protected $registrations = [];
48 protected $backups = [];
56 $this->handlers = $handlers;
75 self::OPTION_STOPPABLE =>
true,
79 if ($this->
hasTimer() && $type ===
'system' && $name !==
'shutdown') {
80 $options[self::OPTION_USE_TIMER] =
true;
81 $options[self::OPTION_TIMER_KEYS] = [
"[{$name},{$type}]"];
85 $handlers = $this->getOrderedHandlers($name, $type);
102 list($success, $return, $event) = $this->callHandler($handler, $event, $event_args,
$options);
108 if (!empty(
$options[self::OPTION_STOPPABLE]) && ($return ===
false)) {
135 foreach ($this->getOrderedHandlers($name, $type) as
$handler) {
138 list($success, $return, $event) = $this->callHandler($handler, $event, $event_args,
$options);
144 if ($return !== null) {
146 $event->setValue($value);
195 $options[self::OPTION_STOPPABLE] =
false;
216 $options[
'_elgg_sequence_id'] = uniqid(
"{$name}{$type}",
true);
252 $unique_id = uniqid(
"{$name}{$type}results",
true);
253 $options[
'_elgg_sequence_id'] = $unique_id;
254 $params[
'_elgg_sequence_id'] = $unique_id;
289 $message =
"The '{$name}', '{$type}' event is deprecated. {$message}";
311 $message =
"The '{$name}', '{$type}' event is deprecated. {$message}";
314 return $this->triggerResults($name, $type,
$params, $returnvalue,
$options);
331 if (empty($name) || empty($type) || !is_callable($callback,
true)) {
335 if (($name ==
'view' || $name ==
'view_vars') && $type !==
'all') {
336 $type = ViewsService::canonicalizeViewName($type);
340 if (in_array($this->
getLogger()->getLevel(
false), [LogLevel::WARNING, LogLevel::NOTICE, LogLevel::INFO, LogLevel::DEBUG])) {
341 if (!$services->handlers->isCallable($callback)) {
342 $this->
getLogger()->warning(
'Handler: ' . $services->handlers->describeCallable($callback) .
' is not callable');
348 self::REG_KEY_INDEX => $this->next_index,
349 self::REG_KEY_HANDLER => $callback,
367 if (($name ===
'view' || $name ===
'view_vars') && $type !==
'all') {
368 $type = ViewsService::canonicalizeViewName($type);
371 if (empty($this->registrations[$name][$type])) {
375 $matcher = $this->getMatcher($callback);
377 foreach ($this->registrations[$name][$type] as $i => $registration) {
379 if (!$matcher->matches($registration[self::REG_KEY_HANDLER])) {
382 }
elseif ($registration[self::REG_KEY_HANDLER] != $callback) {
386 unset($this->registrations[$name][$type][$i]);
400 unset($this->registrations[$name][$type]);
419 foreach ($this->registrations as
$name => $types) {
420 foreach ($types as
$type => $registrations) {
421 foreach ($registrations as $registration) {
422 $priority = $registration[self::REG_KEY_PRIORITY];
442 return !empty($this->registrations[$name][$type]);
456 if (!empty($this->registrations[$name][$type])) {
457 if ($name !==
'all' && $type !==
'all') {
458 array_splice($registrations,
count($registrations), 0, $this->registrations[$name][$type]);
462 if (!empty($this->registrations[
'all'][$type])) {
463 if ($type !==
'all') {
464 array_splice($registrations,
count($registrations), 0, $this->registrations[
'all'][$type]);
468 if (!empty($this->registrations[$name][
'all'])) {
469 if ($name !==
'all') {
470 array_splice($registrations,
count($registrations), 0, $this->registrations[$name][
'all']);
474 if (!empty($this->registrations[
'all'][
'all'])) {
475 array_splice($registrations,
count($registrations), 0, $this->registrations[
'all'][
'all']);
478 usort($registrations,
function ($a, $b) {
480 if ($a[self::REG_KEY_PRIORITY] < $b[self::REG_KEY_PRIORITY]) {
484 if ($a[self::REG_KEY_PRIORITY] > $b[self::REG_KEY_PRIORITY]) {
489 return ($a[self::REG_KEY_INDEX] < $b[self::REG_KEY_INDEX]) ? -1 : 1;
493 foreach ($registrations as $registration) {
494 $handlers[] = $registration[self::REG_KEY_HANDLER];
508 if (is_string($spec) && str_contains($spec,
'::')) {
509 list (
$type, $method) = explode(
'::', $spec, 2);
513 if (!is_array($spec) || empty($spec[0]) || empty($spec[1]) || !is_string($spec[1])) {
517 if (is_object($spec[0])) {
518 $spec[0] = get_class($spec[0]);
521 if (!is_string($spec[0])) {
538 $this->backups[] = $this->registrations;
539 $this->registrations = [];
548 $backup = array_pop($this->backups);
549 if (is_array($backup)) {
550 $this->registrations = $backup;
565 $message =
trim($message);
566 if (empty($message)) {
570 if (!$this->hasHandler($name, $type)) {
588 if (is_callable($begin_callback)) {
589 call_user_func($begin_callback, [
590 'callable' => $callable,
591 'readable_callable' => $this->handlers->describeCallable($callable),
593 'arguments' =>
$args,
601 $timer_keys[] = $this->handlers->describeCallable($callable);
606 $results = $this->handlers->call($callable, $event, $args);
615 if (is_callable($end_callback)) {
616 call_user_func($end_callback, [
617 'callable' => $callable,
618 'readable_callable' => $this->handlers->describeCallable($callable),
620 'arguments' =>
$args,
trait Profilable
Make an object accept a timer.
$params
Saves global plugin settings.
registerHandler(string $name, string $type, $callback, int $priority=500)
Register a callback as a event handler.
Helpers for providing callable-based APIs.
hasHandler(string $name, string $type)
Is a handler registered for this specific name and type? "all" handlers are not considered.
if(!$user||!$user->canDelete()) $name
triggerAfter(string $name, string $type, $object=null, array $options=[])
Trigger an "After event" indicating a process has finished.
c Accompany it with the information you received as to the offer to distribute corresponding source complete source code means all the source code for all modules it plus any associated interface definition plus the scripts used to control compilation and installation of the executable as a special the source code distributed need not include anything that is normally and so on of the operating system on which the executable unless that component itself accompanies the executable If distribution of executable or object code is made by offering access to copy from a designated then offering equivalent access to copy the source code from the same place counts as distribution of the source even though third parties are not compelled to copy the source along with the object code You may not or distribute the Program except as expressly provided under this License Any attempt otherwise to sublicense or distribute the Program is void
getAllHandlers()
Returns all registered handlers as array( $name => array( $type => array( $priority => array( callbac...
$args
Some servers don't allow PHP to check the rewrite, so try via AJAX.
hasTimer()
Has a timer been set.
__construct(HandlersService $handlers)
Constructor.
triggerBefore(string $name, string $type, $object=null, array $options=[])
Trigger a "Before event" indicating a process is about to begin.
if($item instanceof\ElggEntity) elseif($item instanceof\ElggRiverItem) elseif($item instanceof\ElggRelationship) elseif(is_callable([$item, 'getType']))
elgg_extract($key, $array, $default=null, bool $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
trait Loggable
Enables adding a logger.
triggerDeprecated(string $name, string $type, $object=null, string $message= '', string $version= '', array $options=[])
Trigger an event sequence normally, but send a notice about deprecated use if any handlers are regist...
triggerDeprecatedResults(string $name, string $type, array $params=[], $returnvalue=null, string $message= '', string $version= '', array $options=[])
Trigger an event sequence normally, but send a notice about deprecated use if any handlers are regist...
trigger(string $name, string $type, $object=null, array $options=[])
Triggers an Elgg event.
triggerResults(string $name, string $type, array $params=[], $value=null, array $options=[])
Triggers a event that is allowed to return a mixed result.
triggerResultsSequence(string $name, string $type, array $params=[], $value=null, callable $callable=null, array $options=[])
Trigger an sequence of <event>:before, <event>, and <event>:after handlers.
getOrderedHandlers(string $name, string $type)
Returns an ordered array of handlers registered for $name and $type.
restore()
Restore backed up event registrations (after tests)
triggerSequence(string $name, string $type, $object=null, callable $callable=null, array $options=[])
Trigger a sequence of <event>:before, <event>, and <event>:after handlers.
getLogger()
Returns logger.
if($email instanceof\Elgg\Email) $object
Identify a static/dynamic method callable, even if contains an object to which you don't have a refer...
beginTimer(array $keys)
Start the timer (when enabled)
backup()
Temporarily remove all event registrations (before tests)
callHandler($callable, $event, array $args, array $options=[])
checkDeprecation(string $name, string $type, string $message, string $version)
Check if handlers are registered on a deprecated event.
_elgg_services()
Get the global service provider.
unregisterHandler(string $name, string $type, $callback)
Unregister a callback as an event handler.
getMatcher($spec)
Create a matcher for the given callable (if it's for a static or dynamic method)
clearHandlers(string $name, string $type)
Clears all callback registrations for an event.
endTimer(array $keys)
Ends the timer (when enabled)
logDeprecatedMessage(string $message, string $version)
Sends a message about deprecated use of a function, view, etc.