Event.php 8.9 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
<?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
27
 * @subpackage  Event
28 29 30 31 32 33 34
 * @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
    const HYDRATE            = 40;

57
    /*
58 59 60 61 62 63 64 65
     * 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;
66
    /**
zYne's avatar
zYne committed
67
     * @var mixed $_invoker             the handler which invoked this event
68
     */
zYne's avatar
zYne committed
69
    protected $_invoker;
70
    /**
zYne's avatar
zYne committed
71
     * @var string $_query              the sql query associated with this event (if any)
72
     */
zYne's avatar
zYne committed
73
    protected $_query;
74
    /**
zYne's avatar
zYne committed
75
     * @var string $_params             the parameters associated with the query (if any)
76
     */
zYne's avatar
zYne committed
77
    protected $_params;
78
    /**
zYne's avatar
zYne committed
79
     * @see Doctrine_Event constants
zYne's avatar
zYne committed
80
     * @var integer $_code              the event code
81
     */
zYne's avatar
zYne committed
82
    protected $_code;
83
    /**
zYne's avatar
zYne committed
84
     * @var integer $_startedMicrotime  the time point in which this event was started
85
     */
zYne's avatar
zYne committed
86
    protected $_startedMicrotime;
87
    /**
zYne's avatar
zYne committed
88
     * @var integer $_endedMicrotime    the time point in which this event was ended
89
     */
zYne's avatar
zYne committed
90 91
    protected $_endedMicrotime;
    /**
zYne's avatar
zYne committed
92
     * @var array $_options             an array of options
zYne's avatar
zYne committed
93
     */
zYne's avatar
zYne committed
94
    protected $_options = array();
95 96 97
    /**
     * constructor
     *
98 99 100 101
     * @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)
102 103 104
     */
    public function __construct($invoker, $code, $query = null, $params = array())
    {
zYne's avatar
zYne committed
105 106 107 108
        $this->_invoker = $invoker;
        $this->_code    = $code;
        $this->_query   = $query;
        $this->_params  = $params;
109 110 111 112 113 114 115 116
    }
    /**
     * getQuery
     *
     * @return string       returns the query associated with this event (if any)
     */
    public function getQuery()
    {
zYne's avatar
zYne committed
117
        return $this->_query;
118 119 120 121 122 123 124 125 126
    }
    /**
     * getName
     * returns the name of this event
     *
     * @return string       the name of this event
     */
    public function getName() 
    {
zYne's avatar
zYne committed
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 135 136 137 138 139 140 141 142 143 144 145 146 147 148
            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:
149
                return 'begin';
150
            case self::TX_COMMIT:
151
                return 'commit';
152
            case self::TX_ROLLBACK:
153
                return 'rollback';
154 155 156 157 158 159

            case self::SAVEPOINT_CREATE:
                return 'create savepoint';
            case self::SAVEPOINT_ROLLBACK:
                return 'rollback savepoint';
            case self::SAVEPOINT_COMMIT:
160
                return 'commit savepoint';
161 162 163 164 165 166 167 168 169 170 171 172 173
 
            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';
174 175 176 177 178 179 180 181 182
        }
    }
    /**
     * getCode
     *
     * @return integer      returns the code associated with this event
     */
    public function getCode()
    {
zYne's avatar
zYne committed
183
        return $this->_code;
184
    }
zYne's avatar
zYne committed
185
    /**
zYne's avatar
zYne committed
186 187
     * getOption
     * returns the value of an option
zYne's avatar
zYne committed
188
     *
zYne's avatar
zYne committed
189 190 191
     * @param string $option    the name of the option
     * @return mixed
     */
192
    public function __get($option)
zYne's avatar
zYne committed
193 194 195 196 197 198 199 200 201 202
    {
        if ( ! isset($this->_options[$option])) {
            return null;
        }
        
        return $this->_options[$option];
    }
    /**
     * skipOperation
     * skips the next operation
203
     * an alias for __set('skipOperation', true)
zYne's avatar
zYne committed
204 205
     *
     * @return Doctrine_Event   this object
zYne's avatar
zYne committed
206
     */
zYne's avatar
zYne committed
207
    public function skipOperation()
zYne's avatar
zYne committed
208
    {
209 210 211
        $this->_options['skipOperation'] = true;
    
        return $this;
zYne's avatar
zYne committed
212 213
    }
    /**
zYne's avatar
zYne committed
214 215
     * setOption
     * sets the value of an option
zYne's avatar
zYne committed
216
     *
zYne's avatar
zYne committed
217 218 219
     * @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
220
     */
221
    public function __set($option, $value)
zYne's avatar
zYne committed
222
    {
zYne's avatar
zYne committed
223 224 225
        $this->_options[$option] = $value;

        return $this;
zYne's avatar
zYne committed
226
    }
227 228 229 230 231 232 233 234 235 236 237 238 239 240
    /**
     * setOption
     * sets the value of an option by reference
     *
     * @param string $option    the name of the option
     * @param mixed $value      the value of the given option
     * @return Doctrine_Event   this object
     */
    public function set($option, &$value)
    {
        $this->_options[$option] =& $value;

        return $this;
    }
241 242 243 244
    /**
     * start
     * starts the internal timer of this event
     *
zYne's avatar
zYne committed
245
     * @return Doctrine_Event   this object
246 247 248
     */
    public function start()
    {
zYne's avatar
zYne committed
249
        $this->_startedMicrotime = microtime(true);
250 251 252 253 254 255 256 257 258
    }
    /**
     * hasEnded
     * whether or not this event has ended
     *
     * @return boolean
     */
    public function hasEnded()
    {
zYne's avatar
zYne committed
259
        return ($this->_endedMicrotime != null);
260 261 262 263 264
    }
    /**
     * end
     * ends the internal timer of this event
     *
zYne's avatar
zYne committed
265
     * @return Doctrine_Event   this object
266 267 268
     */
    public function end()
    {
zYne's avatar
zYne committed
269
        $this->_endedMicrotime = microtime(true);
zYne's avatar
zYne committed
270 271
        
        return $this;
272 273 274 275 276
    }
    /**
     * getInvoker
     * returns the handler that invoked this event
     *
277 278
     * @return Doctrine_Connection|Doctrine_Connection_Statement|
     *         Doctrine_Connection_UnitOfWork|Doctrine_Transaction   the handler that invoked this event
279 280 281
     */
    public function getInvoker()
    {
zYne's avatar
zYne committed
282
        return $this->_invoker;
283 284 285 286 287 288 289 290 291
    }
    /**
     * getParams
     * returns the parameters of the query
     *
     * @return array   parameters of the query
     */
    public function getParams()
    {
zYne's avatar
zYne committed
292
        return $this->_params;
293 294 295 296 297 298 299 300 301
    }
    /**
     * 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
302
        if (is_null($this->_endedMicrotime)) {
303 304
            return false;
        }
zYne's avatar
zYne committed
305
        return ($this->_endedMicrotime - $this->_startedMicrotime);
306 307
    }
}