1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15:
16:
17: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
18:
19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36:
37: class cApiCecRegistry {
38:
39: 40: 41: 42: 43:
44: private $_aChains;
45:
46: 47: 48: 49: 50:
51: private static $_instance = NULL;
52:
53: 54: 55:
56: protected function __construct() {
57: $this->_aChains = array();
58: }
59:
60: 61: 62:
63: private function __clone() {
64:
65: }
66:
67: 68: 69: 70: 71:
72: public static function getInstance() {
73: if (self::$_instance == NULL) {
74: self::$_instance = new cApiCecRegistry();
75: }
76:
77: return self::$_instance;
78: }
79:
80: 81: 82: 83: 84: 85: 86: 87:
88: public function registerChain($sChainName) {
89: $aParam = array();
90: $iNumArgs = func_num_args();
91:
92: for ($iCount = 0; $iCount < $iNumArgs; $iCount++) {
93: $aParam[$iCount] = func_get_arg($iCount);
94: }
95:
96: $this->_addChain($sChainName, $aParam);
97: }
98:
99: 100: 101: 102: 103: 104: 105:
106: public function unregisterChain($sChainName) {
107:
108: if (!$this->isChainRegistered($sChainName)) {
109: throw new cInvalidArgumentException('Chain ' . $sChainName . ' doesn\'t exist.');
110: }
111:
112: $functions = array();
113: $this->_resetIterator($sChainName);
114: $chainFunctions = $this->_aChains[$sChainName]['functions'];
115: foreach ($chainFunctions as $pos => $item) {
116: $functions[] = $item->getFunctionName();
117: }
118:
119: foreach ($functions as $p => $func) {
120: $this->removeChainFunction($sChainName, $func);
121: }
122:
123: unset($this->_aChains[$sChainName]);
124: }
125:
126: 127: 128: 129: 130: 131: 132:
133: public function isChainRegistered($sChainName) {
134: return (isset($this->_aChains[$sChainName]));
135: }
136:
137: 138: 139: 140: 141:
142: public function getRegisteredChainNames() {
143: return array_keys($this->_aChains);
144: }
145:
146: 147: 148: 149: 150: 151:
152: protected function _addChain($sChainName, array $aParameters = array()) {
153: $cfg = cRegistry::getConfig();
154:
155: if ($cfg['debug']['disable_chains']) {
156: return;
157: }
158:
159: $this->_aChains[$sChainName]['parameters'] = $aParameters;
160: $this->_aChains[$sChainName]['functions'] = array();
161: }
162:
163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180:
181: public function addChainFunction($sChainName, $sFunctionName) {
182: $cfg = cRegistry::getConfig();
183:
184: if ($cfg['debug']['disable_chains']) {
185: return;
186: }
187:
188:
189: if (!$this->isChainRegistered($sChainName)) {
190: throw new cInvalidArgumentException('Chain ' . $sChainName . ' doesn\'t exist.');
191: }
192:
193: if (strpos($sFunctionName, '->') > 0) {
194:
195: list($class, $method) = explode('->', $sFunctionName);
196: if (!class_exists($class)) {
197: throw new cInvalidArgumentException('Class ' . $class . ' doesn\'t exist, can\'t add ' . $sFunctionName . ' to chain ' . $sChainName);
198: } elseif (!method_exists($class, $method)) {
199: throw new cInvalidArgumentException('Method ' . $method . ' in class ' . $class . ' doesn\'t exist, can\'t add ' . $sFunctionName . ' to chain ' . $sChainName);
200: }
201: $call = array(
202: new $class(),
203: $method
204: );
205: } elseif (strpos($sFunctionName, '::') > 0) {
206:
207: list($class, $method) = explode('::', $sFunctionName);
208: if (!class_exists($class)) {
209: throw new cInvalidArgumentException('Class ' . $class . ' doesn\'t exist, can\'t add ' . $sFunctionName . ' to chain ' . $sChainName);
210: } elseif (!method_exists($class, $method)) {
211: throw new cInvalidArgumentException('Method ' . $method . ' in class ' . $class . ' doesn\'t exist, can\'t add ' . $sFunctionName . ' to chain ' . $sChainName);
212: }
213: $call = array(
214: $class,
215: $method
216: );
217: } else {
218:
219: if (!function_exists($sFunctionName)) {
220: throw new cInvalidArgumentException('Function ' . $sFunctionName . ' doesn\'t exist, can\'t add to chain ' . $sChainName);
221: }
222: $call = $sFunctionName;
223: }
224:
225:
226: if (!is_callable($call)) {
227: throw new cInvalidArgumentException('Function ' . $sFunctionName . ' isn\'t callable, can\'t add to chain ' . $sChainName);
228: }
229:
230: $oChainItem = new cApiCecChainItem($sChainName, $sFunctionName, $this->_aChains[$sChainName]['parameters']);
231: $oChainItem->setCallback($call);
232: $this->_aChains[$sChainName]['functions'][] = $oChainItem;
233:
234: return true;
235: }
236:
237: 238: 239: 240: 241: 242: 243: 244:
245: public function chainFunctionExists($sChainName, $sFunctionName) {
246: if (!$this->isChainRegistered($sChainName)) {
247: return false;
248: }
249:
250: $this->_resetIterator($sChainName);
251: $chainFunctions = $this->_aChains[$sChainName]['functions'];
252: foreach ($chainFunctions as $pos => $item) {
253: if ($item->getFunctionName() == $sFunctionName) {
254: return true;
255: }
256: }
257:
258: return false;
259: }
260:
261: 262: 263: 264: 265: 266:
267: public function removeChainFunction($sChainName, $sFunctionName) {
268: if (!$this->isChainRegistered($sChainName)) {
269: return;
270: }
271:
272: $this->_resetIterator($sChainName);
273: $chainFunctions = $this->_aChains[$sChainName]['functions'];
274: foreach ($this->_aChains[$sChainName]['functions'] as $pos => $item) {
275: if ($item->getFunctionName() == $sFunctionName) {
276: unset($this->_aChains[$sChainName]['functions'][$pos]);
277:
278: return;
279: }
280: }
281: }
282:
283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293:
294: public function getIterator($sChainName) {
295: return new cIterator($this->_aChains[$sChainName]['functions']);
296: }
297:
298: 299: 300: 301: 302:
303: protected function _resetIterator($sChainName) {
304: $iterator = $this->getIterator($sChainName);
305: $iterator->reset();
306: }
307: }
308:
309: 310: 311: 312: 313: 314:
315: class cApiCecChainItem {
316:
317: 318: 319: 320: 321:
322: protected $_sChainName;
323:
324: 325: 326: 327: 328:
329: protected $_sFunctionName;
330:
331: 332: 333: 334: 335: 336: 337: 338:
339: protected $_mCallback;
340:
341: 342: 343: 344: 345:
346: protected $_aParameters;
347:
348: 349: 350: 351: 352:
353: protected $_mTemporaryArguments;
354:
355: 356: 357: 358: 359: 360: 361:
362: public function __construct($sChainName, $sFunctionName, $aParameters) {
363: $this->setChainName($sChainName);
364: $this->setFunctionName($sFunctionName);
365: $this->setParameters($aParameters);
366: $this->setCallback($this->getFunctionName());
367: }
368:
369: 370: 371: 372: 373:
374: public function setChainName($sChainName) {
375: $this->_sChainName = $sChainName;
376: }
377:
378: 379: 380: 381: 382:
383: public function getChainName() {
384: return $this->_sChainName;
385: }
386:
387: 388: 389: 390: 391:
392: public function setFunctionName($sFunctionName) {
393: $this->_sFunctionName = $sFunctionName;
394: }
395:
396: 397: 398: 399: 400:
401: public function getFunctionName() {
402: return $this->_sFunctionName;
403: }
404:
405: 406: 407: 408: 409:
410: public function setParameters(array $aParameters) {
411: $this->_aParameters = $aParameters;
412: }
413:
414: 415: 416: 417: 418:
419: public function getParameters() {
420: return $this->_aParameters;
421: }
422:
423: 424: 425: 426: 427: 428: 429:
430: public function setCallback($callback) {
431: if (is_string($callback) || is_array($callback)) {
432: $this->_mCallback = $callback;
433: } else {
434: throw new cInvalidArgumentException("Callback has to be a string or an array.");
435: }
436: }
437:
438: 439: 440: 441: 442:
443: public function getCallback() {
444: return $this->_mCallback;
445: }
446:
447: 448: 449: 450: 451:
452: public function setTemporaryArguments(array $args = array()) {
453: $this->_mTemporaryArguments = $args;
454: }
455:
456: 457: 458: 459: 460: 461: 462:
463: public function getTemporaryArguments() {
464: $args = $this->_mTemporaryArguments;
465: $this->_mTemporaryArguments = NULL;
466:
467: return $args;
468: }
469:
470: 471: 472: 473: 474:
475: public function execute() {
476:
477: if (!$args = $this->getTemporaryArguments()) {
478:
479: $args = func_get_args();
480: }
481:
482: return call_user_func_array($this->getCallback(), $args);
483: }
484: }
485: