1: <?php
2:
3: /*
4: * The MIT License (MIT)
5: *
6: * Copyright (c) 2013 Jonathan Vollebregt (jnvsor@gmail.com), Rokas Šleinius (raveren@gmail.com)
7: *
8: * Permission is hereby granted, free of charge, to any person obtaining a copy of
9: * this software and associated documentation files (the "Software"), to deal in
10: * the Software without restriction, including without limitation the rights to
11: * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
12: * the Software, and to permit persons to whom the Software is furnished to do so,
13: * subject to the following conditions:
14: *
15: * The above copyright notice and this permission notice shall be included in all
16: * copies or substantial portions of the Software.
17: *
18: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
20: * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
21: * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
22: * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23: * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
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: