Elgg  Version 4.x
EventsService.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg;
4 
6 
13 
14  use Profilable;
15 
16  const OPTION_STOPPABLE = 'stoppable';
17 
21  private $handlers;
22 
28  public function __construct(HandlersService $handlers) {
29  $this->handlers = $handlers;
30  }
31 
38  public function getHandlersService() {
39  return $this->handlers;
40  }
41 
45  public function registerHandler($name, $type, $callback, $priority = 500) {
46  if (in_array($type, ['member', 'friend', 'attached'])
47  && in_array($name, ['create', 'update', 'delete'])) {
48  $this->getLogger()->error("'{$name}, {$type}' event is no longer triggered. "
49  . "Update your event registration to use '{$name}, relationship'");
50  }
51 
52  return parent::registerHandler($name, $type, $callback, $priority);
53  }
54 
69  public function trigger($name, $type, $object = null, array $options = []) {
70  $options = array_merge([
71  self::OPTION_STOPPABLE => true,
72  ], $options);
73 
74  // check for deprecation
75  $this->checkDeprecation($name, $type, $options);
76 
77  // get registered handlers
78  $handlers = $this->getOrderedHandlers($name, $type);
79 
80  // This starts as a string, but if a handler type-hints an object we convert it on-demand inside
81  // \Elgg\HandlersService::call and keep it alive during all handler calls. We do this because
82  // creating objects for every triggering is expensive.
83  $event = 'event';
84  /* @var Event|string */
85 
86  foreach ($handlers as $handler) {
87  $handler_description = false;
88  if ($this->hasTimer() && $type === 'system' && $name !== 'shutdown') {
89  $handler_description = $this->handlers->describeCallable($handler) . '()';
90  $this->beginTimer(["[{$name},{$type}]", $handler_description]);
91  }
92 
93  list($success, $return, $event) = $this->handlers->call($handler, $event, [$name, $type, $object]);
94 
95  if ($handler_description) {
96  $this->endTimer(["[{$name},{$type}]", $handler_description]);
97  }
98 
99  if (!$success) {
100  continue;
101  }
102 
103  if (!empty($options[self::OPTION_STOPPABLE]) && ($return === false)) {
104  return false;
105  }
106  }
107 
108  return true;
109  }
110 
130  public function triggerBefore($event, $object_type, $object = null, array $options = []) {
131  return $this->trigger("$event:before", $object_type, $object, $options);
132  }
133 
152  public function triggerAfter($event, $object_type, $object = null, array $options = []) {
153  $options[self::OPTION_STOPPABLE] = false;
154 
155  return $this->trigger("$event:after", $object_type, $object, $options);
156  }
157 
172  public function triggerSequence($event, $object_type, $object = null, callable $callable = null, array $options = []) {
173  if (!$this->triggerBefore($event, $object_type, $object, $options)) {
174  return false;
175  }
176 
177  $result = $this->trigger($event, $object_type, $object, $options);
178  if (!$result) {
179  return false;
180  }
181 
182  if ($callable) {
183  $result = call_user_func($callable, $object);
184  }
185 
186  $this->triggerAfter($event, $object_type, $object, $options);
187 
188  return $result;
189  }
190 
205  public function triggerDeprecated($event, $object_type, $object = null, $message = null, $version = null) {
206  $options = [
207  self::OPTION_DEPRECATION_MESSAGE => $message,
208  self::OPTION_DEPRECATION_VERSION => $version,
209  ];
210  return $this->trigger($event, $object_type, $object, $options);
211  }
212 
227  public function triggerDeprecatedSequence($event, $object_type, $object = null, callable $callable = null, string $message = null, string $version = null) {
228  $options = [
229  self::OPTION_DEPRECATION_MESSAGE => $message,
230  self::OPTION_DEPRECATION_VERSION => $version,
231  ];
232  return $this->triggerSequence($event, $object_type, $object, $callable, $options);
233  }
234 }
trait Profilable
Make an object accept a timer.
Definition: Profilable.php:12
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
triggerDeprecatedSequence($event, $object_type, $object=null, callable $callable=null, string $message=null, string $version=null)
Trigger an event normally, but send a notice about deprecated use if any handlers are registered...
Helpers for providing callable-based APIs.
registerHandler($name, $type, $callback, $priority=500)
{}
$version
Events service.
triggerDeprecated($event, $object_type, $object=null, $message=null, $version=null)
Trigger an event sequence normally, but send a notice about deprecated use if any handlers are regist...
hasTimer()
Has a timer been set.
Definition: Profilable.php:34
$type
Definition: delete.php:21
__construct(HandlersService $handlers)
Constructor.
$options
Elgg admin footer.
Definition: footer.php:6
trigger($name, $type, $object=null, array $options=[])
Triggers an Elgg event.
triggerSequence($event, $object_type, $object=null, callable $callable=null, array $options=[])
Trigger an sequence of <event>:before, <event>, and <event>:after handlers.
Base class for events and hooks.
getHandlersService()
Get the handlers service in use.
getLogger()
Returns logger.
Definition: Loggable.php:37
if($email instanceof\Elgg\Email) $object
Definition: body.php:16
beginTimer(array $keys)
Start the timer (when enabled)
Definition: Profilable.php:46
triggerBefore($event, $object_type, $object=null, array $options=[])
Trigger a "Before event" indicating a process is about to begin.
$handler
Definition: add.php:7
$priority
endTimer(array $keys)
Ends the timer (when enabled)
Definition: Profilable.php:62
triggerAfter($event, $object_type, $object=null, array $options=[])
Trigger an "After event" indicating a process has finished.