Open Journal Systems  3.0.0
 All Data Structures Namespaces Functions Variables Groups Pages
HookRegistry.inc.php
1 <?php
2 
17 class HookRegistry {
24  static function &getHooks($hookName = null) {
25  $hooks =& Registry::get('hooks', true, array());
26 
27  if ($hookName) {
28  if (isset($hooks[$hookName])) {
29  $hook =& $hooks[$hookName];
30  } else {
31  $hook = null;
32  }
33  return $hook;
34  }
35 
36  return $hooks;
37  }
38 
45  static function setHooks($hookName, $callbacks) {
46  $hooks =& HookRegistry::getHooks();
47  $hooks[$hookName] =& $callbacks;
48  }
49 
54  static function clear($hookName) {
55  $hooks =& HookRegistry::getHooks();
56  unset($hooks[$hookName]);
57  return $hooks;
58  }
59 
65  static function register($hookName, $callback) {
66  $hooks =& HookRegistry::getHooks();
67  if (!isset($hooks[$hookName])) {
68  $hooks[$hookName] = array();
69  }
70  $hooks[$hookName][] =& $callback;
71  }
72 
84  static function call($hookName, $args = null) {
85  // Called only by Unit Test
86  // The implementation is a bit quirky as this has to work when
87  // executed statically.
88  if (self::rememberCalledHooks(true)) {
89  // Remember the called hooks for testing.
90  $calledHooks =& HookRegistry::getCalledHooks();
91  $calledHooks[] = array(
92  $hookName, $args
93  );
94  }
95 
96  $hooks =& HookRegistry::getHooks();
97  if (!isset($hooks[$hookName])) {
98  return false;
99  }
100 
101  foreach ($hooks[$hookName] as $hook) {
102  if ($result = call_user_func($hook, $hookName, $args)) {
103  break;
104  }
105  }
106 
107  return $result;
108  }
109 
110 
111  //
112  // Methods required for testing only.
113  //
123  static function rememberCalledHooks($askOnly = false, $updateTo = true) {
124  static $rememberCalledHooks = false;
125  if (!$askOnly) {
126  $rememberCalledHooks = $updateTo;
127  }
128  return $rememberCalledHooks;
129  }
130 
138  static function resetCalledHooks($leaveAlive = false) {
139  if (!$leaveAlive) HookRegistry::rememberCalledHooks(false, false);
140  $calledHooks =& HookRegistry::getCalledHooks();
141  $calledHooks = array();
142  }
143 
148  static function &getCalledHooks() {
149  static $calledHooks = array();
150  return $calledHooks;
151  }
152 }
153 
154 ?>
static clear($hookName)
static call($hookName, $args=null)
static & getHooks($hookName=null)
static resetCalledHooks($leaveAlive=false)
static rememberCalledHooks($askOnly=false, $updateTo=true)
static setHooks($hookName, $callbacks)
static & get($key, $createIfEmpty=false, $createWithDefault=null)
static & getCalledHooks()
Class for linking core functionality with plugins.