| 1 |
<?php |
| 2 |
/** |
| 3 |
* |
| 4 |
*/ |
| 5 |
|
| 6 |
//Import dependencies. |
| 7 |
require_once 'observer.interface.php'; |
| 8 |
|
| 9 |
/** |
| 10 |
* |
| 11 |
*/ |
| 12 |
abstract class CJTObserver implements CJTIObserver { |
| 13 |
|
| 14 |
/** |
| 15 |
* |
| 16 |
*/ |
| 17 |
const CALLBACK_CLASS = 0; |
| 18 |
|
| 19 |
/** |
| 20 |
* |
| 21 |
*/ |
| 22 |
const CALLBACK_METHOD = 1; |
| 23 |
|
| 24 |
/** |
| 25 |
* |
| 26 |
*/ |
| 27 |
const REDIRECT_MODE_PERMANENT = 0; |
| 28 |
|
| 29 |
/** |
| 30 |
* put your comment there... |
| 31 |
* |
| 32 |
* @var mixed |
| 33 |
*/ |
| 34 |
protected $callback; |
| 35 |
|
| 36 |
/** |
| 37 |
* put your comment there... |
| 38 |
* |
| 39 |
* @var mixed |
| 40 |
*/ |
| 41 |
protected $filter; |
| 42 |
|
| 43 |
/** |
| 44 |
* put your comment there... |
| 45 |
* |
| 46 |
* @var mixed |
| 47 |
*/ |
| 48 |
protected $key; |
| 49 |
|
| 50 |
/** |
| 51 |
* put your comment there... |
| 52 |
* |
| 53 |
* @var mixed |
| 54 |
*/ |
| 55 |
protected $name; |
| 56 |
|
| 57 |
/** |
| 58 |
* put your comment there... |
| 59 |
* |
| 60 |
* @var mixed |
| 61 |
*/ |
| 62 |
protected $param; |
| 63 |
|
| 64 |
/** |
| 65 |
* put your comment there... |
| 66 |
* |
| 67 |
* @var mixed |
| 68 |
*/ |
| 69 |
protected $params; |
| 70 |
|
| 71 |
/** |
| 72 |
* put your comment there... |
| 73 |
* |
| 74 |
* @var mixed |
| 75 |
*/ |
| 76 |
protected $subject; |
| 77 |
|
| 78 |
/** |
| 79 |
* put your comment there... |
| 80 |
* |
| 81 |
* @param mixed $name |
| 82 |
* @return CJTObserver |
| 83 |
*/ |
| 84 |
public function __construct($name, $filter = null) { |
| 85 |
$this->name = $name; |
| 86 |
$this->filter = $filter; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* put your comment there... |
| 91 |
* |
| 92 |
*/ |
| 93 |
public function getCallback($component = self::CALLBACK_CLASS) { |
| 94 |
$component = ($component !== null) ? $this->callback[$component] : $this->callback; |
| 95 |
return $component; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* put your comment there... |
| 100 |
* |
| 101 |
* @param mixed $name |
| 102 |
*/ |
| 103 |
public function getFilter($name) { |
| 104 |
return $this->filter[$name]; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* put your comment there... |
| 109 |
* |
| 110 |
* @param mixed $observer |
| 111 |
*/ |
| 112 |
public static function getInstance($subject, $callback) { |
| 113 |
if (is_object($callback)) { |
| 114 |
if (!in_array('CJTIObserver', class_implements($callback))) { |
| 115 |
throw new Exception('Invalid observer callback object! Please provide a native PHP callback or observable object!!'); |
| 116 |
} |
| 117 |
// callback is actually the observer! |
| 118 |
$observer = $callback; |
| 119 |
} |
| 120 |
else { |
| 121 |
// Define vars (E_ALL complain)!. |
| 122 |
$name = null; |
| 123 |
$filter = null; |
| 124 |
$param = null; |
| 125 |
// Short-hand array structure! |
| 126 |
if (is_array($callback) && isset($callback['callback'])) { |
| 127 |
// Get all params without callback |
| 128 |
if (isset($callback['name'])) { |
| 129 |
$name = $callback['name']; |
| 130 |
} |
| 131 |
if (isset($callback['filter'])) { |
| 132 |
$filter = $callback['filter']; |
| 133 |
} |
| 134 |
$param = $callback['params']; |
| 135 |
// Get PHP native CALLBACK! |
| 136 |
$callback = $callback['callback']; |
| 137 |
} |
| 138 |
// Instantiate Observer! |
| 139 |
$observerClass = $subject->getDefinition('observerClass'); |
| 140 |
$observer = new $observerClass($name, $filter); |
| 141 |
$observer->init($subject, $callback, $param); |
| 142 |
} |
| 143 |
return $observer; |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* put your comment there... |
| 148 |
* |
| 149 |
*/ |
| 150 |
public function getKey() { |
| 151 |
return $this->key; |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* put your comment there... |
| 156 |
* |
| 157 |
*/ |
| 158 |
public function getName() { |
| 159 |
return $this->name; |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* put your comment there... |
| 164 |
* |
| 165 |
*/ |
| 166 |
public function getParam() { |
| 167 |
return $this->param; |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* put your comment there... |
| 172 |
* |
| 173 |
*/ |
| 174 |
public function getSubject() { |
| 175 |
return $this->subject; |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* put your comment there... |
| 180 |
* |
| 181 |
*/ |
| 182 |
public function getTarget() { |
| 183 |
return $this->getSubject()->getTarget(); |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* put your comment there... |
| 188 |
* |
| 189 |
* @param mixed $subject |
| 190 |
* @param mixed $callback |
| 191 |
*/ |
| 192 |
protected function init($subject, $callback, $param) { |
| 193 |
// Initialize internals! |
| 194 |
$this->subject = $subject; |
| 195 |
$this->callback = $callback; |
| 196 |
$this->param = $param; |
| 197 |
// Chain! |
| 198 |
return $this; |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* put your comment there... |
| 203 |
* |
| 204 |
* @param mixed $callback |
| 205 |
* @param mixed $mode |
| 206 |
*/ |
| 207 |
public function redirect($callback, $mode = self::REDIRECT_MODE_PERMANENT) { |
| 208 |
// If callback is a class method and only the calss is passed |
| 209 |
// use event name as the method name (DIRECT-NAMING-MAP)! DNM |
| 210 |
if (is_array($callback) && !isset($callback[1])) { |
| 211 |
// @TODO Don't use HJARD-CODED 'on PREFIX! |
| 212 |
$callback[1] = "on{$this->subject->getName()}"; |
| 213 |
} |
| 214 |
// Redirect based on the mode! |
| 215 |
switch ($mode) { |
| 216 |
// Remove current observer and add another one to be called |
| 217 |
// next and forever! |
| 218 |
case self::REDIRECT_MODE_PERMANENT : |
| 219 |
// Create a redirect observer and tell it to deattach me! |
| 220 |
$this->subject[] = $callback; |
| 221 |
break; |
| 222 |
} |
| 223 |
return $this->redirectReturn(); |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* put your comment there... |
| 228 |
* |
| 229 |
*/ |
| 230 |
protected abstract function redirectReturn(); |
| 231 |
|
| 232 |
/** |
| 233 |
* put your comment there... |
| 234 |
* |
| 235 |
*/ |
| 236 |
public function trigger() { |
| 237 |
$this->params = func_get_args(); |
| 238 |
// Callback! |
| 239 |
$return = call_user_func_array($this->callback, $this->params); |
| 240 |
return $return; |
| 241 |
} |
| 242 |
|
| 243 |
} // End class |