Elgg  Version 2.3
EventsService.php
Go to the documentation of this file.
1 <?php
2 namespace Elgg;
4 
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 
48  if ($name === 'pagesetup' && $type === 'system') {
49  static $ignore = [
50  'users_pagesetup' => true,
51  'profile_pagesetup' => true,
52  '_elgg_friends_page_setup' => true,
53  '_elgg_setup_collections_menu' => true,
54  '_elgg_user_settings_menu_setup' => true,
55  'developers_setup_menu' => true,
56  'groups_setup_sidebar_menus' => true,
57  'notifications_plugin_pagesetup' => true,
58  '_elgg_admin_pagesetup' => true,
59  'aalborg_theme_pagesetup' => true,
60  'login_as_add_topbar_link' => true,
61  ];
62  if (!is_string($callback) || !isset($ignore[$callback])) {
63  $msg = "Event [pagesetup, system] is deprecated. Use menu or page shell hooks.";
64  elgg_deprecated_notice($msg, '2.3', 2);
65  }
66  }
67 
68  return parent::registerHandler($name, $type, $callback, $priority);
69  }
70 
78  public function trigger($event, $type, $object = null, array $options = array()) {
79  $options = array_merge(array(
80  self::OPTION_STOPPABLE => true,
81  self::OPTION_DEPRECATION_MESSAGE => '',
82  self::OPTION_DEPRECATION_VERSION => '',
83  ), $options);
84 
85  $events = $this->hasHandler($event, $type);
86  if ($events && $options[self::OPTION_DEPRECATION_MESSAGE]) {
88  $options[self::OPTION_DEPRECATION_MESSAGE],
89  $options[self::OPTION_DEPRECATION_VERSION],
90  2
91  );
92  }
93 
94  $events = $this->getOrderedHandlers($event, $type);
95  $args = array($event, $type, $object);
96 
97  foreach ($events as $callback) {
98  if (!is_callable($callback)) {
99  if ($this->logger) {
100  $this->logger->warn("handler for event [$event, $type] is not callable: "
101  . $this->inspector->describeCallable($callback));
102  }
103  continue;
104  }
105 
106  if ($this->timer && $type === 'system' && $event !== 'shutdown') {
107  $callback_as_string = $this->inspector->describeCallable($callback) . "()";
108 
109  $this->timer->begin(["[$event,$type]", $callback_as_string]);
110  $return = call_user_func_array($callback, $args);
111  $this->timer->end(["[$event,$type]", $callback_as_string]);
112  } else {
113  $return = call_user_func_array($callback, $args);
114  }
115 
116  if (!empty($options[self::OPTION_STOPPABLE]) && ($return === false)) {
117  return false;
118  }
119  }
120 
121  return true;
122  }
123 
142  function triggerBefore($event, $object_type, $object = null) {
143  return $this->trigger("$event:before", $object_type, $object);
144  }
145 
162  public function triggerAfter($event, $object_type, $object = null) {
163  $options = array(
164  self::OPTION_STOPPABLE => false,
165  );
166  return $this->trigger("$event:after", $object_type, $object, $options);
167  }
168 
182  function triggerDeprecated($event, $object_type, $object = null, $message, $version) {
183  $options = array(
184  self::OPTION_DEPRECATION_MESSAGE => $message,
185  self::OPTION_DEPRECATION_VERSION => $version,
186  );
187  return $this->trigger($event, $object_type, $object, $options);
188  }
189 }
$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.
$version
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&#39;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.
Save menu items.
elgg_deprecated_notice($msg, $dep_version, $backtrace_level=1)
Log a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:1098
http free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:5
$priority
if(!$display_name) $type
Definition: delete.php:27