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