Elgg  Version 1.11
HooksRegistrationService.php
Go to the documentation of this file.
1 <?php
2 namespace Elgg;
3 
15 abstract class HooksRegistrationService {
16 
17  private $handlers = array();
18 
22  protected $logger;
23 
30  public function setLogger(\Elgg\Logger $logger = null) {
31  $this->logger = $logger;
32  return $this;
33  }
34 
43  public function registerHandler($name, $type, $callback, $priority = 500) {
44  if (empty($name) || empty($type) || !is_callable($callback, true)) {
45  return false;
46  }
47 
48  if (!isset($this->handlers[$name])) {
49  $this->handlers[$name] = array();
50  }
51 
52  if (!isset($this->handlers[$name][$type])) {
53  $this->handlers[$name][$type] = array();
54  }
55 
56  // Priority cannot be lower than 0
57  $priority = max((int) $priority, 0);
58 
59  while (isset($this->handlers[$name][$type][$priority])) {
60  $priority++;
61  }
62 
63  $this->handlers[$name][$type][$priority] = $callback;
64  ksort($this->handlers[$name][$type]);
65 
66  return true;
67  }
68 
79  public function unregisterHandler($name, $type, $callback) {
80  if (isset($this->handlers[$name]) && isset($this->handlers[$name][$type])) {
81  foreach ($this->handlers[$name][$type] as $key => $name_callback) {
82  if ($name_callback == $callback) {
83  unset($this->handlers[$name][$type][$key]);
84  return true;
85  }
86  }
87  }
88 
89  return false;
90  }
91 
103  public function getAllHandlers() {
104  return $this->handlers;
105  }
106 
114  public function hasHandler($name, $type) {
115  return isset($this->handlers[$name][$type]);
116  }
117 
128  public function getOrderedHandlers($name, $type) {
129  $handlers = array();
130 
131  if (isset($this->handlers[$name][$type])) {
132  if ($name != 'all' && $type != 'all') {
133  $handlers = array_merge($handlers, array_values($this->handlers[$name][$type]));
134  }
135  }
136  if (isset($this->handlers['all'][$type])) {
137  if ($type != 'all') {
138  $handlers = array_merge($handlers, array_values($this->handlers['all'][$type]));
139  }
140  }
141  if (isset($this->handlers[$name]['all'])) {
142  if ($name != 'all') {
143  $handlers = array_merge($handlers, array_values($this->handlers[$name]['all']));
144  }
145  }
146  if (isset($this->handlers['all']['all'])) {
147  $handlers = array_merge($handlers, array_values($this->handlers['all']['all']));
148  }
149 
150  return $handlers;
151  }
152 }
hasHandler($name, $type)
Does the hook have a handler?
setLogger(\Elgg\Logger $logger=null)
Set a logger instance, e.g.
if($guid==elgg_get_logged_in_user_guid()) $name
Definition: delete.php:21
registerHandler($name, $type, $callback, $priority=500)
Registers a handler.
Save menu items.
$key
Definition: summary.php:34
$type
Definition: add.php:8
getAllHandlers()
Returns all registered handlers as array( $name => array( $type => array( $priority => callback...
unregisterHandler($name, $type, $callback)
Unregister a handler.
$priority
getOrderedHandlers($name, $type)
Returns an ordered array of handlers registered for $name and $type.