| 1 |
<?php |
| 2 |
/** |
| 3 |
* |
| 4 |
*/ |
| 5 |
|
| 6 |
/** |
| 7 |
* |
| 8 |
*/ |
| 9 |
abstract class CJTEvents { |
| 10 |
|
| 11 |
/** |
| 12 |
* put your comment there... |
| 13 |
* |
| 14 |
* @var mixed |
| 15 |
*/ |
| 16 |
protected static $classes = array(); |
| 17 |
|
| 18 |
/** |
| 19 |
* put your comment there... |
| 20 |
* |
| 21 |
* @var mixed |
| 22 |
*/ |
| 23 |
public static $defaultOptions = array('prefix' => 'on'); |
| 24 |
|
| 25 |
/** |
| 26 |
* put your comment there... |
| 27 |
* |
| 28 |
* @var mixed |
| 29 |
*/ |
| 30 |
public static $definition; |
| 31 |
|
| 32 |
/** |
| 33 |
* put your comment there... |
| 34 |
* |
| 35 |
* @var mixed |
| 36 |
*/ |
| 37 |
protected $freeStatic; |
| 38 |
|
| 39 |
/** |
| 40 |
* put your comment there... |
| 41 |
* |
| 42 |
* @var mixed |
| 43 |
*/ |
| 44 |
protected static $live; |
| 45 |
|
| 46 |
/** |
| 47 |
* put your comment there... |
| 48 |
* |
| 49 |
* @var mixed |
| 50 |
*/ |
| 51 |
private $options = array(); |
| 52 |
|
| 53 |
/** |
| 54 |
* put your comment there... |
| 55 |
* |
| 56 |
* @var mixed |
| 57 |
*/ |
| 58 |
public static $paths = array(); |
| 59 |
|
| 60 |
/** |
| 61 |
* put your comment there... |
| 62 |
* |
| 63 |
* @var mixed |
| 64 |
*/ |
| 65 |
protected $subjects; |
| 66 |
|
| 67 |
/** |
| 68 |
* put your comment there... |
| 69 |
* |
| 70 |
* @var mixed |
| 71 |
*/ |
| 72 |
private $target; |
| 73 |
|
| 74 |
/** |
| 75 |
* put your comment there... |
| 76 |
* |
| 77 |
* @var mixed |
| 78 |
*/ |
| 79 |
private $targetClass; |
| 80 |
|
| 81 |
/** |
| 82 |
* put your comment there... |
| 83 |
* |
| 84 |
* @param mixed $target |
| 85 |
* @param mixed $options |
| 86 |
* @return CJTEvents |
| 87 |
*/ |
| 88 |
public function __construct($target, $options = array(), $freeStatic = false) { |
| 89 |
// Initialize vars! |
| 90 |
$this->target = $target; |
| 91 |
$this->options = array_merge(self::$defaultOptions, $options); |
| 92 |
$this->freeStatic = $freeStatic; |
| 93 |
$this->targetClass = is_object($this->target) ? get_class($this->target) : $this->target; |
| 94 |
// Define class events if not defined yet! |
| 95 |
$this->define(); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* put your comment there... |
| 100 |
* |
| 101 |
* @param mixed $options |
| 102 |
*/ |
| 103 |
public static function _init($options = array()) { |
| 104 |
if (!self::$definition) { |
| 105 |
self::$defaultOptions += $options; |
| 106 |
// Definition object! |
| 107 |
self::$definition = (new CJTEventsDefinition()); |
| 108 |
// Paths! |
| 109 |
self::$paths['subjects'] = new CJTIncludes('subjects'); |
| 110 |
self::$paths['observers'] = new CJTIncludes('observers'); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* put your comment there... |
| 116 |
* |
| 117 |
* @param mixed $typeName |
| 118 |
* @param mixed $observer |
| 119 |
* @param mixed $typePrefixed |
| 120 |
* @return CJTEvents |
| 121 |
*/ |
| 122 |
public function bind($typeName, $observer, $typePrefixed = true) { |
| 123 |
$type = $this->parseEventType($typeName, $typePrefixed); |
| 124 |
$subject = $this->getSubject($type); |
| 125 |
$subject[] = $observer; |
| 126 |
return $this; |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* put your comment there... |
| 131 |
* |
| 132 |
* @param mixed $type |
| 133 |
*/ |
| 134 |
public function createSubject($event) { |
| 135 |
// Initializing! |
| 136 |
$subject = false; |
| 137 |
$event = $this->prepareEventTypeOptions($event); |
| 138 |
$type = $event['type']; |
| 139 |
// Import classd file if not exists! |
| 140 |
if (!class_exists($type['subjectClass'])) { |
| 141 |
self::$paths['subjects']->import($type['file']); |
| 142 |
if (!class_exists($type['subjectClass'])) { |
| 143 |
throw new Exception('Could not instantiate Subject class!! Class is not found!!'); |
| 144 |
} |
| 145 |
} |
| 146 |
$type['targetClass'] = $this->targetClass; |
| 147 |
// Instantiate! |
| 148 |
$subject = call_user_func(array($type['subjectClass'], 'getInstance'), $event['name'], $this->target, $type, self::$paths['observers']); |
| 149 |
return $subject; |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* put your comment there... |
| 154 |
* |
| 155 |
*/ |
| 156 |
protected function define() { |
| 157 |
$className = $this->targetClass; |
| 158 |
if (!isset(self::$classes[$className])) { |
| 159 |
// Add class Definition! |
| 160 |
self::$definition->define($className, $this->options); |
| 161 |
// Store! |
| 162 |
self::$classes[$className] = $this; |
| 163 |
} |
| 164 |
return self::$definition->get($className); |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* put your comment there... |
| 169 |
* |
| 170 |
* @deprecated |
| 171 |
*/ |
| 172 |
private function findEvents() { |
| 173 |
// If the class is not defined, define it! |
| 174 |
$typeDef = $this->define(); |
| 175 |
// Create subjects!! |
| 176 |
foreach ($typeDef['events'] as $event) { |
| 177 |
$eventId = $event['id']; |
| 178 |
if (!$subject = $this->getSubject($eventId)) { |
| 179 |
$subject = $this->subjects[$eventId] = $this->createSubject($event); |
| 180 |
// Live events! |
| 181 |
$lives = (array) self::$live[$this->targetClass][$eventId]; |
| 182 |
foreach ($lives as $live) { |
| 183 |
$subject[] = $live['observer']; |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* put your comment there... |
| 191 |
* |
| 192 |
* @param mixed $class |
| 193 |
*/ |
| 194 |
public function getDefinition() { |
| 195 |
return self::$definition->get($this->targetClass); |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* put your comment there... |
| 200 |
* |
| 201 |
* @param mixed $type |
| 202 |
*/ |
| 203 |
public function getSubject($type) { |
| 204 |
// Initialize. |
| 205 |
$definition = $this->getDefinition(); |
| 206 |
$events = $definition['events']; |
| 207 |
//print_r($type); |
| 208 |
// Instantiate subject if not ready yet! |
| 209 |
if (!isset($this->subjects[$type->name])) { |
| 210 |
// Check event type existance! |
| 211 |
if (!isset($events[$type->type])) { |
| 212 |
$type = print_r($type, true); |
| 213 |
throw new Exception("Event type is not found! Could not find ({$type}) event type!"); |
| 214 |
} |
| 215 |
$event = $events[$type->type]; |
| 216 |
// Create subject object! and bind live events! |
| 217 |
$subject = $this->subjects[$event['id']] = $this->createSubject($event); |
| 218 |
// Live events! |
| 219 |
$lives = isset(self::$live[$this->targetClass][$event['id']]) ? self::$live[$this->targetClass][$event['id']] : array(); |
| 220 |
foreach ($lives as $live) { |
| 221 |
$subject[] = $live['observer']; |
| 222 |
} |
| 223 |
} |
| 224 |
return $this->subjects[$type->name]; |
| 225 |
} |
| 226 |
|
| 227 |
/** |
| 228 |
* put your comment there... |
| 229 |
* |
| 230 |
* @param mixed $type |
| 231 |
* @return CJTWordpressEvents |
| 232 |
*/ |
| 233 |
public static function getTypeEvents($typeName, $typePrefixed = true) { |
| 234 |
$type = self::parseEventType($typeName, $typePrefixed); |
| 235 |
if (!self::$classes[$type->class]) { |
| 236 |
throw new Exception("Type not found!! Could not find {$typeName}!"); |
| 237 |
} |
| 238 |
return self::$classes[$type->class]; |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* put your comment there... |
| 243 |
* |
| 244 |
*/ |
| 245 |
public function isFreeStatic() { |
| 246 |
return $this->freeStatic; |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* put your comment there... |
| 251 |
* |
| 252 |
* @param mixed $typeName |
| 253 |
* @param mixed $observer |
| 254 |
*/ |
| 255 |
public function off($typeName, $observer) { |
| 256 |
|
| 257 |
} |
| 258 |
|
| 259 |
/** |
| 260 |
* put your comment there... |
| 261 |
* |
| 262 |
* @param mixed $typeName |
| 263 |
* @param mixed $observer |
| 264 |
*/ |
| 265 |
public static function on($typeName, $observer) { |
| 266 |
$type = self::parseEventType($typeName, false); |
| 267 |
// Create live event! |
| 268 |
$on = array(); |
| 269 |
$on['observer'] = $observer; |
| 270 |
self::$live[$type->class][$type->name][] = $on; |
| 271 |
} |
| 272 |
|
| 273 |
/** |
| 274 |
* put your comment there... |
| 275 |
* |
| 276 |
* @param mixed $type |
| 277 |
*/ |
| 278 |
public static function parseEventType($type, $typePrefixed = true) { |
| 279 |
$separator = '.'; |
| 280 |
$prefix = 'on'; |
| 281 |
// CLASS.TYPE |
| 282 |
$parts = explode('.', $type); |
| 283 |
// Return type array! |
| 284 |
$type = array(); |
| 285 |
if (count($parts) == 1) { |
| 286 |
$type['type'] = $parts[0]; |
| 287 |
} |
| 288 |
else { |
| 289 |
$type['class'] = $parts[0]; |
| 290 |
$type['separator'] = $separator; |
| 291 |
$type['type'] = $parts[1]; |
| 292 |
} |
| 293 |
if ($typePrefixed) { |
| 294 |
$type['name'] = substr($type['type'], strlen($prefix)); |
| 295 |
$type['prefix'] = $prefix; |
| 296 |
} |
| 297 |
else { |
| 298 |
$type['name'] = $type['type']; |
| 299 |
$type['type'] = "on{$type['type']}"; |
| 300 |
} |
| 301 |
return ((object) $type); |
| 302 |
} |
| 303 |
|
| 304 |
/** |
| 305 |
* put your comment there... |
| 306 |
* |
| 307 |
* @param mixed $type |
| 308 |
*/ |
| 309 |
protected function prepareEventTypeOptions($event) { |
| 310 |
return $event; |
| 311 |
} |
| 312 |
|
| 313 |
/** |
| 314 |
* put your comment there... |
| 315 |
* |
| 316 |
* @param mixed $type |
| 317 |
* @param mixed $params |
| 318 |
* @param mixed $typePrefixed |
| 319 |
*/ |
| 320 |
public abstract function trigger($type, $params = array(), $typePrefixed = true); |
| 321 |
|
| 322 |
/** |
| 323 |
* put your comment there... |
| 324 |
* |
| 325 |
* @param mixed $type |
| 326 |
* @param mixed $observer |
| 327 |
*/ |
| 328 |
public function unbind($type, $observer, $typePrefixed = true) { |
| 329 |
|
| 330 |
} |
| 331 |
|
| 332 |
} // End class. |