7 use Elgg\Traits\Loggable;
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';
29 const OPTION_CONTINUE_ON_EXCEPTION =
'continue_on_exception';
31 protected int $next_index = 0;
33 protected array $ordered_handlers_cache = [];
38 protected array $registrations = [];
40 protected array $backups = [];
67 self::OPTION_STOPPABLE => true,
71 if ($this->
hasTimer() && $type ===
'system' &&
$name !==
'shutdown') {
72 $options[self::OPTION_USE_TIMER] =
true;
73 $options[self::OPTION_TIMER_KEYS] = [
"[{$name},{$type}]"];
77 $handlers = $this->getOrderedHandlers(
$name,
$type);
95 list($success, $return, $event) = $this->callHandler(
$handler, $event, $event_args,
$options);
101 if (!empty(
$options[self::OPTION_STOPPABLE]) && ($return ===
false)) {
104 }
catch (\Throwable $t) {
105 if (!empty(
$options[self::OPTION_CONTINUE_ON_EXCEPTION])) {
106 $handler_string = $this->handlers->describeCallable(
$handler);
108 $this->
getLogger()->error(
"Callback '{$handler_string}' for the event '{$name}', '{$type}' caused an exception: {$t->getMessage()}");
143 list($success, $return, $event) = $this->callHandler(
$handler, $event, $event_args,
$options);
149 if ($return !==
null) {
153 }
catch (\Throwable $t) {
154 if (!empty(
$options[self::OPTION_CONTINUE_ON_EXCEPTION])) {
155 $handler_string = $this->handlers->describeCallable(
$handler);
157 $this->getLogger()->error(
"Callback '{$handler_string}' for the event '{$name}', '{$type}' caused an exception: {$t->getMessage()}");
210 $options[self::OPTION_STOPPABLE] = false;
231 $options[
'_elgg_sequence_id'] = uniqid(
"{$name}{$type}", true);
269 $unique_id = uniqid(
"{$name}{$type}results",
true);
270 $options[
'_elgg_sequence_id'] = $unique_id;
271 $params[
'_elgg_sequence_id'] = $unique_id;
308 $message =
"The '{$name}', '{$type}' event is deprecated. {$message}";
330 $message =
"The '{$name}', '{$type}' event is deprecated. {$message}";
350 if (empty(
$name) || empty(
$type) || !is_callable($callback, true)) {
354 if (in_array($this->getLogger()->getLevel(
false), [LogLevel::WARNING, LogLevel::NOTICE, LogLevel::INFO, LogLevel::DEBUG])) {
355 if (!$this->handlers->isCallable($callback)) {
356 $this->getLogger()->warning(
'Handler: ' . $this->handlers->describeCallable($callback) .
' is not callable');
360 $this->registrations[
$name][
$type][
"{$priority}_{$this->next_index}"] = [
362 self::REG_KEY_INDEX => $this->next_index,
363 self::REG_KEY_HANDLER => $callback,
367 unset($this->ordered_handlers_cache);
387 $matcher = $this->getMatcher($callback);
389 foreach ($this->registrations[
$name][
$type] as $i => $registration) {
391 if (!$matcher->matches($registration[self::REG_KEY_HANDLER])) {
394 }
elseif ($registration[self::REG_KEY_HANDLER] != $callback) {
398 unset($this->registrations[
$name][
$type][$i]);
399 unset($this->ordered_handlers_cache);
415 unset($this->ordered_handlers_cache);
434 foreach ($this->registrations as
$name => $types) {
435 foreach ($types as
$type => $registrations) {
436 foreach ($registrations as $registration) {
437 $priority = $registration[self::REG_KEY_PRIORITY];
471 if (isset($this->ordered_handlers_cache[
$name .
$type])) {
472 return $this->ordered_handlers_cache[
$name .
$type];
475 if (!empty($this->registrations[
$name][
$type])) {
477 $registrations = $this->registrations[
$name][
$type];
481 if (!empty($this->registrations[
'all'][
$type])) {
482 if (
$type !==
'all') {
483 $registrations += $this->registrations[
'all'][
$type];
487 if (!empty($this->registrations[
$name][
'all'])) {
488 if (
$name !==
'all') {
489 $registrations += $this->registrations[
$name][
'all'];
493 if (!empty($this->registrations[
'all'][
'all'])) {
494 $registrations += $this->registrations[
'all'][
'all'];
497 ksort($registrations, SORT_NATURAL);
500 foreach ($registrations as $registration) {
501 $handlers[] = $registration[self::REG_KEY_HANDLER];
504 $this->ordered_handlers_cache[
$name .
$type] = $handlers;
517 if (is_string($spec) && str_contains($spec,
'::')) {
518 list (
$type, $method) = explode(
'::', $spec, 2);
522 if (!is_array($spec) || empty($spec[0]) || empty($spec[1]) || !is_string($spec[1])) {
526 if (is_object($spec[0])) {
527 $spec[0] = get_class($spec[0]);
530 if (!is_string($spec[0])) {
534 return new MethodMatcher($spec[0], $spec[1]);
547 $this->backups[] = $this->registrations;
548 $this->registrations = [];
549 unset($this->ordered_handlers_cache);
558 $backup = array_pop($this->backups);
559 if (is_array($backup)) {
560 $this->registrations = $backup;
563 unset($this->ordered_handlers_cache);
600 if (is_callable($begin_callback)) {
601 call_user_func($begin_callback, [
602 'callable' => $callable,
603 'readable_callable' => $this->handlers->describeCallable($callable),
605 'arguments' =>
$args,
613 $timer_keys[] = $this->handlers->describeCallable($callable);
627 if (is_callable($end_callback)) {
628 call_user_func($end_callback, [
629 'callable' => $callable,
630 'readable_callable' => $this->handlers->describeCallable($callable),
632 'arguments' =>
$args,
getLogger()
Returns logger.
if(! $user||! $user->canDelete()) $name
$params
Saves global plugin settings.
return[ 'admin/delete_admin_notices'=>['access'=> 'admin'], 'admin/menu/save'=>['access'=> 'admin'], 'admin/plugins/activate'=>['access'=> 'admin'], 'admin/plugins/activate_all'=>['access'=> 'admin'], 'admin/plugins/deactivate'=>['access'=> 'admin'], 'admin/plugins/deactivate_all'=>['access'=> 'admin'], 'admin/plugins/set_priority'=>['access'=> 'admin'], 'admin/security/security_txt'=>['access'=> 'admin'], 'admin/security/settings'=>['access'=> 'admin'], 'admin/security/regenerate_site_secret'=>['access'=> 'admin'], 'admin/site/cache/invalidate'=>['access'=> 'admin'], 'admin/site/flush_cache'=>['access'=> 'admin'], 'admin/site/icons'=>['access'=> 'admin'], 'admin/site/set_maintenance_mode'=>['access'=> 'admin'], 'admin/site/set_robots'=>['access'=> 'admin'], 'admin/site/theme'=>['access'=> 'admin'], 'admin/site/unlock_upgrade'=>['access'=> 'admin'], 'admin/site/settings'=>['access'=> 'admin'], 'admin/upgrade'=>['access'=> 'admin'], 'admin/upgrade/reset'=>['access'=> 'admin'], 'admin/user/ban'=>['access'=> 'admin'], 'admin/user/bulk/ban'=>['access'=> 'admin'], 'admin/user/bulk/delete'=>['access'=> 'admin'], 'admin/user/bulk/unban'=>['access'=> 'admin'], 'admin/user/bulk/validate'=>['access'=> 'admin'], 'admin/user/change_email'=>['access'=> 'admin'], 'admin/user/delete'=>['access'=> 'admin'], 'admin/user/login_as'=>['access'=> 'admin'], 'admin/user/logout_as'=>[], 'admin/user/makeadmin'=>['access'=> 'admin'], 'admin/user/resetpassword'=>['access'=> 'admin'], 'admin/user/removeadmin'=>['access'=> 'admin'], 'admin/user/unban'=>['access'=> 'admin'], 'admin/user/validate'=>['access'=> 'admin'], 'annotation/delete'=>[], 'avatar/upload'=>[], 'comment/save'=>[], 'diagnostics/download'=>['access'=> 'admin'], 'entity/chooserestoredestination'=>[], 'entity/delete'=>[], 'entity/mute'=>[], 'entity/restore'=>[], 'entity/subscribe'=>[], 'entity/trash'=>[], 'entity/unmute'=>[], 'entity/unsubscribe'=>[], 'login'=>['access'=> 'logged_out'], 'logout'=>[], 'notifications/mute'=>['access'=> 'public'], 'plugins/settings/remove'=>['access'=> 'admin'], 'plugins/settings/save'=>['access'=> 'admin'], 'plugins/usersettings/save'=>[], 'register'=>['access'=> 'logged_out', 'middleware'=>[\Elgg\Router\Middleware\RegistrationAllowedGatekeeper::class,],], 'river/delete'=>[], 'settings/notifications'=>[], 'settings/notifications/subscriptions'=>[], 'user/changepassword'=>['access'=> 'public'], 'user/requestnewpassword'=>['access'=> 'public'], 'useradd'=>['access'=> 'admin'], 'usersettings/save'=>[], 'widgets/add'=>[], 'widgets/delete'=>[], 'widgets/move'=>[], 'widgets/save'=>[],]
Identify a static/dynamic method callable, even if contains an object to which you don't have a refer...
unregisterHandler(string $name, string $type, $callback)
Unregister a callback as an event handler.
callHandler($callable, $event, array $args, array $options=[])
__construct(protected HandlersService $handlers)
Constructor.
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...
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...
getAllHandlers()
Returns all registered handlers as array( $name => array( $type => array( $priority => array( callbac...
triggerSequence(string $name, string $type, $object=null, ?callable $callable=null, array $options=[])
Trigger a sequence of <event>:before, <event>, and <event>:after handlers.
getMatcher($spec)
Create a matcher for the given callable (if it's for a static or dynamic method)
triggerResults(string $name, string $type, array $params=[], $value=null, array $options=[])
Trigger an event allowed to return a mixed result.
registerHandler(string $name, string $type, $callback, int $priority=500)
Register a callback as a event handler.
trigger(string $name, string $type, $object=null, array $options=[])
Trigger an Elgg event.
restore()
Restore backed up event registrations (after tests)
checkDeprecation(string $name, string $type, string $message, string $version)
Check if handlers are registered on a deprecated event.
triggerAfter(string $name, string $type, $object=null, array $options=[])
Trigger an "After event" indicating a process has finished.
getOrderedHandlers(string $name, string $type)
Returns an ordered array of handlers registered for $name and $type.
clearHandlers(string $name, string $type)
Clears all callback registrations for an event.
backup()
Temporarily remove all event registrations (before tests)
hasHandler(string $name, string $type)
Is a handler registered for this specific name and type? "all" handlers are not considered.
triggerResultsSequence(string $name, string $type, array $params=[], $value=null, ?callable $callable=null, array $options=[])
Trigger a sequence of <event>:before, <event>, and <event>:after handlers.
triggerBefore(string $name, string $type, $object=null, array $options=[])
Trigger a "Before event" indicating a process is about to begin.
Helpers for providing callable-based APIs.
if($who_can_change_language==='nobody') elseif($who_can_change_language==='admin_only' &&!elgg_is_admin_logged_in()) $options
if($email instanceof \Elgg\Email) $object
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.
$args
Some servers don't allow PHP to check the rewrite, so try via AJAX.
endTimer(array $keys)
Ends the timer (when enabled)
trait Profilable
Make an object accept a timer.
hasTimer()
Has a timer been set.
beginTimer(array $keys)
Start the timer (when enabled)
if(parse_url(elgg_get_site_url(), PHP_URL_PATH) !=='/') if(file_exists(elgg_get_root_path() . 'robots.txt'))
Set robots.txt.