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: /**
28: * Callbacks are executed before a item is created.
29: * Expected parameters for callback: none
30: *
31: * @var int
32: */
33: const CREATE_BEFORE = 10;
34:
35: /**
36: * Callbacks are executed if item could not be created.
37: * Expected parameters for callback: none
38: *
39: * @var int
40: */
41: const CREATE_FAILURE = 11;
42:
43: /**
44: * Callbacks are executed if item could be created successfully.
45: * Expected parameters for callback: ID of created item
46: *
47: * @var int
48: */
49: const CREATE_SUCCESS = 12;
50:
51: /**
52: * Callbacks are executed before store process is executed.
53: * Expected parameters for callback: Item instance
54: *
55: * @var int
56: */
57: const STORE_BEFORE = 20;
58:
59: /**
60: * Callbacks are executed if store process failed.
61: * This is also likely to happen if query would not change anything in
62: * database!
63: * Expected parameters for callback: Item instance
64: *
65: * @var int
66: */
67: const STORE_FAILURE = 21;
68:
69: /**
70: * Callbacks are executed if store process saved the values in the database.
71: * Expected parameters for callback: Item instance
72: *
73: * @var int
74: */
75: const STORE_SUCCESS = 22;
76:
77: /**
78: * Callbacks are executed before deleting an item.
79: * Expected parameters for callback: ID of them item to delete
80: *
81: * @var int
82: */
83: const DELETE_BEFORE = 30;
84:
85: /**
86: * Callbacks are executed if deletion of an item fails.
87: * Expected parameters for callback: ID of them item to delete
88: *
89: * @var int
90: */
91: const DELETE_FAILURE = 31;
92:
93: /**
94: * Callbacks are executed if item was deleted successfully.
95: * Expected parameters for callback: ID of them item to delete
96: *
97: * @var int
98: */
99: const DELETE_SUCCESS = 32;
100:
101: /**
102: * Callback stack.
103: *
104: * @var array
105: */
106: private static $_callbacks = array();
107:
108: /**
109: * Registers a new callback.
110: *
111: * Example:
112: * cGenericDb::register(cGenericDb::CREATE_SUCCESS, 'itemCreateHandler',
113: * 'cApiArticle');
114: * cGenericDb::register(cGenericDb::CREATE_SUCCESS,
115: * array('cCallbackHandler', 'executeCreateHandle'), 'cApiArticle');
116: *
117: * @param string $event Callback event, must be a valid value of a
118: * cGenericDb event constant
119: * @param mixed $callback Callback to register
120: * @param mixed $class Class name for registering callback (can be string of
121: * array with names of the concrete Item classes)
122: * @throws cInvalidArgumentException if event or class are not set or the
123: * callback is not callable
124: * @return void
125: */
126: public static function register($event, $callback, $class) {
127: if (isset($event) === false) {
128: throw new cInvalidArgumentException("No callback event for execution was given");
129: }
130:
131: if (is_callable($callback) === false) {
132: throw new cInvalidArgumentException("Given callback is not callable.");
133: }
134:
135: if (isset($class) === false) {
136: throw new cInvalidArgumentException("No class for registering callback was given.");
137: }
138:
139: if (is_array($class)) {
140: foreach ($class as $className) {
141: self::$_callbacks[$className][$event][] = $callback;
142: }
143: } else {
144: self::$_callbacks[$class][$event][] = $callback;
145: }
146: }
147:
148: /**
149: * Unregisters all callbacks for a specific event in a class.
150: *
151: * Example:
152: * cGenericDb::unregister(cGenericDb::CREATE_SUCCESS, 'cApiArticle');
153: *
154: * @param string $event Callback event, must be a valid value of a
155: * cGenericDb event constant
156: * @param mixed $class Class name for unregistering callback (can be string
157: * of array with names of the concrete Item classes)
158: * @throws cInvalidArgumentException if the event or the class are not set
159: * @return void
160: */
161: public static function unregister($event, $class) {
162: if (isset($event) === false) {
163: throw new cInvalidArgumentException("No callback event for execution was given");
164: }
165:
166: if (isset($class) === false) {
167: throw new cInvalidArgumentException("No class for unregistering callbacks was given.");
168: }
169:
170: if (is_array($class)) {
171: foreach ($class as $className) {
172: unset(self::$_callbacks[$className][$event]);
173: }
174: } else {
175: unset(self::$_callbacks[$class][$event]);
176: }
177: }
178:
179: /**
180: * Executes all callbacks for a specific event in a class.
181: *
182: * @param string $event Callback event, must be a valid value of a
183: * cGenericDb event constant
184: * @param string $class Class name for executing callback
185: * @param array $arguments Arguments to pass to the callback function
186: * @throws cInvalidArgumentException if the event or class is not set
187: * @return void
188: */
189: protected final function _executeCallbacks($event, $class, $arguments = array()) {
190: if (isset($event) === false) {
191: throw new cInvalidArgumentException("No callback event for execution was given");
192: }
193:
194: if (isset($class) === false) {
195: throw new cInvalidArgumentException("No class for executing callbacks was given.");
196: }
197:
198: if (!isset(self::$_callbacks[$class])) {
199: return;
200: }
201:
202: if (!isset(self::$_callbacks[$class][$event])) {
203: return;
204: }
205:
206: foreach (self::$_callbacks[$class][$event] as $callback) {
207: call_user_func_array($callback, $arguments);
208: }
209: }
210: }
211:
212: ?>