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\Renderer\Text; |
27: | |
28: | use Kint\Object\BasicObject; |
29: | use Kint\Object\Representation\MicrotimeRepresentation; |
30: | use Kint\Renderer\PlainRenderer; |
31: | use Kint\Renderer\Rich\MicrotimePlugin as RichPlugin; |
32: | use Kint\Renderer\TextRenderer; |
33: | use Kint\Utils; |
34: | |
35: | class MicrotimePlugin extends Plugin |
36: | { |
37: | protected $useJs = false; |
38: | |
39: | public function __construct(TextRenderer $r) |
40: | { |
41: | parent::__construct($r); |
42: | |
43: | if ($this->renderer instanceof PlainRenderer) { |
44: | $this->useJs = true; |
45: | } |
46: | } |
47: | |
48: | public function render(BasicObject $o) |
49: | { |
50: | $r = $o->getRepresentation('microtime'); |
51: | |
52: | if (!$r instanceof MicrotimeRepresentation) { |
53: | return false; |
54: | } |
55: | |
56: | $out = ''; |
57: | |
58: | if (0 == $o->depth) { |
59: | $out .= $this->renderer->colorTitle($this->renderer->renderTitle($o)).PHP_EOL; |
60: | } |
61: | |
62: | $out .= $this->renderer->renderHeader($o); |
63: | $out .= $this->renderer->renderChildren($o).PHP_EOL; |
64: | |
65: | $indent = \str_repeat(' ', ($o->depth + 1) * $this->renderer->indent_width); |
66: | |
67: | if ($this->useJs) { |
68: | $out .= '<span data-kint-microtime-group="'.$r->group.'">'; |
69: | } |
70: | |
71: | $out .= $indent.$this->renderer->colorType('TIME:').' '; |
72: | $out .= $this->renderer->colorValue($r->getDateTime()->format('Y-m-d H:i:s.u')).PHP_EOL; |
73: | |
74: | if (null !== $r->lap) { |
75: | $out .= $indent.$this->renderer->colorType('SINCE LAST CALL:').' '; |
76: | |
77: | $lap = \round($r->lap, 4); |
78: | |
79: | if ($this->useJs) { |
80: | $lap = '<span class="kint-microtime-lap">'.$lap.'</span>'; |
81: | } |
82: | |
83: | $out .= $this->renderer->colorValue($lap.'s').'.'.PHP_EOL; |
84: | } |
85: | if (null !== $r->total) { |
86: | $out .= $indent.$this->renderer->colorType('SINCE START:').' '; |
87: | $out .= $this->renderer->colorValue(\round($r->total, 4).'s').'.'.PHP_EOL; |
88: | } |
89: | if (null !== $r->avg) { |
90: | $out .= $indent.$this->renderer->colorType('AVERAGE DURATION:').' '; |
91: | |
92: | $avg = \round($r->avg, 4); |
93: | |
94: | if ($this->useJs) { |
95: | $avg = '<span class="kint-microtime-avg">'.$avg.'</span>'; |
96: | } |
97: | |
98: | $out .= $this->renderer->colorValue($avg.'s').'.'.PHP_EOL; |
99: | } |
100: | |
101: | $bytes = Utils::getHumanReadableBytes($r->mem); |
102: | $mem = $r->mem.' bytes ('.\round($bytes['value'], 3).' '.$bytes['unit'].')'; |
103: | $bytes = Utils::getHumanReadableBytes($r->mem_real); |
104: | $mem .= ' (real '.\round($bytes['value'], 3).' '.$bytes['unit'].')'; |
105: | |
106: | $out .= $indent.$this->renderer->colorType('MEMORY USAGE:').' '; |
107: | $out .= $this->renderer->colorValue($mem).'.'.PHP_EOL; |
108: | |
109: | $bytes = Utils::getHumanReadableBytes($r->mem_peak); |
110: | $mem = $r->mem_peak.' bytes ('.\round($bytes['value'], 3).' '.$bytes['unit'].')'; |
111: | $bytes = Utils::getHumanReadableBytes($r->mem_peak_real); |
112: | $mem .= ' (real '.\round($bytes['value'], 3).' '.$bytes['unit'].')'; |
113: | |
114: | $out .= $indent.$this->renderer->colorType('PEAK MEMORY USAGE:').' '; |
115: | $out .= $this->renderer->colorValue($mem).'.'.PHP_EOL; |
116: | |
117: | if ($this->useJs) { |
118: | $out .= '</span>'; |
119: | } |
120: | |
121: | return $out; |
122: | } |
123: | |
124: | public static function renderJs() |
125: | { |
126: | return RichPlugin::renderJs(); |
127: | } |
128: | } |
129: | |