Event.php 8.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
<?php
/*
 *  $Id$
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information, see
 * <http://www.phpdoctrine.com>.
 */
/**
 * Doctrine_Event
 *
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @package     Doctrine
 * @category    Object Relational Mapping
 * @link        www.phpdoctrine.com
 * @since       1.0
 * @version     $Revision$
 */
class Doctrine_Event
{
    /**
35
     * CONNECTION EVENT CODES
36
     */
37 38 39 40
    const CONN_QUERY         = 1;
    const CONN_EXEC          = 2;
    const CONN_PREPARE       = 3;
    const CONN_CONNECT       = 4;
zYne's avatar
zYne committed
41
    const CONN_CLOSE         = 5;
zYne's avatar
zYne committed
42
    const CONN_ERROR         = 6;
43 44 45 46 47 48 49 50 51 52 53 54

    const STMT_EXECUTE       = 10;
    const STMT_FETCH         = 11;
    const STMT_FETCHALL      = 12;

    const TX_BEGIN           = 31;
    const TX_COMMIT          = 32;
    const TX_ROLLBACK        = 33;
    const SAVEPOINT_CREATE   = 34;
    const SAVEPOINT_ROLLBACK = 35;
    const SAVEPOINT_COMMIT   = 36;

55
    /*
56 57 58 59 60 61 62 63
     * RECORD EVENT CODES
     */
    const RECORD_DELETE      = 21;
    const RECORD_SAVE        = 22;
    const RECORD_UPDATE      = 23;
    const RECORD_INSERT      = 24;
    const RECORD_SERIALIZE   = 25;
    const RECORD_UNSERIALIZE = 26;
64
    /**
zYne's avatar
zYne committed
65
     * @var mixed $_invoker             the handler which invoked this event
66
     */
zYne's avatar
zYne committed
67
    protected $_invoker;
68
    /**
zYne's avatar
zYne committed
69
     * @var string $_query              the sql query associated with this event (if any)
70
     */
zYne's avatar
zYne committed
71
    protected $_query;
72
    /**
zYne's avatar
zYne committed
73
     * @var string $_params             the parameters associated with the query (if any)
74
     */
zYne's avatar
zYne committed
75
    protected $_params;
76
    /**
zYne's avatar
zYne committed
77
     * @see Doctrine_Event constants
zYne's avatar
zYne committed
78
     * @var integer $_code              the event code
79
     */
zYne's avatar
zYne committed
80
    protected $_code;
81
    /**
zYne's avatar
zYne committed
82
     * @var integer $_startedMicrotime  the time point in which this event was started
83
     */
zYne's avatar
zYne committed
84
    protected $_startedMicrotime;
85
    /**
zYne's avatar
zYne committed
86
     * @var integer $_endedMicrotime    the time point in which this event was ended
87
     */
zYne's avatar
zYne committed
88 89
    protected $_endedMicrotime;
    /**
zYne's avatar
zYne committed
90
     * @var array $_options             an array of options
zYne's avatar
zYne committed
91
     */
zYne's avatar
zYne committed
92
    protected $_options = array();
93 94 95
    /**
     * constructor
     *
96 97 98 99
     * @param Doctrine_Connection|Doctrine_Connection_Statement|
              Doctrine_Connection_UnitOfWork|Doctrine_Transaction $invoker   the handler which invoked this event
     * @param integer $code                                                  the event code
     * @param string $query                                                  the sql query associated with this event (if any)
100 101 102
     */
    public function __construct($invoker, $code, $query = null, $params = array())
    {
zYne's avatar
zYne committed
103 104 105 106
        $this->_invoker = $invoker;
        $this->_code    = $code;
        $this->_query   = $query;
        $this->_params  = $params;
107 108 109 110 111 112 113 114
    }
    /**
     * getQuery
     *
     * @return string       returns the query associated with this event (if any)
     */
    public function getQuery()
    {
zYne's avatar
zYne committed
115
        return $this->_query;
116 117 118 119 120 121 122 123 124
    }
    /**
     * getName
     * returns the name of this event
     *
     * @return string       the name of this event
     */
    public function getName() 
    {
zYne's avatar
zYne committed
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 133 134 135 136 137 138 139 140 141 142 143 144 145 146
            case self::CONN_CONNECT:
                return 'connect';
            case self::CONN_CLOSE:
                return 'close';
            case self::CONN_ERROR:
                return 'error';

            case self::STMT_EXECUTE:
                return 'execute';
            case self::STMT_FETCH:
                return 'fetch';
            case self::STMT_FETCHALL:
                return 'fetch all';
            
            case self::TX_BEGIN:
147
                return 'begin';
148
            case self::TX_COMMIT:
149
                return 'commit';
150
            case self::TX_ROLLBACK:
151
                return 'rollback';
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171

            case self::SAVEPOINT_CREATE:
                return 'create savepoint';
            case self::SAVEPOINT_ROLLBACK:
                return 'rollback savepoint';
            case self::SAVEPOINT_COMMIT:
                return 'commit Ssavepoint';
 
            case self::RECORD_DELETE:
                return 'delete record';
            case self::RECORD_SAVE:
                return 'save record';
            case self::RECORD_UPDATE:
                return 'update record';
            case self::RECORD_INSERT:
                return 'insert record';
            case self::RECORD_SERIALIZE:
                return 'serialize record';
            case self::RECORD_UNSERIALIZE:
                return 'unserialize record';
172 173 174 175 176 177 178 179 180
        }
    }
    /**
     * getCode
     *
     * @return integer      returns the code associated with this event
     */
    public function getCode()
    {
zYne's avatar
zYne committed
181
        return $this->_code;
182
    }
zYne's avatar
zYne committed
183
    /**
zYne's avatar
zYne committed
184 185
     * getOption
     * returns the value of an option
zYne's avatar
zYne committed
186
     *
zYne's avatar
zYne committed
187 188 189
     * @param string $option    the name of the option
     * @return mixed
     */
190
    public function __get($option)
zYne's avatar
zYne committed
191 192 193 194 195 196 197 198 199 200
    {
        if ( ! isset($this->_options[$option])) {
            return null;
        }
        
        return $this->_options[$option];
    }
    /**
     * skipOperation
     * skips the next operation
201
     * an alias for __set('skipOperation', true)
zYne's avatar
zYne committed
202 203
     *
     * @return Doctrine_Event   this object
zYne's avatar
zYne committed
204
     */
zYne's avatar
zYne committed
205
    public function skipOperation()
zYne's avatar
zYne committed
206
    {
207 208 209
        $this->_options['skipOperation'] = true;
    
        return $this;
zYne's avatar
zYne committed
210 211
    }
    /**
zYne's avatar
zYne committed
212 213
     * setOption
     * sets the value of an option
zYne's avatar
zYne committed
214
     *
zYne's avatar
zYne committed
215 216 217
     * @param string $option    the name of the option
     * @param mixed $value      the value of the given option
     * @return Doctrine_Event   this object
zYne's avatar
zYne committed
218
     */
219
    public function __set($option, $value)
zYne's avatar
zYne committed
220
    {
zYne's avatar
zYne committed
221 222 223
        $this->_options[$option] = $value;

        return $this;
zYne's avatar
zYne committed
224
    }
225 226 227 228
    /**
     * start
     * starts the internal timer of this event
     *
zYne's avatar
zYne committed
229
     * @return Doctrine_Event   this object
230 231 232
     */
    public function start()
    {
zYne's avatar
zYne committed
233
        $this->_startedMicrotime = microtime(true);
234 235 236 237 238 239 240 241 242
    }
    /**
     * hasEnded
     * whether or not this event has ended
     *
     * @return boolean
     */
    public function hasEnded()
    {
zYne's avatar
zYne committed
243
        return ($this->_endedMicrotime != null);
244 245 246 247 248
    }
    /**
     * end
     * ends the internal timer of this event
     *
zYne's avatar
zYne committed
249
     * @return Doctrine_Event   this object
250 251 252
     */
    public function end()
    {
zYne's avatar
zYne committed
253
        $this->_endedMicrotime = microtime(true);
zYne's avatar
zYne committed
254 255
        
        return $this;
256 257 258 259 260
    }
    /**
     * getInvoker
     * returns the handler that invoked this event
     *
261 262
     * @return Doctrine_Connection|Doctrine_Connection_Statement|
     *         Doctrine_Connection_UnitOfWork|Doctrine_Transaction   the handler that invoked this event
263 264 265
     */
    public function getInvoker()
    {
zYne's avatar
zYne committed
266
        return $this->_invoker;
267 268 269 270 271 272 273 274 275
    }
    /**
     * getParams
     * returns the parameters of the query
     *
     * @return array   parameters of the query
     */
    public function getParams()
    {
zYne's avatar
zYne committed
276
        return $this->_params;
277 278 279 280 281 282 283 284 285
    }
    /**
     * Get the elapsed time (in microseconds) that the event ran.  If the event has
     * not yet ended, return false.
     *
     * @return mixed
     */
    public function getElapsedSecs()
    {
zYne's avatar
zYne committed
286
        if (is_null($this->_endedMicrotime)) {
287 288
            return false;
        }
zYne's avatar
zYne committed
289
        return ($this->_endedMicrotime - $this->_startedMicrotime);
290 291
    }
}