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