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.org>.
|
20 |
*/
|
21 |
|
22 |
/**
|
23 |
* Doctrine_Event
|
24 |
*
|
25 |
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
26 |
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
27 |
* @package Doctrine
|
28 |
* @subpackage Event
|
29 |
* @link www.phpdoctrine.org
|
30 |
* @since 1.0
|
31 |
* @version $Revision$
|
32 |
*/
|
33 |
class Doctrine_Event
|
34 |
{
|
35 |
/**
|
36 |
* CONNECTION EVENT CODES
|
37 |
*/
|
38 |
const CONN_QUERY = 1;
|
39 |
const CONN_EXEC = 2;
|
40 |
const CONN_PREPARE = 3;
|
41 |
const CONN_CONNECT = 4;
|
42 |
const CONN_CLOSE = 5;
|
43 |
const CONN_ERROR = 6;
|
44 |
|
45 |
const STMT_EXECUTE = 10;
|
46 |
const STMT_FETCH = 11;
|
47 |
const STMT_FETCHALL = 12;
|
48 |
|
49 |
const TX_BEGIN = 31;
|
50 |
const TX_COMMIT = 32;
|
51 |
const TX_ROLLBACK = 33;
|
52 |
const SAVEPOINT_CREATE = 34;
|
53 |
const SAVEPOINT_ROLLBACK = 35;
|
54 |
const SAVEPOINT_COMMIT = 36;
|
55 |
|
56 |
const HYDRATE = 40;
|
57 |
|
58 |
/*
|
59 |
* RECORD EVENT CODES
|
60 |
*/
|
61 |
const RECORD_DELETE = 21;
|
62 |
const RECORD_SAVE = 22;
|
63 |
const RECORD_UPDATE = 23;
|
64 |
const RECORD_INSERT = 24;
|
65 |
const RECORD_SERIALIZE = 25;
|
66 |
const RECORD_UNSERIALIZE = 26;
|
67 |
|
68 |
/**
|
69 |
* @var mixed $_invoker the handler which invoked this event
|
70 |
*/
|
71 |
protected $_invoker;
|
72 |
|
73 |
/**
|
74 |
* @var string $_query the sql query associated with this event (if any)
|
75 |
*/
|
76 |
protected $_query;
|
77 |
|
78 |
/**
|
79 |
* @var string $_params the parameters associated with the query (if any)
|
80 |
*/
|
81 |
protected $_params;
|
82 |
|
83 |
/**
|
84 |
* @see Doctrine_Event constants
|
85 |
* @var integer $_code the event code
|
86 |
*/
|
87 |
protected $_code;
|
88 |
|
89 |
/**
|
90 |
* @var integer $_startedMicrotime the time point in which this event was started
|
91 |
*/
|
92 |
protected $_startedMicrotime;
|
93 |
|
94 |
/**
|
95 |
* @var integer $_endedMicrotime the time point in which this event was ended
|
96 |
*/
|
97 |
protected $_endedMicrotime;
|
98 |
|
99 |
/**
|
100 |
* @var array $_options an array of options
|
101 |
*/
|
102 |
protected $_options = array();
|
103 |
|
104 |
/**
|
105 |
* constructor
|
106 |
*
|
107 |
* @param Doctrine_Connection|Doctrine_Connection_Statement|
|
108 |
Doctrine_Connection_UnitOfWork|Doctrine_Transaction $invoker the handler which invoked this event
|
109 |
* @param integer $code the event code
|
110 |
* @param string $query the sql query associated with this event (if any)
|
111 |
*/
|
112 |
public function __construct($invoker, $code, $query = null, $params = array())
|
113 |
{
|
114 |
$this->_invoker = $invoker;
|
115 |
$this->_code = $code;
|
116 |
$this->_query = $query;
|
117 |
$this->_params = $params;
|
118 |
}
|
119 |
|
120 |
/**
|
121 |
* getQuery
|
122 |
*
|
123 |
* @return string returns the query associated with this event (if any)
|
124 |
*/
|
125 |
public function getQuery()
|
126 |
{
|
127 |
return $this->_query;
|
128 |
}
|
129 |
|
130 |
/**
|
131 |
* getName
|
132 |
* returns the name of this event
|
133 |
*
|
134 |
* @return string the name of this event
|
135 |
*/
|
136 |
public function getName()
|
137 |
{
|
138 |
switch ($this->_code) {
|
139 |
case self::CONN_QUERY:
|
140 |
return 'query';
|
141 |
case self::CONN_EXEC:
|
142 |
return 'exec';
|
143 |
case self::CONN_PREPARE:
|
144 |
return 'prepare';
|
145 |
case self::CONN_CONNECT:
|
146 |
return 'connect';
|
147 |
case self::CONN_CLOSE:
|
148 |
return 'close';
|
149 |
case self::CONN_ERROR:
|
150 |
return 'error';
|
151 |
|
152 |
case self::STMT_EXECUTE:
|
153 |
return 'execute';
|
154 |
case self::STMT_FETCH:
|
155 |
return 'fetch';
|
156 |
case self::STMT_FETCHALL:
|
157 |
return 'fetch all';
|
158 |
|
159 |
case self::TX_BEGIN:
|
160 |
return 'begin';
|
161 |
case self::TX_COMMIT:
|
162 |
return 'commit';
|
163 |
case self::TX_ROLLBACK:
|
164 |
return 'rollback';
|
165 |
|
166 |
case self::SAVEPOINT_CREATE:
|
167 |
return 'create savepoint';
|
168 |
case self::SAVEPOINT_ROLLBACK:
|
169 |
return 'rollback savepoint';
|
170 |
case self::SAVEPOINT_COMMIT:
|
171 |
return 'commit savepoint';
|
172 |
|
173 |
case self::RECORD_DELETE:
|
174 |
return 'delete record';
|
175 |
case self::RECORD_SAVE:
|
176 |
return 'save record';
|
177 |
case self::RECORD_UPDATE:
|
178 |
return 'update record';
|
179 |
case self::RECORD_INSERT:
|
180 |
return 'insert record';
|
181 |
case self::RECORD_SERIALIZE:
|
182 |
return 'serialize record';
|
183 |
case self::RECORD_UNSERIALIZE:
|
184 |
return 'unserialize record';
|
185 |
}
|
186 |
}
|
187 |
|
188 |
/**
|
189 |
* getCode
|
190 |
*
|
191 |
* @return integer returns the code associated with this event
|
192 |
*/
|
193 |
public function getCode()
|
194 |
{
|
195 |
return $this->_code;
|
196 |
}
|
197 |
|
198 |
/**
|
199 |
* getOption
|
200 |
* returns the value of an option
|
201 |
*
|
202 |
* @param string $option the name of the option
|
203 |
* @return mixed
|
204 |
*/
|
205 |
public function __get($option)
|
206 |
{
|
207 |
if ( ! isset($this->_options[$option])) {
|
208 |
return null;
|
209 |
}
|
210 |
|
211 |
return $this->_options[$option];
|
212 |
}
|
213 |
|
214 |
/**
|
215 |
* skipOperation
|
216 |
* skips the next operation
|
217 |
* an alias for __set('skipOperation', true)
|
218 |
*
|
219 |
* @return Doctrine_Event this object
|
220 |
*/
|
221 |
public function skipOperation()
|
222 |
{
|
223 |
$this->_options['skipOperation'] = true;
|
224 |
|
225 |
return $this;
|
226 |
}
|
227 |
|
228 |
/**
|
229 |
* setOption
|
230 |
* sets the value of an option
|
231 |
*
|
232 |
* @param string $option the name of the option
|
233 |
* @param mixed $value the value of the given option
|
234 |
* @return Doctrine_Event this object
|
235 |
*/
|
236 |
public function __set($option, $value)
|
237 |
{
|
238 |
$this->_options[$option] = $value;
|
239 |
|
240 |
return $this;
|
241 |
}
|
242 |
|
243 |
/**
|
244 |
* setOption
|
245 |
* sets the value of an option by reference
|
246 |
*
|
247 |
* @param string $option the name of the option
|
248 |
* @param mixed $value the value of the given option
|
249 |
* @return Doctrine_Event this object
|
250 |
*/
|
251 |
public function set($option, &$value)
|
252 |
{
|
253 |
$this->_options[$option] =& $value;
|
254 |
|
255 |
return $this;
|
256 |
}
|
257 |
|
258 |
/**
|
259 |
* start
|
260 |
* starts the internal timer of this event
|
261 |
*
|
262 |
* @return Doctrine_Event this object
|
263 |
*/
|
264 |
public function start()
|
265 |
{
|
266 |
$this->_startedMicrotime = microtime(true);
|
267 |
}
|
268 |
|
269 |
/**
|
270 |
* hasEnded
|
271 |
* whether or not this event has ended
|
272 |
*
|
273 |
* @return boolean
|
274 |
*/
|
275 |
public function hasEnded()
|
276 |
{
|
277 |
return ($this->_endedMicrotime != null);
|
278 |
}
|
279 |
|
280 |
/**
|
281 |
* end
|
282 |
* ends the internal timer of this event
|
283 |
*
|
284 |
* @return Doctrine_Event this object
|
285 |
*/
|
286 |
public function end()
|
287 |
{
|
288 |
$this->_endedMicrotime = microtime(true);
|
289 |
|
290 |
return $this;
|
291 |
}
|
292 |
|
293 |
/**
|
294 |
* getInvoker
|
295 |
* returns the handler that invoked this event
|
296 |
*
|
297 |
* @return Doctrine_Connection|Doctrine_Connection_Statement|
|
298 |
* Doctrine_Connection_UnitOfWork|Doctrine_Transaction the handler that invoked this event
|
299 |
*/
|
300 |
public function getInvoker()
|
301 |
{
|
302 |
return $this->_invoker;
|
303 |
}
|
304 |
|
305 |
/**
|
306 |
* getParams
|
307 |
* returns the parameters of the query
|
308 |
*
|
309 |
* @return array parameters of the query
|
310 |
*/
|
311 |
public function getParams()
|
312 |
{
|
313 |
return $this->_params;
|
314 |
}
|
315 |
|
316 |
/**
|
317 |
* Get the elapsed time (in microseconds) that the event ran. If the event has
|
318 |
* not yet ended, return false.
|
319 |
*
|
320 |
* @return mixed
|
321 |
*/
|
322 |
public function getElapsedSecs()
|
323 |
{
|
324 |
if (is_null($this->_endedMicrotime)) {
|
325 |
return false;
|
326 |
}
|
327 |
return ($this->_endedMicrotime - $this->_startedMicrotime);
|
328 |
}
|
329 |
}
|