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\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: