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: 89: 90:
91: public function registerChain($sChainName) {
92: $aParam = array();
93: $iNumArgs = func_num_args();
94:
95: for ($iCount = 0; $iCount < $iNumArgs; $iCount++) {
96: $aParam[$iCount] = func_get_arg($iCount);
97: }
98:
99: $this->_addChain($sChainName, $aParam);
100: }
101:
102: 103: 104: 105: 106: 107: 108:
109: public function unregisterChain($sChainName) {
110:
111: if (!$this->isChainRegistered($sChainName)) {
112: throw new cInvalidArgumentException('Chain ' . $sChainName . ' doesn\'t exist.');
113: }
114:
115: $functions = array();
116: $this->_resetIterator($sChainName);
117: $chainFunctions = $this->_aChains[$sChainName]['functions'];
118: foreach ($chainFunctions as $pos => $item) {
119: $functions[] = $item->getFunctionName();
120: }
121:
122: foreach ($functions as $p => $func) {
123: $this->removeChainFunction($sChainName, $func);
124: }
125:
126: unset($this->_aChains[$sChainName]);
127: }
128:
129: 130: 131: 132: 133: 134: 135:
136: public function isChainRegistered($sChainName) {
137: return (isset($this->_aChains[$sChainName]));
138: }
139:
140: 141: 142: 143: 144:
145: public function getRegisteredChainNames() {
146: return array_keys($this->_aChains);
147: }
148:
149: 150: 151: 152: 153: 154:
155: protected function _addChain($sChainName, array $aParameters = array()) {
156: $cfg = cRegistry::getConfig();
157:
158: if ($cfg['debug']['disable_chains']) {
159: return;
160: }
161:
162: $this->_aChains[$sChainName]['parameters'] = $aParameters;
163: $this->_aChains[$sChainName]['functions'] = array();
164: }
165:
166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182:
183: public function addChainFunction($sChainName, $sFunctionName) {
184: $cfg = cRegistry::getConfig();
185:
186: if ($cfg['debug']['disable_chains']) {
187: return;
188: }
189:
190:
191: if (!$this->isChainRegistered($sChainName)) {
192: throw new cInvalidArgumentException('Chain ' . $sChainName . ' doesn\'t exist.');
193: }
194:
195: if (strpos($sFunctionName, '->') > 0) {
196:
197: list($class, $method) = explode('->', $sFunctionName);
198: if (!class_exists($class)) {
199: throw new cInvalidArgumentException('Class ' . $class . ' doesn\'t exist, can\'t add ' . $sFunctionName . ' to chain ' . $sChainName);
200: } elseif (!method_exists($class, $method)) {
201: throw new cInvalidArgumentException('Method ' . $method . ' in class ' . $class . ' doesn\'t exist, can\'t add ' . $sFunctionName . ' to chain ' . $sChainName);
202: }
203: $call = array(
204: new $class(), $method
205: );
206: } elseif (strpos($sFunctionName, '::') > 0) {
207:
208: list($class, $method) = explode('::', $sFunctionName);
209: if (!class_exists($class)) {
210: throw new cInvalidArgumentException('Class ' . $class . ' doesn\'t exist, can\'t add ' . $sFunctionName . ' to chain ' . $sChainName);
211: } elseif (!method_exists($class, $method)) {
212: throw new cInvalidArgumentException('Method ' . $method . ' in class ' . $class . ' doesn\'t exist, can\'t add ' . $sFunctionName . ' to chain ' . $sChainName);
213: }
214: $call = array($class, $method);
215: } else {
216:
217: if (!function_exists($sFunctionName)) {
218: throw new cInvalidArgumentException('Function ' . $sFunctionName . ' doesn\'t exist, can\'t add to chain ' . $sChainName);
219: }
220: $call = $sFunctionName;
221: }
222:
223:
224: if (!is_callable($call)) {
225: throw new cInvalidArgumentException('Function ' . $sFunctionName . ' isn\'t callable, can\'t add to chain ' . $sChainName);
226: }
227:
228: $oChainItem = new cApiCecChainItem($sChainName, $sFunctionName, $this->_aChains[$sChainName]['parameters']);
229: $oChainItem->setCallback($call);
230: $this->_aChains[$sChainName]['functions'][] = $oChainItem;
231:
232: return true;
233: }
234:
235: 236: 237: 238: 239: 240: 241: 242:
243: public function chainFunctionExists($sChainName, $sFunctionName) {
244: if (!$this->isChainRegistered($sChainName)) {
245: return false;
246: }
247:
248: $this->_resetIterator($sChainName);
249: $chainFunctions = $this->_aChains[$sChainName]['functions'];
250: foreach ($chainFunctions as $pos => $item) {
251: if ($item->getFunctionName() == $sFunctionName) {
252: return true;
253: }
254: }
255:
256: return false;
257: }
258:
259: 260: 261: 262: 263: 264:
265: public function removeChainFunction($sChainName, $sFunctionName) {
266: if (!$this->isChainRegistered($sChainName)) {
267: return;
268: }
269:
270: $this->_resetIterator($sChainName);
271: $chainFunctions = $this->_aChains[$sChainName]['functions'];
272: foreach ($this->_aChains[$sChainName]['functions'] as $pos => $item) {
273: if ($item->getFunctionName() == $sFunctionName) {
274: unset($this->_aChains[$sChainName]['functions'][$pos]);
275:
276: return;
277: }
278: }
279: }
280:
281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291:
292: public function getIterator($sChainName) {
293: return new cIterator($this->_aChains[$sChainName]['functions']);
294: }
295:
296: 297: 298: 299: 300:
301: protected function _resetIterator($sChainName) {
302: $iterator = $this->getIterator($sChainName);
303: $iterator->reset();
304: }
305:
306: }
307:
308: 309: 310: 311: 312: 313:
314: class cApiCecChainItem {
315:
316: 317: 318: 319: 320:
321: protected $_sChainName;
322:
323: 324: 325: 326: 327:
328: protected $_sFunctionName;
329:
330: 331: 332: 333: 334: 335: 336: 337:
338: protected $_mCallback;
339:
340: 341: 342: 343: 344:
345: protected $_aParameters;
346:
347: 348: 349: 350: 351:
352: protected $_mTemporaryArguments;
353:
354: 355: 356: 357: 358: 359: 360:
361: public function __construct($sChainName, $sFunctionName, $aParameters) {
362: $this->setChainName($sChainName);
363: $this->setFunctionName($sFunctionName);
364: $this->setParameters($aParameters);
365: $this->setCallback($this->getFunctionName());
366: }
367:
368: 369: 370: 371: 372:
373: public function setChainName($sChainName) {
374: $this->_sChainName = $sChainName;
375: }
376:
377: 378: 379: 380: 381:
382: public function getChainName() {
383: return $this->_sChainName;
384: }
385:
386: 387: 388: 389: 390:
391: public function setFunctionName($sFunctionName) {
392: $this->_sFunctionName = $sFunctionName;
393: }
394:
395: 396: 397: 398: 399:
400: public function getFunctionName() {
401: return $this->_sFunctionName;
402: }
403:
404: 405: 406: 407: 408:
409: public function setParameters(array $aParameters) {
410: $this->_aParameters = $aParameters;
411: }
412:
413: 414: 415: 416: 417:
418: public function getParameters() {
419: return $this->_aParameters;
420: }
421:
422: 423: 424: 425: 426: 427:
428: public function setCallback($callback) {
429: if (is_string($callback) || is_array($callback)) {
430: $this->_mCallback = $callback;
431: } else {
432: throw new cInvalidArgumentException("Callback has to be a string or an array.");
433: }
434: }
435:
436: 437: 438: 439: 440:
441: public function getCallback() {
442: return $this->_mCallback;
443: }
444:
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: }
486: