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: */
125: public static function register($event, $callback, $class) {
126: if (isset($event) === false) {
127: throw new cInvalidArgumentException("No callback event for execution was given");
128: }
129:
130: if (is_callable($callback) === false) {
131: throw new cInvalidArgumentException("Given callback is not callable.");
132: }
133:
134: if (isset($class) === false) {
135: throw new cInvalidArgumentException("No class for registering callback was given.");
136: }
137:
138: if (is_array($class)) {
139: foreach ($class as $className) {
140: self::$_callbacks[$className][$event][] = $callback;
141: }
142: } else {
143: self::$_callbacks[$class][$event][] = $callback;
144: }
145: }
146:
147: /**
148: * Unregisters all callbacks for a specific event in a class.
149: *
150: * Example:
151: * cGenericDb::unregister(cGenericDb::CREATE_SUCCESS, 'cApiArticle');
152: *
153: * @param string $event Callback event, must be a valid value of a
154: * cGenericDb event constant
155: * @param mixed $class Class name for unregistering callback (can be string
156: * of array with names of the concrete Item classes)
157: * @throws cInvalidArgumentException if the event or the class are not set
158: */
159: public static function unregister($event, $class) {
160: if (isset($event) === false) {
161: throw new cInvalidArgumentException("No callback event for execution was given");
162: }
163:
164: if (isset($class) === false) {
165: throw new cInvalidArgumentException("No class for unregistering callbacks was given.");
166: }
167:
168: if (is_array($class)) {
169: foreach ($class as $className) {
170: unset(self::$_callbacks[$className][$event]);
171: }
172: } else {
173: unset(self::$_callbacks[$class][$event]);
174: }
175: }
176:
177: /**
178: * Executes all callbacks for a specific event in a class.
179: *
180: * @param string $event Callback event, must be a valid value of a
181: * cGenericDb event constant
182: * @param string $class Class name for executing callback
183: * @param array $arguments Arguments to pass to the callback function
184: * @throws cInvalidArgumentException if the event or class is not set
185: */
186: protected final function _executeCallbacks($event, $class, $arguments = array()) {
187: if (isset($event) === false) {
188: throw new cInvalidArgumentException("No callback event for execution was given");
189: }
190:
191: if (isset($class) === false) {
192: throw new cInvalidArgumentException("No class for executing callbacks was given.");
193: }
194:
195: if (!isset(self::$_callbacks[$class])) {
196: return;
197: }
198:
199: if (!isset(self::$_callbacks[$class][$event])) {
200: return;
201: }
202:
203: foreach (self::$_callbacks[$class][$event] as $callback) {
204: call_user_func_array($callback, $arguments);
205: }
206: }
207: }
208:
209: ?>