# Source for file Event.php

Documentation is available at Event.php

1. <?php
2. /*
3. * $Id$
4. *
5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16. *
17. * This software consists of voluntary contributions made by many individuals
18. * and is licensed under the LGPL. For more information, see
19. * <http://www.phpdoctrine.com>.
20. */
21. /**
22. * Doctrine_Event
23. *
24. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
25. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
26. * @package Doctrine
27. * @category Object Relational Mapping
28. * @link www.phpdoctrine.com
29. * @since 1.0
30. * @version $Revision$
31. */
32. class Doctrine_Event
33. {
34. /**
35. * CONNECTION EVENT CODES
36. */
37. const CONN_QUERY = 1;
38. const CONN_EXEC = 2;
39. const CONN_PREPARE = 3;
40. const CONN_CONNECT = 4;
41. const CONN_CLOSE = 5;
42. const CONN_ERROR = 6;
44. const STMT_EXECUTE = 10;
45. const STMT_FETCH = 11;
46. const STMT_FETCHALL = 12;
48. const TX_BEGIN = 31;
49. const TX_COMMIT = 32;
50. const TX_ROLLBACK = 33;
51. const SAVEPOINT_CREATE = 34;
52. const SAVEPOINT_ROLLBACK = 35;
53. const SAVEPOINT_COMMIT = 36;
55. /*
56. * RECORD EVENT CODES
57. */
58. const RECORD_DELETE = 21;
59. const RECORD_SAVE = 22;
60. const RECORD_UPDATE = 23;
61. const RECORD_INSERT = 24;
62. const RECORD_SERIALIZE = 25;
63. const RECORD_UNSERIALIZE = 26;
64. /**
65. * @var mixed $_invoker the handler which invoked this event
66. */
67. protected $_invoker;
68. /**
69. * @var string $_query the sql query associated with this event (if any)
70. */
71. protected $_query;
72. /**
73. * @var string $_params the parameters associated with the query (if any)
74. */
75. protected $_params;
76. /**
77. * @see Doctrine_Event constants
78. * @var integer $_code the event code
79. */
80. protected $_code;
81. /**
82. * @var integer $_startedMicrotime the time point in which this event was started
83. */
84. protected $_startedMicrotime;
85. /**
86. * @var integer $_endedMicrotime the time point in which this event was ended
87. */
88. protected $_endedMicrotime;
89. /**
90. * @var array $_options an array of options
91. */
92. protected $_options = array();
93. /**
94. * constructor
95. *
96. * @param Doctrine_Connection|Doctrine_Connection_Statement|
97. Doctrine_Connection_UnitOfWork|Doctrine_Transaction $invoker the handler which invoked this event
98. * @param integer $code the event code
99. * @param string $query the sql query associated with this event (if any)
100. */
101. public function __construct($invoker, $code, $query = null, $params = array())
102. {
103. $this->_invoker = $invoker;
104. $this->_code = $code;
105. $this->_query = $query;
106. $this->_params = $params;
107. }
108. /**
109. * getQuery
110. *
111. * @return string returns the query associated with this event (if any)
112. */
113. public function getQuery()
114. {
115. return $this->_query;
116. }
117. /**
118. * getName
119. * returns the name of this event
120. *
121. * @return string the name of this event
122. */
123. public function getName()
124. {
125. switch ($this->_code) {
126. case self::CONN_QUERY:
127. return 'query';
128. case self::CONN_EXEC:
129. return 'exec';
130. case self::CONN_PREPARE:
131. return 'prepare';
132. case self::CONN_CONNECT:
133. return 'connect';
134. case self::CONN_CLOSE:
135. return 'close';
136. case self::CONN_ERROR:
137. return 'error';
139. case self::STMT_EXECUTE:
140. return 'execute';
141. case self::STMT_FETCH:
142. return 'fetch';
143. case self::STMT_FETCHALL:
144. return 'fetch all';
146. case self::TX_BEGIN:
147. return 'begin';
148. case self::TX_COMMIT:
149. return 'commit';
150. case self::TX_ROLLBACK:
151. return 'rollback';
153. case self::SAVEPOINT_CREATE:
154. return 'create savepoint';
155. case self::SAVEPOINT_ROLLBACK:
156. return 'rollback savepoint';
157. case self::SAVEPOINT_COMMIT:
158. return 'commit Ssavepoint';
160. case self::RECORD_DELETE:
161. return 'delete record';
162. case self::RECORD_SAVE:
163. return 'save record';
164. case self::RECORD_UPDATE:
165. return 'update record';
166. case self::RECORD_INSERT:
167. return 'insert record';
168. case self::RECORD_SERIALIZE:
169. return 'serialize record';
170. case self::RECORD_UNSERIALIZE:
171. return 'unserialize record';
172. }
173. }
174. /**
175. * getCode
176. *
177. * @return integer returns the code associated with this event
178. */
179. public function getCode()
180. {
181. return $this->_code;
182. }
183. /**
184. * getOption
185. * returns the value of an option
186. *
187. * @param string $option the name of the option
188. * @return mixed
189. */
190. public function __get($option)
191. {
192. if ( ! isset($this->_options[$option])) {
193. return null;
194. }
196. return $this->_options[$option];
197. }
198. /**
199. * skipOperation
200. * skips the next operation
201. * an alias for __set('skipOperation', true)
202. *
203. * @return Doctrine_Event this object
204. */
205. public function skipOperation()
206. {
207. $this->_options['skipOperation'] = true;
209. return $this;
210. }
211. /**
212. * setOption
213. * sets the value of an option
214. *
215. * @param string $option the name of the option
216. * @param mixed $value the value of the given option
217. * @return Doctrine_Event this object
218. */
219. public function __set($option, $value)
220. {
221. $this->_options[$option] = $value;
223. return $this;
224. }
225. /**
226. * start
227. * starts the internal timer of this event
228. *
229. * @return Doctrine_Event this object
230. */
231. public function start()
232. {
233. $this->_startedMicrotime = microtime(true);
234. }
235. /**
236. * hasEnded
237. * whether or not this event has ended
238. *
239. * @return boolean
240. */
241. public function hasEnded()
242. {
243. return ($this->_endedMicrotime != null);
244. }
245. /**
246. * end
247. * ends the internal timer of this event
248. *
249. * @return Doctrine_Event this object
250. */
251. public function end()
252. {
253. $this->_endedMicrotime = microtime(true);
255. return $this;
256. }
257. /**
258. * getInvoker
259. * returns the handler that invoked this event
260. *
261. * @return Doctrine_Connection|Doctrine_Connection_Statement|
262. * Doctrine_Connection_UnitOfWork|Doctrine_Transaction the handler that invoked this event
263. */
264. public function getInvoker()
265. {
266. return $this->_invoker;
267. }
268. /**
269. * getParams
270. * returns the parameters of the query
271. *
272. * @return array parameters of the query
273. */
274. public function getParams()
275. {
276. return $this->_params;
277. }
278. /**
279. * Get the elapsed time (in microseconds) that the event ran. If the event has
280. * not yet ended, return false.
281. *
282. * @return mixed
283. */
284. public function getElapsedSecs()
285. {
286. if (is_null($this->_endedMicrotime)) {
287. return false;
288. }
289. return ($this->_endedMicrotime - $this->_startedMicrotime);
290. }
291. }