Elgg  Version 1.9
HooksRegistrationService.php
Go to the documentation of this file.
1 <?php
2 
15 
16  private $handlers = array();
17 
21  protected $logger;
22 
29  public function setLogger(Elgg_Logger $logger = null) {
30  $this->logger = $logger;
31  return $this;
32  }
33 
42  public function registerHandler($name, $type, $callback, $priority = 500) {
43  if (empty($name) || empty($type) || !is_callable($callback, true)) {
44  return false;
45  }
46 
47  if (!isset($this->handlers[$name])) {
48  $this->handlers[$name] = array();
49  }
50 
51  if (!isset($this->handlers[$name][$type])) {
52  $this->handlers[$name][$type] = array();
53  }
54 
55  // Priority cannot be lower than 0
56  $priority = max((int) $priority, 0);
57 
58  while (isset($this->handlers[$name][$type][$priority])) {
59  $priority++;
60  }
61 
62  $this->handlers[$name][$type][$priority] = $callback;
63  ksort($this->handlers[$name][$type]);
64 
65  return true;
66  }
67 
78  public function unregisterHandler($name, $type, $callback) {
79  if (isset($this->handlers[$name]) && isset($this->handlers[$name][$type])) {
80  foreach ($this->handlers[$name][$type] as $key => $name_callback) {
81  if ($name_callback == $callback) {
82  unset($this->handlers[$name][$type][$key]);
83  return true;
84  }
85  }
86  }
87 
88  return false;
89  }
90 
102  public function getAllHandlers() {
103  return $this->handlers;
104  }
105 
113  public function hasHandler($name, $type) {
114  return isset($this->handlers[$name][$type]);
115  }
116 
125  protected function getOrderedHandlers($name, $type) {
126  $handlers = array();
127 
128  if (isset($this->handlers[$name][$type])) {
129  if ($name != 'all' && $type != 'all') {
130  $handlers = array_merge($handlers, array_values($this->handlers[$name][$type]));
131  }
132  }
133  if (isset($this->handlers['all'][$type])) {
134  if ($type != 'all') {
135  $handlers = array_merge($handlers, array_values($this->handlers['all'][$type]));
136  }
137  }
138  if (isset($this->handlers[$name]['all'])) {
139  if ($name != 'all') {
140  $handlers = array_merge($handlers, array_values($this->handlers[$name]['all']));
141  }
142  }
143  if (isset($this->handlers['all']['all'])) {
144  $handlers = array_merge($handlers, array_values($this->handlers['all']['all']));
145  }
146 
147  return $handlers;
148  }
149 
158  protected function describeCallable($callable) {
159  if (is_string($callable)) {
160  return $callable;
161  }
162  if (is_array($callable) && array_keys($callable) === array(0, 1) && is_string($callable[1])) {
163  if (is_string($callable[0])) {
164  return "{$callable[0]}::{$callable[1]}";
165  }
166  return "(" . get_class($callable[0]) . ")->{$callable[1]}";
167  }
168  if ($callable instanceof Closure) {
169  $ref = new ReflectionFunction($callable);
170  $file = $ref->getFileName();
171  $line = $ref->getStartLine();
172  return "(Closure {$file}:{$line})";
173  }
174  if (is_object($callable)) {
175  return "(" . get_class($callable) . ")->__invoke()";
176  }
177  return "(unknown)";
178  }
179 }
unregisterHandler($name, $type, $callback)
Unregister a handler.
describeCallable($callable)
Get a string description of a callback.
if($guid==elgg_get_logged_in_user_guid()) $name
Definition: delete.php:21
registerHandler($name, $type, $callback, $priority=500)
Registers a handler.
setLogger(Elgg_Logger $logger=null)
Set a logger instance, e.g.
$key
Definition: summary.php:34
$type
Definition: add.php:8
hasHandler($name, $type)
Does the hook have a handler?
getAllHandlers()
Returns all registered handlers as array( $name => array( $type => array( $priority => callback...
$priority
getOrderedHandlers($name, $type)
Returns an ordered array of handlers registered for $name and $type.