Elgg  Version 3.0
EventsService.php
Go to the documentation of this file.
1 <?php
2 namespace Elgg;
3 
10  use Profilable;
11 
12  const OPTION_STOPPABLE = 'stoppable';
13 
17  private $handlers;
18 
24  public function __construct(HandlersService $handlers) {
25  $this->handlers = $handlers;
26  }
27 
34  public function getHandlersService() {
35  return $this->handlers;
36  }
37 
41  public function registerHandler($name, $type, $callback, $priority = 500) {
42  if (in_array($type, ['member', 'friend', 'attached'])
43  && in_array($name, ['create', 'update', 'delete'])) {
44  _elgg_services()->logger->error("'$name, $type' event is no longer triggered. "
45  . "Update your event registration to use '$name, relationship'");
46  }
47 
48  return parent::registerHandler($name, $type, $callback, $priority);
49  }
50 
65  public function trigger($name, $type, $object = null, array $options = []) {
66  $options = array_merge([
67  self::OPTION_STOPPABLE => true,
68  ], $options);
69 
70  // check for deprecation
71  $this->checkDeprecation($name, $type, $options);
72 
73  // get registered handlers
74  $handlers = $this->getOrderedHandlers($name, $type);
75 
76  // This starts as a string, but if a handler type-hints an object we convert it on-demand inside
77  // \Elgg\HandlersService::call and keep it alive during all handler calls. We do this because
78  // creating objects for every triggering is expensive.
79  $event = 'event';
80  /* @var Event|string */
81 
82  foreach ($handlers as $handler) {
83  $handler_description = false;
84  if ($this->timer && $type === 'system' && $name !== 'shutdown') {
85  $handler_description = $this->handlers->describeCallable($handler) . "()";
86  $this->timer->begin(["[$name,$type]", $handler_description]);
87  }
88 
89  list($success, $return, $event) = $this->handlers->call($handler, $event, [$name, $type, $object]);
90 
91  if ($handler_description) {
92  $this->timer->end(["[$name,$type]", $handler_description]);
93  }
94 
95  if (!$success) {
96  continue;
97  }
98 
99  if (!empty($options[self::OPTION_STOPPABLE]) && ($return === false)) {
100  return false;
101  }
102  }
103 
104  return true;
105  }
106 
125  public function triggerBefore($event, $object_type, $object = null) {
126  return $this->trigger("$event:before", $object_type, $object);
127  }
128 
146  public function triggerAfter($event, $object_type, $object = null) {
147  $options = [
148  self::OPTION_STOPPABLE => false,
149  ];
150  return $this->trigger("$event:after", $object_type, $object, $options);
151  }
152 
165  public function triggerSequence($event, $object_type, $object = null, callable $callable = null) {
166  if (!$this->triggerBefore($event, $object_type, $object)) {
167  return false;
168  }
169 
170  $result = $this->trigger($event, $object_type, $object);
171  if (!$result) {
172  return false;
173  }
174 
175  if ($callable) {
176  $result = call_user_func($callable, $object);
177  }
178 
179  $this->triggerAfter($event, $object_type, $object);
180 
181  return $result;
182  }
183 
198  public function triggerDeprecated($event, $object_type, $object = null, $message = null, $version = null) {
199  $options = [
200  self::OPTION_DEPRECATION_MESSAGE => $message,
201  self::OPTION_DEPRECATION_VERSION => $version,
202  ];
203  return $this->trigger($event, $object_type, $object, $options);
204  }
205 }
triggerBefore($event, $object_type, $object=null)
Trigger a "Before event" indicating a process is about to begin.
if(!$item instanceof ElggRiverItem) $object
Definition: responses.php:23
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
Helpers for providing callable-based APIs.
registerHandler($name, $type, $callback, $priority=500)
{}
triggerAfter($event, $object_type, $object=null)
Trigger an "After event" indicating a process has finished.
Events service.
triggerDeprecated($event, $object_type, $object=null, $message=null, $version=null)
Trigger an event normally, but send a notice about deprecated use if any handlers are registered...
$type
Definition: delete.php:21
__construct(HandlersService $handlers)
Constructor.
trait Profilable
Make an object accept a timer.
Definition: Profilable.php:10
$options
Elgg admin footer.
Definition: footer.php:6
trigger($name, $type, $object=null, array $options=[])
Triggers an Elgg event.
Configuration exception.
Base class for events and hooks.
getHandlersService()
Get the handlers service in use.
triggerSequence($event, $object_type, $object=null, callable $callable=null)
Trigger an sequence of <event>:before, <event>, and <event>:after handlers.
_elgg_services()
Get the global service provider.
Definition: elgglib.php:1292
$handler
Definition: add.php:7
$version
Definition: version.php:14
$priority