Elgg  Version 3.0
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 
33  public function getEvents() {
34  _elgg_services()->deprecation->sendNotice(__METHOD__ . " has been deprecated, please use elgg()->events", '3.0');
35 
36  return $this->events;
37  }
38 
52  public function trigger($name, $type, $params = null, $value = null, array $options = []) {
53 
54  // check for deprecation
55  $this->checkDeprecation($name, $type, $options);
56 
57  // This starts as a string, but if a handler type-hints an object we convert it on-demand inside
58  // \Elgg\HandlersService::call and keep it alive during all handler calls. We do this because
59  // creating objects for every triggering is expensive.
60  $hook = 'hook';
61  /* @var Hook|string $hook */
62 
63  $handlers = $this->events->getHandlersService();
64 
65  foreach ($this->getOrderedHandlers($name, $type) as $handler) {
66  $exit_warning = null;
67 
68  if (in_array($name, ['forward', 'action', 'route'])) {
69  // assume the handler is going to exit the request...
70  $exit_warning = function () use ($name, $type, $handler, $handlers) {
71  _elgg_services()->deprecation->sendNotice(
72  "'$name', '$type' plugin hook should not be used to serve a response. Instead return an "
73  . "appropriate ResponseBuilder instance from an action or page handler. Do not terminate "
74  . "code execution with exit() or die() in {$handlers->describeCallable($handler)}",
75  '2.3'
76  );
77  };
78  $this->events->registerHandler('shutdown', 'system', $exit_warning);
79  }
80 
81  list($success, $return, $hook) = $handlers->call($handler, $hook, [$name, $type, $value, $params]);
82 
83  if ($exit_warning) {
84  // an exit did not occur, so no need for the warning...
85  $this->events->unregisterHandler('shutdown', 'system', $exit_warning);
86  }
87 
88  if (!$success) {
89  continue;
90  }
91  if ($return !== null) {
92  $value = $return;
93  if ($hook instanceof HrsHook) {
94  $hook->setValue($return);
95  }
96  }
97  }
98 
99  return $value;
100  }
101 
117  public function triggerDeprecated($name, $type, $params = null, $value = null, $message = null, $version = null) {
118  $options = [
119  self::OPTION_DEPRECATION_MESSAGE => $message,
120  self::OPTION_DEPRECATION_VERSION => $version,
121  ];
122 
123  return $this->trigger($name, $type, $params, $value, $options);
124  }
125 
129  public function registerHandler($name, $type, $callback, $priority = 500) {
130  if (($name == 'view' || $name == 'view_vars') && $type !== 'all') {
131  $type = ViewsService::canonicalizeViewName($type);
132  }
133 
134  return parent::registerHandler($name, $type, $callback, $priority);
135  }
136 
140  public function unregisterHandler($name, $type, $callback) {
141  if (($name == 'view' || $name == 'view_vars') && $type != 'all') {
142  $type = ViewsService::canonicalizeViewName($type);
143  }
144 
145  return parent::unregisterHandler($name, $type, $callback);
146  }
147 }
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
Events service.
unregisterHandler($name, $type, $callback)
$type
Definition: delete.php:21
$options
Elgg admin footer.
Definition: footer.php:6
getEvents()
Get the events API.
Configuration exception.
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.
$value
Definition: debugging.php:7
_elgg_services()
Get the global service provider.
Definition: elgglib.php:1292
$handler
Definition: add.php:7
__construct(EventsService $events)
Constructor.
$version
Definition: version.php:14
$priority