Elgg  Version 1.12
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  $matcher = $this->getMatcher($callback);
82 
83  foreach ($this->handlers[$name][$type] as $key => $handler) {
84  if ($matcher) {
85  if (!$matcher->matches($handler)) {
86  continue;
87  }
88  } else {
89  if ($handler != $callback) {
90  continue;
91  }
92  }
93 
94  unset($this->handlers[$name][$type][$key]);
95  return true;
96  }
97  }
98 
99  return false;
100  }
101 
113  public function getAllHandlers() {
114  return $this->handlers;
115  }
116 
124  public function hasHandler($name, $type) {
125  return isset($this->handlers[$name][$type]);
126  }
127 
138  public function getOrderedHandlers($name, $type) {
139  $handlers = array();
140 
141  if (isset($this->handlers[$name][$type])) {
142  if ($name != 'all' && $type != 'all') {
143  $handlers = array_merge($handlers, array_values($this->handlers[$name][$type]));
144  }
145  }
146  if (isset($this->handlers['all'][$type])) {
147  if ($type != 'all') {
148  $handlers = array_merge($handlers, array_values($this->handlers['all'][$type]));
149  }
150  }
151  if (isset($this->handlers[$name]['all'])) {
152  if ($name != 'all') {
153  $handlers = array_merge($handlers, array_values($this->handlers[$name]['all']));
154  }
155  }
156  if (isset($this->handlers['all']['all'])) {
157  $handlers = array_merge($handlers, array_values($this->handlers['all']['all']));
158  }
159 
160  return $handlers;
161  }
162 
170  protected function getMatcher($spec) {
171  if (is_string($spec) && false !== strpos($spec, '::')) {
172  list ($type, $method) = explode('::', $spec, 2);
173  return new MethodMatcher($type, $method);
174  }
175 
176  if (!is_array($spec) || empty($spec[0]) || empty($spec[1]) || !is_string($spec[1])) {
177  return null;
178  }
179 
180  if (is_object($spec[0])) {
181  $spec[0] = get_class($spec[0]);
182  }
183 
184  if (!is_string($spec[0])) {
185  return null;
186  }
187 
188  return new MethodMatcher($spec[0], $spec[1]);
189  }
190 }
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
$method
Definition: form.php:25
registerHandler($name, $type, $callback, $priority=500)
Registers a handler.
Save menu items.
$key
Definition: summary.php:34
Identify a static/dynamic method callable, even if contains an object to which you don&#39;t have a refer...
getMatcher($spec)
Create a matcher for the given callable (if it&#39;s for a static or dynamic method)
$type
Definition: add.php:8
getAllHandlers()
Returns all registered handlers as array( $name => array( $type => array( $priority => callback...
$handler
Definition: add.php:10
unregisterHandler($name, $type, $callback)
Unregister a handler.
$priority
getOrderedHandlers($name, $type)
Returns an ordered array of handlers registered for $name and $type.