1: | <?php |
2: | |
3: | |
4: | |
5: | |
6: | |
7: | |
8: | |
9: | |
10: | |
11: | |
12: | |
13: | |
14: | |
15: | |
16: | |
17: | |
18: | |
19: | |
20: | |
21: | |
22: | |
23: | |
24: | |
25: | |
26: | namespace Kint\Parser; |
27: | |
28: | use Kint\Object\BasicObject; |
29: | use Kint\Object\Representation\MicrotimeRepresentation; |
30: | |
31: | class MicrotimePlugin extends Plugin |
32: | { |
33: | private static $last = null; |
34: | private static $start = null; |
35: | private static $times = 0; |
36: | private static $group = 0; |
37: | |
38: | public function getTypes() |
39: | { |
40: | return array('string', 'double'); |
41: | } |
42: | |
43: | public function getTriggers() |
44: | { |
45: | return Parser::TRIGGER_SUCCESS; |
46: | } |
47: | |
48: | public function parse(&$var, BasicObject &$o, $trigger) |
49: | { |
50: | if (0 !== $o->depth) { |
51: | return; |
52: | } |
53: | |
54: | if (\is_string($var)) { |
55: | if ('microtime()' !== $o->name || !\preg_match('/^0\\.[0-9]{8} [0-9]{10}$/', $var)) { |
56: | return; |
57: | } |
58: | |
59: | $usec = (int) \substr($var, 2, 6); |
60: | $sec = (int) \substr($var, 11, 10); |
61: | } else { |
62: | if ('microtime(...)' !== $o->name) { |
63: | return; |
64: | } |
65: | |
66: | $sec = \floor($var); |
67: | $usec = $var - $sec; |
68: | $usec = \floor($usec * 1000000); |
69: | } |
70: | |
71: | $time = $sec + ($usec / 1000000); |
72: | |
73: | if (null !== self::$last) { |
74: | $last_time = self::$last[0] + (self::$last[1] / 1000000); |
75: | $lap = $time - $last_time; |
76: | ++self::$times; |
77: | } else { |
78: | $lap = null; |
79: | self::$start = $time; |
80: | } |
81: | |
82: | self::$last = array($sec, $usec); |
83: | |
84: | if (null !== $lap) { |
85: | $total = $time - self::$start; |
86: | $r = new MicrotimeRepresentation($sec, $usec, self::$group, $lap, $total, self::$times); |
87: | } else { |
88: | $r = new MicrotimeRepresentation($sec, $usec, self::$group); |
89: | } |
90: | $r->contents = $var; |
91: | $r->implicit_label = true; |
92: | |
93: | $o->removeRepresentation($o->value); |
94: | $o->addRepresentation($r); |
95: | $o->hints[] = 'microtime'; |
96: | } |
97: | |
98: | public static function clean() |
99: | { |
100: | self::$last = null; |
101: | self::$start = null; |
102: | self::$times = 0; |
103: | ++self::$group; |
104: | } |
105: | } |
106: | |