1: <?php
2: /**
3: * This file contains the generic db class.
4: *
5: * @package Core
6: * @subpackage GenericDB
7: * @version SVN Revision $Rev:$
8: *
9: * @author Dominik Ziegler
10: * @copyright four for business AG <www.4fb.de>
11: * @license http://www.contenido.org/license/LIZENZ.txt
12: * @link http://www.4fb.de
13: * @link http://www.contenido.org
14: */
15:
16: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
17:
18: /**
19: * Class cGenericDb.
20: * Handles the generic execution of callbacks.
21: *
22: * @package Core
23: * @subpackage GenericDB
24: */
25: class cGenericDb {
26: /**
27: * Callbacks are executed before a item is created.
28: * Expected parameters for callback: none
29: */
30: const CREATE_BEFORE = 10;
31:
32: /**
33: * Callbacks are executed if item could not be created.
34: * Expected parameters for callback: none
35: */
36: const CREATE_FAILURE = 11;
37:
38: /**
39: * Callbacks are executed if item could be created successfully.
40: * Expected parameters for callback: ID of created item
41: */
42: const CREATE_SUCCESS = 12;
43:
44:
45: /**
46: * Callbacks are executed before store process is executed.
47: * Expected parameters for callback: Item instance
48: */
49: const STORE_BEFORE = 20;
50:
51: /**
52: * Callbacks are executed if store process failed.
53: * This is also likely to happen if query would not change anything in database!
54: * Expected parameters for callback: Item instance
55: */
56: const STORE_FAILURE = 21;
57:
58: /**
59: * Callbacks are executed if store process saved the values in the database.
60: * Expected parameters for callback: Item instance
61: */
62: const STORE_SUCCESS = 22;
63:
64:
65: /**
66: * Callbacks are executed before deleting an item.
67: * Expected parameters for callback: ID of them item to delete
68: */
69: const DELETE_BEFORE = 30;
70:
71: /**
72: * Callbacks are executed if deletion of an item fails.
73: * Expected parameters for callback: ID of them item to delete
74: */
75: const DELETE_FAILURE = 31;
76:
77: /**
78: * Callbacks are executed if item was deleted successfully.
79: * Expected parameters for callback: ID of them item to delete
80: */
81: const DELETE_SUCCESS = 32;
82:
83: /**
84: * Callback stack.
85: * @var array
86: */
87: private static $_callbacks = array();
88:
89: /**
90: * Registers a new callback.
91: *
92: * Example:
93: * cGenericDb::register(cGenericDb::CREATE_SUCCESS, 'itemCreateHandler', 'cApiArticle');
94: * cGenericDb::register(cGenericDb::CREATE_SUCCESS, array('cCallbackHandler', 'executeCreateHandle'), 'cApiArticle');
95: *
96: * @param string $event Callback event, must be a valid value of a cGenericDb event constant
97: * @param mixed $callback Callback to register
98: * @param mixed $class Class name for registering callback (can be string of array with names of the concrete Item classes)
99: * @throws cInvalidArgumentException if event or class are not set or the callback is not callable
100: * @return void
101: */
102: public static function register($event, $callback, $class) {
103: if (isset($event) === false) {
104: throw new cInvalidArgumentException("No callback event for execution was given");
105: }
106:
107: if (is_callable($callback) === false) {
108: throw new cInvalidArgumentException("Given callback is not callable.");
109: }
110:
111: if (isset($class) === false) {
112: throw new cInvalidArgumentException("No class for registering callback was given.");
113: }
114:
115: if (is_array($class)) {
116: foreach ($class as $className) {
117: self::$_callbacks[$className][$event][] = $callback;
118: }
119: } else {
120: self::$_callbacks[$class][$event][] = $callback;
121: }
122: }
123:
124: /**
125: * Unregisters all callbacks for a specific event in a class.
126: *
127: * Example:
128: * cGenericDb::unregister(cGenericDb::CREATE_SUCCESS, 'cApiArticle');
129: *
130: * @param string $event Callback event, must be a valid value of a cGenericDb event constant
131: * @param mixed $class Class name for unregistering callback (can be string of array with names of the concrete Item classes)
132: * @throws cInvalidArgumentException if the event or the class are not set
133: * @return void
134: */
135: public static function unregister($event, $class) {
136: if (isset($event) === false) {
137: throw new cInvalidArgumentException("No callback event for execution was given");
138: }
139:
140: if (isset($class) === false) {
141: throw new cInvalidArgumentException("No class for unregistering callbacks was given.");
142: }
143:
144: if (is_array($class)) {
145: foreach ($class as $className) {
146: unset(self::$_callbacks[$className][$event]);
147: }
148: } else {
149: unset(self::$_callbacks[$class][$event]);
150: }
151: }
152:
153: /**
154: * Executes all callbacks for a specific event in a class.
155: *
156: * @param string $event Callback event, must be a valid value of a cGenericDb event constant
157: * @param string $class Class name for executing callback
158: * @param array $arguments Arguments to pass to the callback function
159: * @throws cInvalidArgumentException if the event or class is not set
160: * @return void
161: */
162: protected final function _executeCallbacks($event, $class, $arguments = array()) {
163: if (isset($event) === false) {
164: throw new cInvalidArgumentException("No callback event for execution was given");
165: }
166:
167: if (isset($class) === false) {
168: throw new cInvalidArgumentException("No class for executing callbacks was given.");
169: }
170:
171: if (!isset(self::$_callbacks[$class])) {
172: return;
173: }
174:
175: if (!isset(self::$_callbacks[$class][$event])) {
176: return;
177: }
178:
179: foreach (self::$_callbacks[$class][$event] as $callback) {
180: call_user_func_array($callback, $arguments);
181: }
182: }
183:
184: }
185:
186: ?>