123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- <?php
- namespace Elgg;
- abstract class HooksRegistrationService {
- private $handlers = array();
-
- protected $logger;
-
- public function setLogger(\Elgg\Logger $logger = null) {
- $this->logger = $logger;
- return $this;
- }
-
-
- public function registerHandler($name, $type, $callback, $priority = 500) {
- if (empty($name) || empty($type) || !is_callable($callback, true)) {
- return false;
- }
-
- if (!isset($this->handlers[$name])) {
- $this->handlers[$name] = array();
- }
-
- if (!isset($this->handlers[$name][$type])) {
- $this->handlers[$name][$type] = array();
- }
-
-
- $priority = max((int) $priority, 0);
-
- while (isset($this->handlers[$name][$type][$priority])) {
- $priority++;
- }
-
- $this->handlers[$name][$type][$priority] = $callback;
- ksort($this->handlers[$name][$type]);
- return true;
- }
-
-
- public function unregisterHandler($name, $type, $callback) {
- if (isset($this->handlers[$name]) && isset($this->handlers[$name][$type])) {
- $matcher = $this->getMatcher($callback);
- foreach ($this->handlers[$name][$type] as $key => $handler) {
- if ($matcher) {
- if (!$matcher->matches($handler)) {
- continue;
- }
- } else {
- if ($handler != $callback) {
- continue;
- }
- }
- unset($this->handlers[$name][$type][$key]);
- return true;
- }
- }
- return false;
- }
-
- public function getAllHandlers() {
- return $this->handlers;
- }
-
- public function hasHandler($name, $type) {
- return isset($this->handlers[$name][$type]);
- }
-
- public function getOrderedHandlers($name, $type) {
- $handlers = array();
-
- if (isset($this->handlers[$name][$type])) {
- if ($name != 'all' && $type != 'all') {
- $handlers = array_merge($handlers, array_values($this->handlers[$name][$type]));
- }
- }
- if (isset($this->handlers['all'][$type])) {
- if ($type != 'all') {
- $handlers = array_merge($handlers, array_values($this->handlers['all'][$type]));
- }
- }
- if (isset($this->handlers[$name]['all'])) {
- if ($name != 'all') {
- $handlers = array_merge($handlers, array_values($this->handlers[$name]['all']));
- }
- }
- if (isset($this->handlers['all']['all'])) {
- $handlers = array_merge($handlers, array_values($this->handlers['all']['all']));
- }
- return $handlers;
- }
-
- protected function getMatcher($spec) {
- if (is_string($spec) && false !== strpos($spec, '::')) {
- list ($type, $method) = explode('::', $spec, 2);
- return new MethodMatcher($type, $method);
- }
- if (!is_array($spec) || empty($spec[0]) || empty($spec[1]) || !is_string($spec[1])) {
- return null;
- }
- if (is_object($spec[0])) {
- $spec[0] = get_class($spec[0]);
- }
- if (!is_string($spec[0])) {
- return null;
- }
- return new MethodMatcher($spec[0], $spec[1]);
- }
- }
|