Coverage for Doctrine_Connection_Profiler

Back to coverage report

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