| 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 | Doctrine::autoload('Doctrine_Overloadable'); |
| 22 | /** |
| 23 | * Doctrine_Connection_Profiler |
| 24 | * |
| 25 | * @package Doctrine |
| 26 | * @subpackage Connection |
| 27 | * @author Konsta Vesterinen <kvesteri@cc.hut.fi> |
| 28 | * @license http://www.opensource.org/licenses/lgpl-license.php LGPL |
| 29 | * @link www.phpdoctrine.org |
| 30 | * @since 1.0 |
| 31 | * @version $Revision$ |
| 32 | */ |
| 33 | class Doctrine_Connection_Profiler implements Doctrine_Overloadable, IteratorAggregate, Countable |
| 34 | { |
| 35 | /** |
| 36 | * @param array $listeners an array containing all availible listeners |
| 37 | */ |
| 38 | private $listeners = array('query', |
| 39 | 'prepare', |
| 40 | 'commit', |
| 41 | 'rollback', |
| 42 | 'connect', |
| 43 | 'begintransaction', |
| 44 | 'exec', |
| 45 | 'execute', |
| 46 | ); |
| 47 | |
| 48 | /** |
| 49 | * @param array $events an array containing all listened events |
| 50 | */ |
| 51 | private $events = array(); |
| 52 | |
| 53 | /** |
| 54 | * constructor |
| 55 | */ |
| 56 | public function __construct() { |
| 57 | |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * setFilterQueryType |
| 62 | * |
| 63 | * @param integer $filter |
| 64 | * @return boolean |
| 65 | */ |
| 66 | public function setFilterQueryType() { |
| 67 | |
| 68 | } |
| 69 | /** |
| 70 | * method overloader |
| 71 | * this method is used for invoking different listeners, for the full |
| 72 | * list of availible listeners, see Doctrine_EventListener |
| 73 | * |
| 74 | * @param string $m the name of the method |
| 75 | * @param array $a method arguments |
| 76 | * @see Doctrine_EventListener |
| 77 | * @return boolean |
| 78 | */ |
| 79 | public function __call($m, $a) |
| 80 | { |
| 81 | // first argument should be an instance of Doctrine_Event |
| 82 | if ( ! ($a[0] instanceof Doctrine_Event)) { |
| 83 | throw new Doctrine_Connection_Profiler_Exception("Couldn't listen event. Event should be an instance of Doctrine_Event."); |
| 84 | } |
| 85 | |
| 86 | |
| 87 | if (substr($m, 0, 3) === 'pre') { |
| 88 | // pre-event listener found |
| 89 | $a[0]->start(); |
| 90 | |
| 91 | if ( ! in_array($a[0], $this->events, true)) { |
| 92 | $this->events[] = $a[0]; |
| 93 | } |
| 94 | } else { |
| 95 | // after-event listener found |
| 96 | $a[0]->end(); |
| 97 | } |
| 98 | /** |
| 99 | * If filtering by query type is enabled, only keep the query if |
| 100 | * it was one of the allowed types. |
| 101 | */ |
| 102 | /** |
| 103 | if ( ! is_null($this->filterTypes)) { |
| 104 | if ( ! ($a[0]->getQueryType() & $this->_filterTypes)) { |
| 105 | |
| 106 | } |
| 107 | } |
| 108 | */ |
| 109 | |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * get |
| 114 | * |
| 115 | * @param mixed $key |
| 116 | * @return Doctrine_Event |
| 117 | */ |
| 118 | public function get($key) |
| 119 | { |
| 120 | if (isset($this->events[$key])) { |
| 121 | return $this->events[$key]; |
| 122 | } |
| 123 | return null; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * getAll |
| 128 | * returns all profiled events as an array |
| 129 | * |
| 130 | * @return array all events in an array |
| 131 | */ |
| 132 | public function getAll() |
| 133 | { |
| 134 | return $this->events; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * getIterator |
| 139 | * returns an iterator that iterates through the logged events |
| 140 | * |
| 141 | * @return ArrayIterator |
| 142 | */ |
| 143 | public function getIterator() |
| 144 | { |
| 145 | return new ArrayIterator($this->events); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * count |
| 150 | * |
| 151 | * @return integer |
| 152 | */ |
| 153 | public function count() |
| 154 | { |
| 155 | return count($this->events); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * pop the last event from the event stack |
| 160 | * |
| 161 | * @return Doctrine_Event |
| 162 | */ |
| 163 | public function pop() |
| 164 | { |
| 165 | return array_pop($this->events); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Get the Doctrine_Event object for the last query that was run, regardless if it has |
| 170 | * ended or not. If the event has not ended, it's end time will be Null. |
| 171 | * |
| 172 | * @return Doctrine_Event |
| 173 | */ |
| 174 | public function lastEvent() |
| 175 | { |
| 176 | if (empty($this->events)) { |
| 177 | return false; |
| 178 | } |
| 179 | |
| 180 | end($this->events); |
| 181 | return current($this->events); |
| 182 | } |
| 183 | } |