Event.php 7.37 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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
    const CONN_QUERY         = 1;
    const CONN_EXEC          = 2;
    const CONN_PREPARE       = 3;
    const CONN_CONNECT       = 4;

    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;

    /**
     * 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;
62
    /**
zYne's avatar
zYne committed
63
     * @var mixed $_invoker             the handler which invoked this event
64
     */
zYne's avatar
zYne committed
65
    protected $_invoker;
66
    /**
zYne's avatar
zYne committed
67
     * @var string $_query              the sql query associated with this event (if any)
68
     */
zYne's avatar
zYne committed
69
    protected $_query;
70
    /**
zYne's avatar
zYne committed
71
     * @var string $_params             the parameters associated with the query (if any)
72
     */
zYne's avatar
zYne committed
73
    protected $_params;
74
    /**
zYne's avatar
zYne committed
75
     * @see Doctrine_Event constants
zYne's avatar
zYne committed
76
     * @var integer $_code              the event code
77
     */
zYne's avatar
zYne committed
78
    protected $_code;
79
    /**
zYne's avatar
zYne committed
80
     * @var integer $_startedMicrotime  the time point in which this event was started
81
     */
zYne's avatar
zYne committed
82
    protected $_startedMicrotime;
83
    /**
zYne's avatar
zYne committed
84
     * @var integer $_endedMicrotime    the time point in which this event was ended
85
     */
zYne's avatar
zYne committed
86 87
    protected $_endedMicrotime;
    /**
zYne's avatar
zYne committed
88
     * @var array $_options             an array of options
zYne's avatar
zYne committed
89
     */
zYne's avatar
zYne committed
90
    protected $_options = array();
91 92 93
    /**
     * constructor
     *
94 95 96 97
     * @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)
98 99 100
     */
    public function __construct($invoker, $code, $query = null, $params = array())
    {
zYne's avatar
zYne committed
101 102 103 104
        $this->_invoker = $invoker;
        $this->_code    = $code;
        $this->_query   = $query;
        $this->_params  = $params;
105 106 107 108 109 110 111 112
    }
    /**
     * getQuery
     *
     * @return string       returns the query associated with this event (if any)
     */
    public function getQuery()
    {
zYne's avatar
zYne committed
113
        return $this->_query;
114 115 116 117 118 119 120 121 122
    }
    /**
     * getName
     * returns the name of this event
     *
     * @return string       the name of this event
     */
    public function getName() 
    {
zYne's avatar
zYne committed
123
        switch ($this->_code) {
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
            case self::QUERY:
                return 'query';
            case self::EXEC:
                return 'exec';
            case self::EXECUTE:
                return 'execute';
            case self::PREPARE:
                return 'prepare';
            case self::BEGIN:
                return 'begin';
            case self::COMMIT:
                return 'commit';
            case self::ROLLBACK:
                return 'rollback';
            case self::CONNECT:
                return 'connect';
        }
    }
    /**
     * getCode
     *
     * @return integer      returns the code associated with this event
     */
    public function getCode()
    {
zYne's avatar
zYne committed
149
        return $this->_code;
150
    }
zYne's avatar
zYne committed
151
    /**
zYne's avatar
zYne committed
152 153
     * getOption
     * returns the value of an option
zYne's avatar
zYne committed
154
     *
zYne's avatar
zYne committed
155 156 157
     * @param string $option    the name of the option
     * @return mixed
     */
158
    public function __get($option)
zYne's avatar
zYne committed
159 160 161 162 163 164 165 166 167 168
    {
        if ( ! isset($this->_options[$option])) {
            return null;
        }
        
        return $this->_options[$option];
    }
    /**
     * skipOperation
     * skips the next operation
169
     * an alias for __set('skipOperation', true)
zYne's avatar
zYne committed
170 171
     *
     * @return Doctrine_Event   this object
zYne's avatar
zYne committed
172
     */
zYne's avatar
zYne committed
173
    public function skipOperation()
zYne's avatar
zYne committed
174
    {
175 176 177
        $this->_options['skipOperation'] = true;
    
        return $this;
zYne's avatar
zYne committed
178 179
    }
    /**
zYne's avatar
zYne committed
180 181
     * setOption
     * sets the value of an option
zYne's avatar
zYne committed
182
     *
zYne's avatar
zYne committed
183 184 185
     * @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
186
     */
187
    public function __set($option, $value)
zYne's avatar
zYne committed
188
    {
zYne's avatar
zYne committed
189 190 191
        $this->_options[$option] = $value;

        return $this;
zYne's avatar
zYne committed
192
    }
193 194 195 196
    /**
     * start
     * starts the internal timer of this event
     *
zYne's avatar
zYne committed
197
     * @return Doctrine_Event   this object
198 199 200
     */
    public function start()
    {
zYne's avatar
zYne committed
201
        $this->_startedMicrotime = microtime(true);
202 203 204 205 206 207 208 209 210
    }
    /**
     * hasEnded
     * whether or not this event has ended
     *
     * @return boolean
     */
    public function hasEnded()
    {
zYne's avatar
zYne committed
211
        return ($this->_endedMicrotime != null);
212 213 214 215 216
    }
    /**
     * end
     * ends the internal timer of this event
     *
zYne's avatar
zYne committed
217
     * @return Doctrine_Event   this object
218 219 220
     */
    public function end()
    {
zYne's avatar
zYne committed
221
        $this->_endedMicrotime = microtime(true);
zYne's avatar
zYne committed
222 223
        
        return $this;
224 225 226 227 228
    }
    /**
     * getInvoker
     * returns the handler that invoked this event
     *
229 230
     * @return Doctrine_Connection|Doctrine_Connection_Statement|
     *         Doctrine_Connection_UnitOfWork|Doctrine_Transaction   the handler that invoked this event
231 232 233
     */
    public function getInvoker()
    {
zYne's avatar
zYne committed
234
        return $this->_invoker;
235 236 237 238 239 240 241 242 243
    }
    /**
     * getParams
     * returns the parameters of the query
     *
     * @return array   parameters of the query
     */
    public function getParams()
    {
zYne's avatar
zYne committed
244
        return $this->_params;
245 246 247 248 249 250 251 252 253
    }
    /**
     * 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
254
        if (is_null($this->_endedMicrotime)) {
255 256
            return false;
        }
zYne's avatar
zYne committed
257
        return ($this->_endedMicrotime - $this->_startedMicrotime);
258 259
    }
}