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\Rich;
27:
28: use Kint\Object\Representation\Representation;
29: use Kint\Object\Representation\SourceRepresentation;
30:
31: class SourcePlugin extends Plugin implements TabPluginInterface
32: {
33: public function renderTab(Representation $r)
34: {
35: if (!($r instanceof SourceRepresentation) || empty($r->source)) {
36: return false;
37: }
38:
39: $source = $r->source;
40:
41: // Trim empty lines from the start and end of the source
42: foreach ($source as $linenum => $line) {
43: if (\strlen(\trim($line)) || $linenum === $r->line) {
44: break;
45: }
46:
47: unset($source[$linenum]);
48: }
49:
50: foreach (\array_reverse($source, true) as $linenum => $line) {
51: if (\strlen(\trim($line)) || $linenum === $r->line) {
52: break;
53: }
54:
55: unset($source[$linenum]);
56: }
57:
58: $output = '';
59:
60: foreach ($source as $linenum => $line) {
61: if ($linenum === $r->line) {
62: $output .= '<div class="kint-highlight">'.$this->renderer->escape($line)."\n".'</div>';
63: } else {
64: $output .= '<div>'.$this->renderer->escape($line)."\n".'</div>';
65: }
66: }
67:
68: if ($output) {
69: \reset($source);
70:
71: $data = '';
72: if ($r->showfilename) {
73: $data = ' data-kint-filename="'.$this->renderer->escape($r->filename).'"';
74: }
75:
76: return '<div><pre class="kint-source"'.$data.' style="counter-reset: kint-l '.((int) \key($source) - 1).';">'.$output.'</pre></div><div></div>';
77: }
78: }
79: }
80: