Elgg  Version 4.x
PluginHooksService.php
Go to the documentation of this file.
1 <?php
2 namespace Elgg;
3 
5 
12 
16  private $events;
17 
23  public function __construct(EventsService $events) {
24  $this->events = $events;
25  }
26 
40  public function trigger($name, $type, $params = null, $value = null, array $options = []) {
41 
42  // check for deprecation
43  $this->checkDeprecation($name, $type, $options);
44 
45  // This starts as a string, but if a handler type-hints an object we convert it on-demand inside
46  // \Elgg\HandlersService::call and keep it alive during all handler calls. We do this because
47  // creating objects for every triggering is expensive.
48  $hook = 'hook';
49  /* @var Hook|string $hook */
50 
51  $handlers = $this->events->getHandlersService();
52 
53  foreach ($this->getOrderedHandlers($name, $type) as $handler) {
54  $exit_warning = null;
55 
56  if (in_array($name, ['forward', 'action', 'route'])) {
57  // assume the handler is going to exit the request...
58  $exit_warning = function () use ($name, $type, $handler, $handlers) {
59  $this->logDeprecatedMessage(
60  "'{$name}', '{$type}' plugin hook should not be used to serve a response. Instead return an "
61  . "appropriate ResponseBuilder instance from an action or page handler. Do not terminate "
62  . "code execution with exit() or die() in {$handlers->describeCallable($handler)}",
63  '2.3'
64  );
65  };
66  $this->events->registerHandler('shutdown', 'system', $exit_warning);
67  }
68 
69  list($success, $return, $hook) = $handlers->call($handler, $hook, [$name, $type, $value, $params]);
70 
71  if ($exit_warning) {
72  // an exit did not occur, so no need for the warning...
73  $this->events->unregisterHandler('shutdown', 'system', $exit_warning);
74  }
75 
76  if (!$success) {
77  continue;
78  }
79  if ($return !== null) {
80  $value = $return;
81  if ($hook instanceof HrsHook) {
82  $hook->setValue($return);
83  }
84  }
85  }
86 
87  return $value;
88  }
89 
105  public function triggerDeprecated($name, $type, $params = null, $value = null, $message = null, $version = null) {
106  $options = [
107  self::OPTION_DEPRECATION_MESSAGE => $message,
108  self::OPTION_DEPRECATION_VERSION => $version,
109  ];
110 
111  return $this->trigger($name, $type, $params, $value, $options);
112  }
113 
117  public function registerHandler($name, $type, $callback, $priority = 500) {
118  if (($name == 'view' || $name == 'view_vars') && $type !== 'all') {
119  $type = ViewsService::canonicalizeViewName($type);
120  }
121 
122  return parent::registerHandler($name, $type, $callback, $priority);
123  }
124 
128  public function unregisterHandler($name, $type, $callback) {
129  if (($name == 'view' || $name == 'view_vars') && $type != 'all') {
130  $type = ViewsService::canonicalizeViewName($type);
131  }
132 
133  return parent::unregisterHandler($name, $type, $callback);
134  }
135 }
registerHandler($name, $type, $callback, $priority=500)
{}
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
triggerDeprecated($name, $type, $params=null, $value=null, $message=null, $version=null)
Trigger an plugin hook normally, but send a notice about deprecated use if any handlers are registere...
$params
Saves global plugin settings.
Definition: save.php:13
$version
Events service.
unregisterHandler($name, $type, $callback)
$type
Definition: delete.php:21
$options
Elgg admin footer.
Definition: footer.php:6
$value
Definition: generic.php:51
Base class for events and hooks.
The object passed to invokable class name handlers.
Definition: Hook.php:12
trigger($name, $type, $params=null, $value=null, array $options=[])
Triggers a plugin hook.
$handler
Definition: add.php:7
__construct(EventsService $events)
Constructor.
$priority
logDeprecatedMessage(string $message, string $version)
Sends a message about deprecated use of a function, view, etc.
Definition: Loggable.php:80