1
<?php
2
/**
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information please see
17
 * <http://phing.info>.
18
 */
19

20
use Symfony\Component\Stopwatch\Stopwatch;
21

22
/**
23
 * Stopwatch.
24
 *
25
 * @author  Siad Ardroumli <siad.ardroumli@gmail.com>
26
 * @package phing.tasks.ext.stopwatch
27
 */
28
class StopwatchTask extends DispatchTask
29
{
30
    /**
31
     * Name of the timer.
32
     *
33
     * @var string $name
34
     */
35
    private $name = '';
36

37
    /**
38
     * Category of the timer.
39
     *
40
     * @var string $category optional
41
     */
42
    private $category = '';
43

44
    /**
45
     * Holds an instance of Stopwatch.
46
     *
47
     * @var Stopwatch $timer
48
     */
49
    private static $timer = null;
50

51
    /**
52
     * Initialize Task.
53
     *
54
     * @return void
55
     */
56 0
    public function init()
57
    {
58 0
        if (!class_exists('\\Symfony\\Component\\Stopwatch\\Stopwatch')) {
59 0
            throw new BuildException("StopwatchTask requires symfony/stopwatch to be installed.");
60
        }
61

62 0
        $this->setAction('start');
63
    }
64

65
    /**
66
     * Get the stopwatch instance.
67
     *
68
     * @return Stopwatch
69
     */
70 0
    private function getStopwatchInstance()
71
    {
72 0
        if (self::$timer === null) {
73 0
            self::$timer = new Stopwatch();
74
        }
75

76 0
        return self::$timer;
77
    }
78

79
    /**
80
     * Start timer.
81
     *
82
     * @return void
83
     */
84 0
    public function start()
85
    {
86 0
        $timer = $this->getStopwatchInstance();
87 0
        $timer->start($this->name, $this->category);
88
    }
89

90
    /**
91
     * Stop timer.
92
     *
93
     * @return void
94
     */
95 0
    public function stop()
96
    {
97 0
        $timer = $this->getStopwatchInstance();
98 0
        $event = $timer->stop($this->name);
99

100 0
        foreach ($event->getPeriods() as $period) {
101 0
            $this->log(
102 0
                'Starttime: ' . $period->getStartTime() . ' - Endtime: ' . $period->getEndTime() . ' - Duration: ' . $period->getDuration() . ' - Memory: ' . $period->getMemory(),
103 0
                Project::MSG_INFO
104
            );
105
        }
106

107 0
        $this->log('Name:       ' . $this->name, Project::MSG_INFO);
108 0
        $this->log('Category:   ' . $event->getCategory(), Project::MSG_INFO);
109 0
        $this->log('Origin:     ' . $event->getOrigin(), Project::MSG_INFO);
110 0
        $this->log('Start time: ' . $event->getStartTime(), Project::MSG_INFO);
111 0
        $this->log('End time:   ' . $event->getEndTime(), Project::MSG_INFO);
112 0
        $this->log('Duration:   ' . $event->getDuration(), Project::MSG_INFO);
113 0
        $this->log('Memory:     ' . $event->getMemory(), Project::MSG_INFO);
114
    }
115

116
    /**
117
     * Measure lap time.
118
     *
119
     * @return void
120
     */
121 0
    public function lap()
122
    {
123 0
        $timer = $this->getStopwatchInstance();
124 0
        $timer->lap($this->name);
125
    }
126

127
    /**
128
     * Set the name of the stopwatch.
129
     *
130
     * @param string $name the name of the stopwatch timer
131
     *
132
     * @return void
133
     */
134 0
    public function setName($name)
135
    {
136 0
        $this->name = $name;
137
    }
138

139
    /**
140
     * Set the category of the stopwatch.
141
     *
142
     * @param string $category
143
     *
144
     * @return void
145
     */
146 0
    public function setCategory($category)
147
    {
148 0
        $this->category = $category;
149
    }
150

151
    /**
152
     * The main entry point
153
     *
154
     * @return void
155
     *
156
     * @throws BuildException
157
     */
158 0
    public function main()
159
    {
160 0
        switch ($this->getAction()) {
161 0
            case "start":
162 0
                $this->start();
163 0
                break;
164 0
            case "stop":
165 0
                $this->stop();
166 0
                break;
167 0
            case "lap":
168 0
                $this->lap();
169 0
                break;
170
            default:
171 0
                throw new BuildException('action should be one of start, stop, lap.');
172
        }
173
    }
174
}

Read our documentation on viewing source code .

Loading