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