# Source for file Profiler.php

Documentation is available at Profiler.php

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