Open Journal Systems  3.0.0
 All Classes Namespaces Functions Variables Groups Pages
HookRegistry.inc.php
1 <?php
2 
17 class HookRegistry {
21  static function &getHooks() {
22  $hooks =& Registry::get('hooks', true, array());
23  return $hooks;
24  }
25 
32  static function setHooks($hookName, $callbacks) {
33  $hooks =& HookRegistry::getHooks();
34  $hooks[$hookName] =& $callbacks;
35  }
36 
41  static function clear($hookName) {
42  $hooks =& HookRegistry::getHooks();
43  unset($hooks[$hookName]);
44  return $hooks;
45  }
46 
52  static function register($hookName, $callback) {
53  $hooks =& HookRegistry::getHooks();
54  if (!isset($hooks[$hookName])) {
55  $hooks[$hookName] = array();
56  }
57  $hooks[$hookName][] =& $callback;
58  }
59 
71  static function call($hookName, $args = null) {
72  // Called only by Unit Test
73  // The implementation is a bit quirky as this has to work when
74  // executed statically.
75  if (self::rememberCalledHooks(true)) {
76  // Remember the called hooks for testing.
77  $calledHooks =& HookRegistry::getCalledHooks();
78  $calledHooks[] = array(
79  $hookName, $args
80  );
81  }
82 
83  $hooks =& HookRegistry::getHooks();
84  if (!isset($hooks[$hookName])) {
85  return false;
86  }
87 
88  foreach ($hooks[$hookName] as $hook) {
89  if ($result = call_user_func($hook, $hookName, $args)) {
90  break;
91  }
92  }
93 
94  return $result;
95  }
96 
97 
98  //
99  // Methods required for testing only.
100  //
110  static function rememberCalledHooks($askOnly = false, $updateTo = true) {
111  static $rememberCalledHooks = false;
112  if (!$askOnly) {
113  $rememberCalledHooks = $updateTo;
114  }
115  return $rememberCalledHooks;
116  }
117 
125  static function resetCalledHooks($leaveAlive = false) {
126  if (!$leaveAlive) HookRegistry::rememberCalledHooks(false, false);
127  $calledHooks =& HookRegistry::getCalledHooks();
128  $calledHooks = array();
129  }
130 
135  static function &getCalledHooks() {
136  static $calledHooks = array();
137  return $calledHooks;
138  }
139 }
140 
141 ?>
static clear($hookName)
static call($hookName, $args=null)
static resetCalledHooks($leaveAlive=false)
static rememberCalledHooks($askOnly=false, $updateTo=true)
static setHooks($hookName, $callbacks)
static & get($key, $createIfEmpty=false, $createWithDefault=null)
static & getHooks()
static & getCalledHooks()
Class for linking core functionality with plugins.