Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 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.doctrine-project.org>.
20
 */
21
 
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.doctrine-project.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
     * @param array $eventSequences         an array containing sequences of all listened events as keys
54
     */
55
    private $eventSequences = array();
56
 
57
    /**
58
     * constructor
59
     */
60
    public function __construct() {
61
 
62
    }
63
 
64
    /**
65
     * setFilterQueryType
66
     *
67
     * @param integer $filter
68
     * @return boolean
69
     */
70
    public function setFilterQueryType() {
71
 
72
    }
73
    /**
74
     * method overloader
75
     * this method is used for invoking different listeners, for the full
76
     * list of availible listeners, see Doctrine_EventListener
77
     *
78
     * @param string $m     the name of the method
79
     * @param array $a      method arguments
80
     * @see Doctrine_EventListener
81
     * @return boolean
82
     */
83
    public function __call($m, $a)
84
    {
85
        // first argument should be an instance of Doctrine_Event
86
        if ( ! ($a[0] instanceof Doctrine_Event)) {
87
            throw new Doctrine_Connection_Profiler_Exception("Couldn't listen event. Event should be an instance of Doctrine_Event.");
88
        }
89
 
90
 
91
        if (substr($m, 0, 3) === 'pre') {
92
            // pre-event listener found
93
            $a[0]->start();
94
 
95
            $eventSequence = $a[0]->getSequence();
96
            if ( ! isset($this->eventSequences[$eventSequence])) {
97
                $this->events[] = $a[0];
98
                $this->eventSequences[$eventSequence] = true;
99
            }
100
        } else {
101
            // after-event listener found
102
            $a[0]->end();
103
        }
104
    }
105
 
106
    /**
107
     * get
108
     *
109
     * @param mixed $key
110
     * @return Doctrine_Event
111
     */
112
    public function get($key)
113
    {
114
        if (isset($this->events[$key])) {
115
            return $this->events[$key];
116
        }
117
        return null;
118
    }
119
 
120
    /**
121
     * getAll
122
     * returns all profiled events as an array
123
     *
124
     * @return array        all events in an array
125
     */
126
    public function getAll()
127
    {
128
        return $this->events;
129
    }
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
    /**
143
     * count
144
     *
145
     * @return integer
146
     */
147
    public function count()
148
    {
149
        return count($this->events);
150
    }
151
 
152
    /**
153
     * pop the last event from the event stack
154
     *
155
     * @return Doctrine_Event
156
     */
157
    public function pop()
158
    {
159
        $event = array_pop($this->events);
160
        if ($event !== null)
161
        {
162
            unset($this->eventSequences[$event->getSequence()]);
163
        }
164
        return $event;
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
}