1: <?php
2: /**
3: * This file contains the CEC registry class.
4: *
5: * @package Core
6: * @subpackage CEC
7: * @author Timo A. Hummel
8: * @author Murat Purc <murat@purc.de>
9: * @copyright four for business AG <www.4fb.de>
10: * @license http://www.contenido.org/license/LIZENZ.txt
11: * @link http://www.4fb.de
12: * @link http://www.contenido.org
13: */
14:
15: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
16:
17: /**
18: * CEC registry class.
19: * Used to register chains and chain functions to invoke.
20: *
21: * Following 3 types of CEC functions/callbacks are supported at the moment:
22: * - Callbacks, which should only be invoked. They don't return a value and have
23: * no
24: * break conditions, @see cApiCecHook::execute()
25: * - Callbacks, which should return a value and/or should modify a passed
26: * parameter,
27: *
28: * @see cApiCecHook::executeAndReturn() - Callbacks, which should be processed
29: * untill a defined break condition achieves,
30: * @see cApiCecHook::executeWhileBreakCondition()
31: *
32: * @package Core
33: * @subpackage CEC
34: */
35: class cApiCecRegistry {
36:
37: /**
38: * List of available chains
39: *
40: * @var array
41: */
42: private $_aChains;
43:
44: /**
45: * Self instance
46: *
47: * @var cApiCecRegistry
48: */
49: private static $_instance = NULL;
50:
51: /**
52: * Constructor to create an instance of this class.
53: */
54: protected function __construct() {
55: $this->_aChains = array();
56: }
57:
58: /**
59: * Prevent cloning
60: */
61: private function __clone() {
62: // donut
63: }
64:
65: /**
66: * Returns a instance of cApiCecRegistry
67: *
68: * @return cApiCecRegistry
69: */
70: public static function getInstance() {
71: if (self::$_instance == NULL) {
72: self::$_instance = new cApiCecRegistry();
73: }
74:
75: return self::$_instance;
76: }
77:
78: /**
79: * Registers a chain (adds the chain to the internal chain holder)
80: * NOTE: The number of parameter is not restricted.
81: * You can pass
82: * as much parameter as you want.
83: *
84: * @deprecated [2014-08-07]
85: * This method is deprecated and is not needed any longer
86: * @param string $sChainName
87: */
88: public function registerChain($sChainName) {
89: cDeprecated('This method is deprecated and is not needed any longer');
90: }
91:
92: /**
93: * Unregisters a chain
94: *
95: * @deprecated [2014-08-07]
96: * This method is deprecated and is not needed any longer
97: * @param string $sChainName
98: */
99: public function unregisterChain($sChainName) {
100: cDeprecated('This method is deprecated and is not needed any longer');
101: }
102:
103: /**
104: * Checks if a chain is registered or not.
105: *
106: * @deprecated [2014-08-07]
107: * This method is deprecated and is not needed any longer
108: * @param string $sChainName
109: * @return bool
110: */
111: public function isChainRegistered($sChainName) {
112: cDeprecated('This method is deprecated and is not needed any longer');
113: return true;
114: }
115:
116: /**
117: * Returns list of registered chain names
118: *
119: * @deprecated [2014-08-07]
120: * This method is deprecated and is not needed any longer
121: * @return array
122: */
123: public function getRegisteredChainNames() {
124: cDeprecated('This method is deprecated and is not needed any longer');
125: return array();
126: }
127:
128: /**
129: * Adds the chain to the internal chain holder
130: *
131: * @deprecated [2014-08-07]
132: * This method is deprecated and is not needed any longer
133: * @param string $sChainName
134: * Chain name
135: * @param array $aParameters [optional]
136: * Chain parameter
137: * @return NULL
138: */
139: protected function _addChain($sChainName, array $aParameters = array()) {
140: cDeprecated('This method is deprecated and is not needed any longer');
141: return NULL;
142: }
143:
144: /**
145: * Adds a chain function which is to invoke.
146: *
147: * @param string $sChainName
148: * Chain name
149: * @param string $sFunctionName
150: * Name of function/callback to invoke.
151: * Feasible values are:
152: * - "ClassName->methodName" to invoke a method of a ClassName
153: * instance.
154: * A instance of the clas will be created here.
155: * - "ClassName::methodName" to invoke a static method of ClassName.
156: * - "FunctionName" to invoke a function.
157: * NOTE: Necessary files must be manually included before or by
158: * defined autoloader.
159: *
160: * @return bool
161: * True on success, otherwise false
162: *
163: * @throws cInvalidArgumentException
164: * if the given chain is not registered or the given callback is not callable
165: */
166: public function addChainFunction($sChainName, $sFunctionName) {
167: $cfg = cRegistry::getConfig();
168:
169: // do not add the chain if the chain system is disabled
170: if ($cfg['debug']['disable_chains']) {
171: return false;
172: }
173:
174: if (cString::findFirstPos($sFunctionName, '->') > 0) {
175: // chain function is a method of a object instance
176: list($class, $method) = explode('->', $sFunctionName);
177: if (!class_exists($class)) {
178: throw new cInvalidArgumentException('Class ' . $class . ' doesn\'t exist, can\'t add ' . $sFunctionName . ' to chain ' . $sChainName);
179: } elseif (!method_exists($class, $method)) {
180: throw new cInvalidArgumentException('Method ' . $method . ' in class ' . $class . ' doesn\'t exist, can\'t add ' . $sFunctionName . ' to chain ' . $sChainName);
181: }
182: $call = array(
183: new $class(),
184: $method
185: );
186: } elseif (cString::findFirstPos($sFunctionName, '::') > 0) {
187: // chain function is static method of a object
188: list($class, $method) = explode('::', $sFunctionName);
189: if (!class_exists($class)) {
190: throw new cInvalidArgumentException('Class ' . $class . ' doesn\'t exist, can\'t add ' . $sFunctionName . ' to chain ' . $sChainName);
191: } elseif (!method_exists($class, $method)) {
192: throw new cInvalidArgumentException('Method ' . $method . ' in class ' . $class . ' doesn\'t exist, can\'t add ' . $sFunctionName . ' to chain ' . $sChainName);
193: }
194: $call = array(
195: $class,
196: $method
197: );
198: } else {
199: // chain function is a function
200: if (!function_exists($sFunctionName)) {
201: throw new cInvalidArgumentException('Function ' . $sFunctionName . ' doesn\'t exist, can\'t add to chain ' . $sChainName);
202: }
203: $call = $sFunctionName;
204: }
205:
206: // Last check if the callback is callable
207: if (!is_callable($call)) {
208: throw new cInvalidArgumentException('Function ' . $sFunctionName . ' isn\'t callable, can\'t add to chain ' . $sChainName);
209: }
210:
211: if (!isset($this->_aChains[$sChainName])) {
212: $this->_aChains[$sChainName] = [
213: 'functions' => [],
214: 'parameters' => [],
215: ];
216: }
217:
218: $oChainItem = new cApiCecChainItem($sChainName, $sFunctionName, $this->_aChains[$sChainName]['parameters']);
219: $oChainItem->setCallback($call);
220:
221: $this->_aChains[$sChainName]['functions'][] = $oChainItem;
222:
223: return true;
224: }
225:
226: /**
227: * Checks if a chain function exists.
228: *
229: * @param string $sChainName
230: * Chain name
231: * @param string $sFunctionName
232: * Name of function to check
233: * @return bool
234: */
235: public function chainFunctionExists($sChainName, $sFunctionName) {
236: $this->_resetIterator($sChainName);
237: $chainFunctions = $this->_aChains[$sChainName]['functions'];
238: foreach ($chainFunctions as $pos => $item) {
239: if ($item->getFunctionName() == $sFunctionName) {
240: return true;
241: }
242: }
243:
244: return false;
245: }
246:
247: /**
248: * Removes a chain function.
249: *
250: * @param string $sChainName
251: * Chain name
252: * @param string $sFunctionName
253: * Name of function to remove from chain.
254: */
255: public function removeChainFunction($sChainName, $sFunctionName) {
256: $this->_resetIterator($sChainName);
257:
258: foreach ($this->_aChains[$sChainName]['functions'] as $pos => $item) {
259: if ($item->getFunctionName() == $sFunctionName) {
260: unset($this->_aChains[$sChainName]['functions'][$pos]);
261:
262: return;
263: }
264: }
265: }
266:
267: /**
268: * Returns the iterator for a desired chain.
269: *
270: * @param string $sChainName
271: * Chain name
272: * @return cIterator
273: */
274: public function getIterator($sChainName) {
275: if (isset($this->_aChains[$sChainName]) && isset($this->_aChains[$sChainName]['functions'])) {
276: return new cIterator($this->_aChains[$sChainName]['functions']);
277: } else {
278: return new cIterator([]);
279: }
280: }
281:
282: /**
283: * Resets the chain iterator.
284: *
285: * @param string $sChainName
286: */
287: protected function _resetIterator($sChainName) {
288: $iterator = $this->getIterator($sChainName);
289: $iterator->reset();
290: }
291:
292: /**
293: * Flushs added chains
294: *
295: */
296: public function flushAddedChains() {
297: $this->_aChains = array();
298: }
299: }
300:
301: /**
302: * CEC chain item class.
303: *
304: * @package Core
305: * @subpackage CEC
306: */
307: class cApiCecChainItem {
308:
309: /**
310: * Chain name
311: *
312: * @var string
313: */
314: protected $_sChainName;
315:
316: /**
317: * Name of function to invoke
318: *
319: * @var string
320: */
321: protected $_sFunctionName;
322:
323: /**
324: * Callback name.
325: * Contains either the function name to invoke, or a indexed array
326: * (class/object and method)
327: * and it's method to execute.
328: *
329: * @var array string
330: */
331: protected $_mCallback;
332:
333: /**
334: * Parameter to pass to the function
335: *
336: * @var array
337: */
338: protected $_aParameters;
339:
340: /**
341: * Temporary arguments holder
342: *
343: * @var array NULL
344: */
345: protected $_mTemporaryArguments;
346:
347: /**
348: * Constructor to create an instance of this class.
349: *
350: * Sets the CEC chain item properties.
351: *
352: * @param string $sChainName
353: * @param string $sFunctionName
354: * @param array $aParameters
355: *
356: * @throws cInvalidArgumentException
357: */
358: public function __construct($sChainName, $sFunctionName, $aParameters) {
359: $this->setChainName($sChainName);
360: $this->setFunctionName($sFunctionName);
361: $this->setCallback($this->getFunctionName());
362: }
363:
364: /**
365: * Sets the chain name
366: *
367: * @param string $sChainName
368: */
369: public function setChainName($sChainName) {
370: $this->_sChainName = $sChainName;
371: }
372:
373: /**
374: * Returns the chain name
375: *
376: * @return string
377: */
378: public function getChainName() {
379: return $this->_sChainName;
380: }
381:
382: /**
383: * Sets the function name
384: *
385: * @param string $sFunctionName
386: */
387: public function setFunctionName($sFunctionName) {
388: $this->_sFunctionName = $sFunctionName;
389: }
390:
391: /**
392: * Returns the function name
393: *
394: * @return string
395: */
396: public function getFunctionName() {
397: return $this->_sFunctionName;
398: }
399:
400: /**
401: * Sets the callback parameters
402: *
403: * @deprecated [2014-08-07]
404: * This method is deprecated and is not needed any longer
405: * @param array $aParameters
406: */
407: public function setParameters(array $aParameters) {
408: cDeprecated('This method is deprecated and is not needed any longer');
409: }
410:
411: /**
412: * Returns the function name
413: *
414: * @deprecated [2014-08-07]
415: * This method is deprecated and is not needed any longer
416: * @return array
417: */
418: public function getParameters() {
419: cDeprecated('This method is deprecated and is not needed any longer');
420: return array();
421: }
422:
423: /**
424: * Sets the callback
425: *
426: * @param string|array $callback
427: *
428: * @throws cInvalidArgumentException if the given callback is not a string
429: * or an array
430: */
431: public function setCallback($callback) {
432: if (is_string($callback) || is_array($callback)) {
433: $this->_mCallback = $callback;
434: } else {
435: throw new cInvalidArgumentException("Callback has to be a string or an array.");
436: }
437: }
438:
439: /**
440: * Returns the callback
441: *
442: * @return string|array
443: */
444: public function getCallback() {
445: return $this->_mCallback;
446: }
447:
448: /**
449: * Another way to set the arguments before invoking execute() method.
450: *
451: * @param array $args [optional]
452: */
453: public function setTemporaryArguments(array $args = array()) {
454: $this->_mTemporaryArguments = $args;
455: }
456:
457: /**
458: * Will be invoked by execute() method.
459: * If temporary arguments where set before, it returns them and resets the
460: * property.
461: *
462: * @return array
463: */
464: public function getTemporaryArguments() {
465: $args = $this->_mTemporaryArguments;
466: $this->_mTemporaryArguments = NULL;
467:
468: return $args;
469: }
470:
471: /**
472: * Invokes the CEC function/callback.
473: *
474: * @return mixed
475: * If available, the result of the CEC function/callback
476: */
477: public function execute() {
478: // get temporary arguments, if the where set before
479: if (!$args = $this->getTemporaryArguments()) {
480: // no temporary arguments available, get them by func_get_args()
481: $args = func_get_args();
482: }
483:
484: return call_user_func_array($this->getCallback(), $args);
485: }
486: }
487: