1: 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: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58:
<?php
namespace Faulancer\View\Helper;
use Faulancer\View\AbstractViewHelper;
use Faulancer\View\ViewController;
class CodeBlock extends AbstractViewHelper
{
public function __invoke(ViewController $view, $filename, $actualLine = 0)
{
$lineNr = 0;
$lines = '';
$file = fopen($filename, 'r');
$tabChar = ' ';
$regularChar = ' ';
while ($line = fgets($file)) {
$lineNr++;
if ($lineNr >= $actualLine - 6 && $lineNr <= $actualLine + 3) {
$l = str_replace("\t", $tabChar, $line);
$l = str_replace(" ", $regularChar, $l);
if ($lineNr === $actualLine) {
$lines .= '<span class="line highlight"><span class="line-nr">' . $lineNr . '</span>' . $view->highlighter($l, 'php') . '</span>';
} else {
$lines .= '<span class="line"><span class="line-nr">' . $lineNr . '</span>' . $view->highlighter($l, 'php') . '</span>';
}
}
}
return $lines;
}
}