|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Platine Framework |
|
5
|
|
|
* |
|
6
|
|
|
* Platine Framework is a lightweight, high-performance, simple and elegant |
|
7
|
|
|
* PHP Web framework |
|
8
|
|
|
* |
|
9
|
|
|
* This content is released under the MIT License (MIT) |
|
10
|
|
|
* |
|
11
|
|
|
* Copyright (c) 2020 Platine Framework |
|
12
|
|
|
* Copyright (c) 2015 PHP Reboot |
|
13
|
|
|
* |
|
14
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
15
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
16
|
|
|
* in the Software without restriction, including without limitation the rights |
|
17
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
18
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
19
|
|
|
* furnished to do so, subject to the following conditions: |
|
20
|
|
|
* |
|
21
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
22
|
|
|
* copies or substantial portions of the Software. |
|
23
|
|
|
* |
|
24
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
25
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
26
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
27
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
28
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
29
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
30
|
|
|
* SOFTWARE. |
|
31
|
|
|
*/ |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @file Watch.php |
|
35
|
|
|
* |
|
36
|
|
|
* The Stop Watch class |
|
37
|
|
|
* |
|
38
|
|
|
* @package Platine\Framework\Helper\Timer |
|
39
|
|
|
* @author Platine Developers team |
|
40
|
|
|
* @copyright Copyright (c) 2020 |
|
41
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
|
42
|
|
|
* @link https://www.platine-php.com |
|
43
|
|
|
* @version 1.0.0 |
|
44
|
|
|
* @filesource |
|
45
|
|
|
*/ |
|
46
|
|
|
|
|
47
|
|
|
declare(strict_types=1); |
|
48
|
|
|
|
|
49
|
|
|
namespace Platine\Framework\Helper\Timer; |
|
50
|
|
|
|
|
51
|
|
|
use InvalidArgumentException; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @class Watch |
|
55
|
|
|
* @package Platine\Framework\Helper\Timer |
|
56
|
|
|
*/ |
|
57
|
|
|
class Watch |
|
58
|
|
|
{ |
|
59
|
|
|
/** |
|
60
|
|
|
* The default name to use if none is provided |
|
61
|
|
|
*/ |
|
62
|
|
|
public const WATCH_DEFAULT_NAME = '__default__'; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* The timer list |
|
66
|
|
|
* @var array<string, Timer> |
|
67
|
|
|
*/ |
|
68
|
|
|
protected array $timers = []; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Start the timer |
|
72
|
|
|
* @param string $name |
|
73
|
|
|
* @return bool |
|
74
|
|
|
*/ |
|
75
|
|
|
public function start(string $name = self::WATCH_DEFAULT_NAME): bool |
|
76
|
|
|
{ |
|
77
|
|
|
$this->addWatch($name); |
|
78
|
|
|
|
|
79
|
|
|
return $this->getWatch($name)->start(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Pause the timer |
|
84
|
|
|
* @param string $name |
|
85
|
|
|
* @return bool |
|
86
|
|
|
*/ |
|
87
|
|
|
public function pause(string $name = self::WATCH_DEFAULT_NAME): bool |
|
88
|
|
|
{ |
|
89
|
|
|
if ($this->exists($name) === false) { |
|
90
|
|
|
return false; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return $this->getWatch($name)->pause(); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Stop the timer |
|
98
|
|
|
* @param string $name |
|
99
|
|
|
* @return bool |
|
100
|
|
|
*/ |
|
101
|
|
|
public function stop(string $name = self::WATCH_DEFAULT_NAME): bool |
|
102
|
|
|
{ |
|
103
|
|
|
if ($this->exists($name) === false) { |
|
104
|
|
|
return false; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $this->getWatch($name)->stop(); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Get the timer time |
|
112
|
|
|
* @param string $name |
|
113
|
|
|
* @return float |
|
114
|
|
|
*/ |
|
115
|
|
|
public function getTime(string $name = self::WATCH_DEFAULT_NAME): float |
|
116
|
|
|
{ |
|
117
|
|
|
if ($this->exists($name) === false) { |
|
118
|
|
|
return -1; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return $this->getWatch($name)->getTime(); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Add a new time to the stop watch. |
|
126
|
|
|
* @param string $name |
|
127
|
|
|
* @return $this |
|
128
|
|
|
*/ |
|
129
|
|
|
public function addWatch(string $name): self |
|
130
|
|
|
{ |
|
131
|
|
|
if (array_key_exists($name, $this->timers)) { |
|
132
|
|
|
throw new InvalidArgumentException(sprintf( |
|
133
|
|
|
'Watch with the name [%s] already exist', |
|
134
|
|
|
$name |
|
135
|
|
|
)); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
$timer = new Timer($name); |
|
139
|
|
|
$this->timers[$name] = $timer; |
|
140
|
|
|
|
|
141
|
|
|
return $this; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Get watch |
|
146
|
|
|
* @param string $name |
|
147
|
|
|
* @return Timer |
|
148
|
|
|
*/ |
|
149
|
|
|
public function getWatch(string $name): Timer |
|
150
|
|
|
{ |
|
151
|
|
|
if (array_key_exists($name, $this->timers) === false) { |
|
152
|
|
|
throw new InvalidArgumentException(sprintf( |
|
153
|
|
|
'Watch with the name [%s] does not exist', |
|
154
|
|
|
$name |
|
155
|
|
|
)); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
return $this->timers[$name]; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Whether the timer with given name exists |
|
163
|
|
|
* @param string $name |
|
164
|
|
|
* @return bool |
|
165
|
|
|
*/ |
|
166
|
|
|
public function exists(string $name): bool |
|
167
|
|
|
{ |
|
168
|
|
|
return array_key_exists($name, $this->timers); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Return the total timers |
|
173
|
|
|
* @return int |
|
174
|
|
|
*/ |
|
175
|
|
|
public function count(): int |
|
176
|
|
|
{ |
|
177
|
|
|
return count($this->timers); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Return watch details |
|
182
|
|
|
* @return array<string, float> |
|
183
|
|
|
*/ |
|
184
|
|
|
public function info(): array |
|
185
|
|
|
{ |
|
186
|
|
|
$result = []; |
|
187
|
|
|
foreach ($this->timers as $name => /** @var Timer $timer */ $timer) { |
|
188
|
|
|
$result[$name] = $timer->getTime(); |
|
189
|
|
|
} |
|
190
|
|
|
return $result; |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|