~patrix-sbs/oraculum/git

« back to all changes in this revision

Viewing changes to models/doctrine/lib/Doctrine/Event.php

  • Committer: Patrick Kaminski
  • Date: 2009-09-02 02:33:07 UTC
  • Revision ID: git-v1:943803254fca67bfb4c0374422b1b836b14dc518
Tags: v0.1a
Sending Oraculum Framework v0.1 alpha

Show diffs side-by-side

added added

removed removed

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