You are here

private static function Kint::_showSource in Devel 8.2

Same name and namespace in other branches
  1. 8 kint/kint/Kint.class.php \Kint::_showSource()

* trace helper, shows the place in code inline * *

Parameters

string $file full path to file: * @param int $lineNumber the line to display * @param int $padding surrounding lines to show besides the main one * * @return bool|string

1 call to Kint::_showSource()
Kint::_parseTrace in kint/kint/Kint.class.php

File

kint/kint/Kint.class.php, line 322

Class

Kint

Code

private static function _showSource($file, $lineNumber, $padding = 7) {
  if (!$file or !is_readable($file)) {

    # continuing will cause errors
    return false;
  }

  # open the file and set the line position
  $file = fopen($file, 'r');
  $line = 0;

  # Set the reading range
  $range = array(
    'start' => $lineNumber - $padding,
    'end' => $lineNumber + $padding,
  );

  # set the zero-padding amount for line numbers
  $format = '% ' . strlen($range['end']) . 'd';
  $source = '';
  while (($row = fgets($file)) !== false) {

    # increment the line number
    if (++$line > $range['end']) {
      break;
    }
    if ($line >= $range['start']) {

      # make the row safe for output
      $row = htmlspecialchars($row, ENT_NOQUOTES, 'UTF-8');

      # trim whitespace and sanitize the row
      $row = '<span>' . sprintf($format, $line) . '</span> ' . $row;
      if ($line === $lineNumber) {

        # apply highlighting to this row
        $row = '<div class="kint-highlight">' . $row . '</div>';
      }
      else {
        $row = '<div>' . $row . '</div>';
      }

      # add to the captured source
      $source .= $row;
    }
  }

  # close the file
  fclose($file);
  return $source;
}