Elgg  Version 2.2
 All Classes Namespaces Files Functions Variables Pages
EventsService.php
Go to the documentation of this file.
1 <?php
2 namespace Elgg;
4 
15  use Profilable;
16 
17  const OPTION_STOPPABLE = 'stoppable';
18  const OPTION_DEPRECATION_MESSAGE = 'deprecation_message';
19  const OPTION_DEPRECATION_VERSION = 'deprecation_version';
20 
24  private $inspector;
25 
31  public function __construct(Inspector $inspector = null) {
32  if (!$inspector) {
33  $inspector = new Inspector();
34  }
35  $this->inspector = $inspector;
36  }
37 
41  public function registerHandler($name, $type, $callback, $priority = 500) {
42  if (in_array($type, ['member', 'friend', 'member_of_site', 'attached'])
43  && in_array($name, ['create', 'update', 'delete'])) {
44  $this->logger->error("'$name, $type' event is no longer triggered. Update your event registration "
45  . "to use '$name, relationship'");
46  }
47  return parent::registerHandler($name, $type, $callback, $priority);
48  }
49 
57  public function trigger($event, $type, $object = null, array $options = array()) {
58  $options = array_merge(array(
59  self::OPTION_STOPPABLE => true,
60  self::OPTION_DEPRECATION_MESSAGE => '',
61  self::OPTION_DEPRECATION_VERSION => '',
62  ), $options);
63 
64  $events = $this->hasHandler($event, $type);
65  if ($events && $options[self::OPTION_DEPRECATION_MESSAGE]) {
67  $options[self::OPTION_DEPRECATION_MESSAGE],
68  $options[self::OPTION_DEPRECATION_VERSION],
69  2
70  );
71  }
72 
73  $events = $this->getOrderedHandlers($event, $type);
74  $args = array($event, $type, $object);
75 
76  foreach ($events as $callback) {
77  if (!is_callable($callback)) {
78  if ($this->logger) {
79  $this->logger->warn("handler for event [$event, $type] is not callable: "
80  . $this->inspector->describeCallable($callback));
81  }
82  continue;
83  }
84 
85  if ($this->timer && $type === 'system' && $event !== 'shutdown') {
86  $callback_as_string = $this->inspector->describeCallable($callback) . "()";
87 
88  $this->timer->begin(["[$event,$type]", $callback_as_string]);
89  $return = call_user_func_array($callback, $args);
90  $this->timer->end(["[$event,$type]", $callback_as_string]);
91  } else {
92  $return = call_user_func_array($callback, $args);
93  }
94 
95  if (!empty($options[self::OPTION_STOPPABLE]) && ($return === false)) {
96  return false;
97  }
98  }
99 
100  return true;
101  }
102 
121  function triggerBefore($event, $object_type, $object = null) {
122  return $this->trigger("$event:before", $object_type, $object);
123  }
124 
141  public function triggerAfter($event, $object_type, $object = null) {
142  $options = array(
143  self::OPTION_STOPPABLE => false,
144  );
145  return $this->trigger("$event:after", $object_type, $object, $options);
146  }
147 
161  function triggerDeprecated($event, $object_type, $object = null, $message, $version) {
162  $options = array(
163  self::OPTION_DEPRECATION_MESSAGE => $message,
164  self::OPTION_DEPRECATION_VERSION => $version,
165  );
166  return $this->trigger($event, $object_type, $object, $options);
167  }
168 }
$object
These two snippets demonstrates triggering an event and how to register for that event.
Definition: trigger.php:7
trigger($event, $type, $object=null, array $options=array())
Triggers an Elgg event.
triggerBefore($event, $object_type, $object=null)
Trigger a "Before event" indicating a process is about to begin.
if($guid==elgg_get_logged_in_user_guid()) $name
Definition: delete.php:21
registerHandler($name, $type, $callback, $priority=500)
{}
triggerAfter($event, $object_type, $object=null)
Trigger an "After event" indicating a process has finished.
triggerDeprecated($event, $object_type, $object=null, $message, $version)
Trigger an event normally, but send a notice about deprecated use if any handlers are registered...
$return
Definition: opendd.php:15
$args
Some servers don't allow PHP to check the rewrite, so try via AJAX.
trait Profilable
Make an object accept a timer.
Definition: Profilable.php:9
$options
Elgg admin footer.
Definition: footer.php:6
__construct(Inspector $inspector=null)
Constructor.
elgg_deprecated_notice($msg, $dep_version, $backtrace_level=1)
Log a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:1070
$version
Definition: version.php:14
$priority
if(!$display_name) $type
Definition: delete.php:27