HooksRegistrationService.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. namespace Elgg;
  3. /**
  4. * WARNING: API IN FLUX. DO NOT USE DIRECTLY.
  5. *
  6. * Use the elgg_* versions instead.
  7. *
  8. * @access private
  9. *
  10. * @package Elgg.Core
  11. * @subpackage Hooks
  12. * @since 1.9.0
  13. */
  14. abstract class HooksRegistrationService {
  15. private $handlers = array();
  16. /**
  17. * @var \Elgg\Logger
  18. */
  19. protected $logger;
  20. /**
  21. * Set a logger instance, e.g. for reporting uncallable handlers
  22. *
  23. * @param \Elgg\Logger $logger The logger
  24. * @return self
  25. */
  26. public function setLogger(\Elgg\Logger $logger = null) {
  27. $this->logger = $logger;
  28. return $this;
  29. }
  30. /**
  31. * Registers a handler.
  32. *
  33. * @warning This doesn't check if a callback is valid to be called, only if it is in the
  34. * correct format as a callable.
  35. *
  36. * @access private
  37. */
  38. public function registerHandler($name, $type, $callback, $priority = 500) {
  39. if (empty($name) || empty($type) || !is_callable($callback, true)) {
  40. return false;
  41. }
  42. if (!isset($this->handlers[$name])) {
  43. $this->handlers[$name] = array();
  44. }
  45. if (!isset($this->handlers[$name][$type])) {
  46. $this->handlers[$name][$type] = array();
  47. }
  48. // Priority cannot be lower than 0
  49. $priority = max((int) $priority, 0);
  50. while (isset($this->handlers[$name][$type][$priority])) {
  51. $priority++;
  52. }
  53. $this->handlers[$name][$type][$priority] = $callback;
  54. ksort($this->handlers[$name][$type]);
  55. return true;
  56. }
  57. /**
  58. * Unregister a handler
  59. *
  60. * @param string $name
  61. * @param string $type
  62. * @param callable $callback
  63. *
  64. * @return bool
  65. * @access private
  66. */
  67. public function unregisterHandler($name, $type, $callback) {
  68. if (isset($this->handlers[$name]) && isset($this->handlers[$name][$type])) {
  69. $matcher = $this->getMatcher($callback);
  70. foreach ($this->handlers[$name][$type] as $key => $handler) {
  71. if ($matcher) {
  72. if (!$matcher->matches($handler)) {
  73. continue;
  74. }
  75. } else {
  76. if ($handler != $callback) {
  77. continue;
  78. }
  79. }
  80. unset($this->handlers[$name][$type][$key]);
  81. return true;
  82. }
  83. }
  84. return false;
  85. }
  86. /**
  87. * Returns all registered handlers as array(
  88. * $name => array(
  89. * $type => array(
  90. * $priority => callback, ...
  91. * )
  92. * )
  93. *
  94. * @access private
  95. * @return array
  96. */
  97. public function getAllHandlers() {
  98. return $this->handlers;
  99. }
  100. /**
  101. * Does the hook have a handler?
  102. *
  103. * @param string $name The name of the hook
  104. * @param string $type The type of the hook
  105. * @return boolean
  106. */
  107. public function hasHandler($name, $type) {
  108. return isset($this->handlers[$name][$type]);
  109. }
  110. /**
  111. * Returns an ordered array of handlers registered for $name and $type.
  112. *
  113. * @param string $name The name of the hook
  114. * @param string $type The type of the hook
  115. * @return array
  116. * @see \Elgg\HooksRegistrationService::getAllHandlers()
  117. *
  118. * @access private
  119. */
  120. public function getOrderedHandlers($name, $type) {
  121. $handlers = array();
  122. if (isset($this->handlers[$name][$type])) {
  123. if ($name != 'all' && $type != 'all') {
  124. $handlers = array_merge($handlers, array_values($this->handlers[$name][$type]));
  125. }
  126. }
  127. if (isset($this->handlers['all'][$type])) {
  128. if ($type != 'all') {
  129. $handlers = array_merge($handlers, array_values($this->handlers['all'][$type]));
  130. }
  131. }
  132. if (isset($this->handlers[$name]['all'])) {
  133. if ($name != 'all') {
  134. $handlers = array_merge($handlers, array_values($this->handlers[$name]['all']));
  135. }
  136. }
  137. if (isset($this->handlers['all']['all'])) {
  138. $handlers = array_merge($handlers, array_values($this->handlers['all']['all']));
  139. }
  140. return $handlers;
  141. }
  142. /**
  143. * Create a matcher for the given callable (if it's for a static or dynamic method)
  144. *
  145. * @param callable $spec Callable we're creating a matcher for
  146. *
  147. * @return MethodMatcher|null
  148. */
  149. protected function getMatcher($spec) {
  150. if (is_string($spec) && false !== strpos($spec, '::')) {
  151. list ($type, $method) = explode('::', $spec, 2);
  152. return new MethodMatcher($type, $method);
  153. }
  154. if (!is_array($spec) || empty($spec[0]) || empty($spec[1]) || !is_string($spec[1])) {
  155. return null;
  156. }
  157. if (is_object($spec[0])) {
  158. $spec[0] = get_class($spec[0]);
  159. }
  160. if (!is_string($spec[0])) {
  161. return null;
  162. }
  163. return new MethodMatcher($spec[0], $spec[1]);
  164. }
  165. }