Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 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.doctrine-project.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.doctrine-project.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
    const RECORD_DQL_DELETE  = 27;
68
    const RECORD_DQL_SELECT  = 28;
69
    const RECORD_DQL_UPDATE  = 29;
70
    const RECORD_VALIDATE    = 30;
71
 
72
    /**
73
     * @var mixed $_nextSequence        the sequence of the next event that will be created
74
     */
75
    static protected $_nextSequence = 0;
76
 
77
    /**
78
     * @var mixed $_sequence            the sequence of this event
79
     */
80
    protected $_sequence;
81
 
82
    /**
83
     * @var mixed $_invoker             the handler which invoked this event
84
     */
85
    protected $_invoker;
86
 
87
    /**
88
     * @var string $_query              the sql query associated with this event (if any)
89
     */
90
    protected $_query;
91
 
92
    /**
93
     * @var string $_params             the parameters associated with the query (if any)
94
     */
95
    protected $_params;
96
 
97
    /**
98
     * @see Doctrine_Event constants
99
     * @var integer $_code              the event code
100
     */
101
    protected $_code;
102
 
103
    /**
104
     * @var integer $_startedMicrotime  the time point in which this event was started
105
     */
106
    protected $_startedMicrotime;
107
 
108
    /**
109
     * @var integer $_endedMicrotime    the time point in which this event was ended
110
     */
111
    protected $_endedMicrotime;
112
 
113
    /**
114
     * @var array $_options             an array of options
115
     */
116
    protected $_options = array();
117
 
118
    /**
119
     * constructor
120
     *
121
     * @param Doctrine_Connection|Doctrine_Connection_Statement|
122
              Doctrine_Connection_UnitOfWork|Doctrine_Transaction $invoker   the handler which invoked this event
123
     * @param integer $code                                                  the event code
124
     * @param string $query                                                  the sql query associated with this event (if any)
125
     */
126
    public function __construct($invoker, $code, $query = null, $params = array())
127
    {
128
        $this->_sequence = self::$_nextSequence++;
129
        $this->_invoker  = $invoker;
130
        $this->_code     = $code;
131
        $this->_query    = $query;
132
        $this->_params   = $params;
133
    }
134
 
135
    /**
136
     * getQuery
137
     *
138
     * @return Doctrine_Query       returns the query associated with this event (if any)
139
     */
140
    public function getQuery()
141
    {
142
        return $this->_query;
143
    }
144
 
145
    /**
146
     * getName
147
     * returns the name of this event
148
     *
149
     * @return string       the name of this event
150
     */
151
    public function getName()
152
    {
153
        switch ($this->_code) {
154
            case self::CONN_QUERY:
155
                return 'query';
156
            case self::CONN_EXEC:
157
                return 'exec';
158
            case self::CONN_PREPARE:
159
                return 'prepare';
160
            case self::CONN_CONNECT:
161
                return 'connect';
162
            case self::CONN_CLOSE:
163
                return 'close';
164
            case self::CONN_ERROR:
165
                return 'error';
166
 
167
            case self::STMT_EXECUTE:
168
                return 'execute';
169
            case self::STMT_FETCH:
170
                return 'fetch';
171
            case self::STMT_FETCHALL:
172
                return 'fetch all';
173
 
174
            case self::TX_BEGIN:
175
                return 'begin';
176
            case self::TX_COMMIT:
177
                return 'commit';
178
            case self::TX_ROLLBACK:
179
                return 'rollback';
180
 
181
            case self::SAVEPOINT_CREATE:
182
                return 'create savepoint';
183
            case self::SAVEPOINT_ROLLBACK:
184
                return 'rollback savepoint';
185
            case self::SAVEPOINT_COMMIT:
186
                return 'commit savepoint';
187
 
188
            case self::RECORD_DELETE:
189
                return 'delete record';
190
            case self::RECORD_SAVE:
191
                return 'save record';
192
            case self::RECORD_UPDATE:
193
                return 'update record';
194
            case self::RECORD_INSERT:
195
                return 'insert record';
196
            case self::RECORD_SERIALIZE:
197
                return 'serialize record';
198
            case self::RECORD_UNSERIALIZE:
199
                return 'unserialize record';
200
            case self::RECORD_DQL_SELECT:
201
                return 'select records';
202
            case self::RECORD_DQL_DELETE:
203
                return 'delete records';
204
            case self::RECORD_DQL_UPDATE:
205
                return 'update records';
206
            case self::RECORD_VALIDATE:
207
                return 'validate record';
208
        }
209
    }
210
 
211
    /**
212
     * getCode
213
     *
214
     * @return integer      returns the code associated with this event
215
     */
216
    public function getCode()
217
    {
218
        return $this->_code;
219
    }
220
 
221
    /**
222
     * getOption
223
     * returns the value of an option
224
     *
225
     * @param string $option    the name of the option
226
     * @return mixed
227
     */
228
    public function __get($option)
229
    {
230
        if ( ! isset($this->_options[$option])) {
231
            return null;
232
        }
233
 
234
        return $this->_options[$option];
235
    }
236
 
237
    /**
238
     * skipOperation
239
     * skips the next operation
240
     * an alias for __set('skipOperation', true)
241
     *
242
     * @return Doctrine_Event   this object
243
     */
244
    public function skipOperation()
245
    {
246
        $this->_options['skipOperation'] = true;
247
 
248
        return $this;
249
    }
250
 
251
    /**
252
     * setOption
253
     * sets the value of an option
254
     *
255
     * @param string $option    the name of the option
256
     * @param mixed $value      the value of the given option
257
     * @return Doctrine_Event   this object
258
     */
259
    public function __set($option, $value)
260
    {
261
        $this->_options[$option] = $value;
262
 
263
        return $this;
264
    }
265
 
266
    /**
267
     * setOption
268
     * sets the value of an option by reference
269
     *
270
     * @param string $option    the name of the option
271
     * @param mixed $value      the value of the given option
272
     * @return Doctrine_Event   this object
273
     */
274
    public function set($option, &$value)
275
    {
276
        $this->_options[$option] =& $value;
277
 
278
        return $this;
279
    }
280
 
281
    /**
282
     * start
283
     * starts the internal timer of this event
284
     *
285
     * @return Doctrine_Event   this object
286
     */
287
    public function start()
288
    {
289
        $this->_startedMicrotime = microtime(true);
290
    }
291
 
292
    /**
293
     * hasEnded
294
     * whether or not this event has ended
295
     *
296
     * @return boolean
297
     */
298
    public function hasEnded()
299
    {
300
        return ($this->_endedMicrotime != null);
301
    }
302
 
303
    /**
304
     * end
305
     * ends the internal timer of this event
306
     *
307
     * @return Doctrine_Event   this object
308
     */
309
    public function end()
310
    {
311
        $this->_endedMicrotime = microtime(true);
312
 
313
        return $this;
314
    }
315
 
316
    /**
317
     * getSequence
318
     * returns the sequence of this event
319
     *
320
     * @return integer
321
     */
322
    public function getSequence()
323
    {
324
        return $this->_sequence;
325
    }
326
 
327
    /**
328
     * getInvoker
329
     * returns the handler that invoked this event
330
     *
331
     * @return Doctrine_Connection|Doctrine_Connection_Statement|
332
     *         Doctrine_Connection_UnitOfWork|Doctrine_Transaction   the handler that invoked this event
333
     */
334
    public function getInvoker()
335
    {
336
        return $this->_invoker;
337
    }
338
 
339
    /**
340
     * setInvoker
341
     * Defines new invoker (used in Hydrator)
342
     *
343
     * @param mixed $invoker
344
     * @return void
345
     */
346
    public function setInvoker($invoker)
347
    {
348
        $this->_invoker = $invoker;
349
    }
350
 
351
    /**
352
     * getParams
353
     * returns the parameters of the query
354
     *
355
     * @return array   parameters of the query
356
     */
357
    public function getParams()
358
    {
359
        return $this->_params;
360
    }
361
 
362
    /**
363
     * Get the elapsed time (in microseconds) that the event ran.  If the event has
364
     * not yet ended, return false.
365
     *
366
     * @return integer
367
     */
368
    public function getElapsedSecs()
369
    {
370
        if (is_null($this->_endedMicrotime)) {
371
            return false;
372
        }
373
        return ($this->_endedMicrotime - $this->_startedMicrotime);
374
    }
375
}