1
0
mirror of synced 2024-12-13 22:56:04 +03:00
doctrine2/lib/Doctrine/Event.php

330 lines
8.9 KiB
PHP
Raw Normal View History

<?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
* @subpackage Event
* @link www.phpdoctrine.com
* @since 1.0
* @version $Revision$
*/
class Doctrine_Event
{
/**
2007-06-25 21:24:20 +04:00
* CONNECTION EVENT CODES
*/
2007-06-25 21:24:20 +04:00
const CONN_QUERY = 1;
const CONN_EXEC = 2;
const CONN_PREPARE = 3;
const CONN_CONNECT = 4;
2007-06-26 16:08:50 +04:00
const CONN_CLOSE = 5;
2007-07-12 02:03:47 +04:00
const CONN_ERROR = 6;
2007-06-25 21:24:20 +04:00
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;
const HYDRATE = 40;
/*
2007-06-25 21:24:20 +04:00
* 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;
/**
2007-06-25 02:12:46 +04:00
* @var mixed $_invoker the handler which invoked this event
*/
2007-06-25 02:12:46 +04:00
protected $_invoker;
/**
2007-06-25 02:12:46 +04:00
* @var string $_query the sql query associated with this event (if any)
*/
2007-06-25 02:12:46 +04:00
protected $_query;
/**
2007-06-25 02:12:46 +04:00
* @var string $_params the parameters associated with the query (if any)
*/
2007-06-25 02:12:46 +04:00
protected $_params;
/**
2007-06-25 02:04:57 +04:00
* @see Doctrine_Event constants
2007-06-25 02:12:46 +04:00
* @var integer $_code the event code
*/
2007-06-25 02:12:46 +04:00
protected $_code;
/**
2007-06-25 02:12:46 +04:00
* @var integer $_startedMicrotime the time point in which this event was started
*/
2007-06-25 02:12:46 +04:00
protected $_startedMicrotime;
/**
2007-06-25 02:12:46 +04:00
* @var integer $_endedMicrotime the time point in which this event was ended
*/
2007-06-25 02:12:46 +04:00
protected $_endedMicrotime;
2007-06-25 02:12:46 +04:00
/**
2007-06-25 13:12:09 +04:00
* @var array $_options an array of options
2007-06-25 02:12:46 +04:00
*/
2007-06-25 13:12:09 +04:00
protected $_options = array();
/**
* constructor
*
* @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)
*/
public function __construct($invoker, $code, $query = null, $params = array())
{
2007-06-25 02:12:46 +04:00
$this->_invoker = $invoker;
$this->_code = $code;
$this->_query = $query;
$this->_params = $params;
}
/**
* getQuery
*
* @return string returns the query associated with this event (if any)
*/
public function getQuery()
{
2007-06-25 02:12:46 +04:00
return $this->_query;
}
/**
* getName
* returns the name of this event
*
* @return string the name of this event
*/
public function getName()
{
2007-06-25 02:12:46 +04:00
switch ($this->_code) {
case self::CONN_QUERY:
return 'query';
case self::CONN_EXEC:
return 'exec';
case self::CONN_PREPARE:
return 'prepare';
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:
return 'begin';
case self::TX_COMMIT:
return 'commit';
case self::TX_ROLLBACK:
return 'rollback';
case self::SAVEPOINT_CREATE:
return 'create savepoint';
case self::SAVEPOINT_ROLLBACK:
return 'rollback savepoint';
case self::SAVEPOINT_COMMIT:
return 'commit savepoint';
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';
}
}
/**
* getCode
*
* @return integer returns the code associated with this event
*/
public function getCode()
{
2007-06-25 02:12:46 +04:00
return $this->_code;
}
2007-06-25 02:23:53 +04:00
/**
2007-06-25 13:12:09 +04:00
* getOption
* returns the value of an option
2007-06-25 02:23:53 +04:00
*
2007-06-25 13:12:09 +04:00
* @param string $option the name of the option
* @return mixed
*/
2007-06-25 14:08:03 +04:00
public function __get($option)
2007-06-25 13:12:09 +04:00
{
if ( ! isset($this->_options[$option])) {
return null;
}
return $this->_options[$option];
}
2007-06-25 13:12:09 +04:00
/**
* skipOperation
* skips the next operation
2007-06-25 14:08:03 +04:00
* an alias for __set('skipOperation', true)
2007-06-25 13:12:09 +04:00
*
* @return Doctrine_Event this object
2007-06-25 02:23:53 +04:00
*/
2007-06-25 13:12:09 +04:00
public function skipOperation()
2007-06-25 02:23:53 +04:00
{
2007-06-25 14:08:03 +04:00
$this->_options['skipOperation'] = true;
return $this;
2007-06-25 02:23:53 +04:00
}
2007-06-25 02:23:53 +04:00
/**
2007-06-25 13:12:09 +04:00
* setOption
* sets the value of an option
2007-06-25 02:23:53 +04:00
*
2007-06-25 13:12:09 +04:00
* @param string $option the name of the option
* @param mixed $value the value of the given option
* @return Doctrine_Event this object
2007-06-25 02:23:53 +04:00
*/
2007-06-25 14:08:03 +04:00
public function __set($option, $value)
2007-06-25 02:23:53 +04:00
{
2007-06-25 13:12:09 +04:00
$this->_options[$option] = $value;
return $this;
2007-06-25 02:23:53 +04:00
}
/**
* 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;
}
/**
* start
* starts the internal timer of this event
*
2007-06-25 13:12:09 +04:00
* @return Doctrine_Event this object
*/
public function start()
{
2007-06-25 02:12:46 +04:00
$this->_startedMicrotime = microtime(true);
}
/**
* hasEnded
* whether or not this event has ended
*
* @return boolean
*/
public function hasEnded()
{
2007-06-25 02:12:46 +04:00
return ($this->_endedMicrotime != null);
}
/**
* end
* ends the internal timer of this event
*
2007-06-25 13:12:09 +04:00
* @return Doctrine_Event this object
*/
public function end()
{
2007-06-25 02:12:46 +04:00
$this->_endedMicrotime = microtime(true);
2007-06-25 13:12:09 +04:00
return $this;
}
/**
* getInvoker
* returns the handler that invoked this event
*
* @return Doctrine_Connection|Doctrine_Connection_Statement|
* Doctrine_Connection_UnitOfWork|Doctrine_Transaction the handler that invoked this event
*/
public function getInvoker()
{
2007-06-25 02:12:46 +04:00
return $this->_invoker;
}
/**
* getParams
* returns the parameters of the query
*
* @return array parameters of the query
*/
public function getParams()
{
2007-06-25 02:12:46 +04:00
return $this->_params;
}
/**
* Get the elapsed time (in microseconds) that the event ran. If the event has
* not yet ended, return false.
*
* @return mixed
*/
public function getElapsedSecs()
{
2007-06-25 02:12:46 +04:00
if (is_null($this->_endedMicrotime)) {
return false;
}
2007-06-25 02:12:46 +04:00
return ($this->_endedMicrotime - $this->_startedMicrotime);
}
}