Esempio n. 1
0
 /**
  * Reads sourcecode from files, for the backtrace.
  *
  * @param string $file
  *   Path to the file you want to read.
  * @param int $highlight
  *   The line number you want to highlight
  * @param int $from
  *   The start line.
  * @param int $to
  *   The end line.
  *
  * @return string
  *   The source code.
  */
 public function readSourcecode($file, $highlight, $from, $to)
 {
     $result = '';
     if (is_readable($file)) {
         // Load content and add it to the backtrace.
         $contentArray = file($file);
         // Correct the value, in case we are exceeding the line numbers.
         if ($from < 0) {
             $from = 0;
         }
         if ($to > count($contentArray)) {
             $to = count($contentArray);
         }
         for ($currentLineNo = $from; $currentLineNo <= $to; $currentLineNo++) {
             if (isset($contentArray[$currentLineNo])) {
                 // Add it to the result.
                 $realLineNo = $currentLineNo + 1;
                 // Escape it.
                 $contentArray[$currentLineNo] = $this->encodeString($contentArray[$currentLineNo], true);
                 if ($currentLineNo === $highlight) {
                     $result .= $this->render->renderBacktraceSourceLine('highlight', $realLineNo, $contentArray[$currentLineNo]);
                 } else {
                     $result .= $this->render->renderBacktraceSourceLine('source', $realLineNo, $contentArray[$currentLineNo]);
                 }
             } else {
                 // End of the file.
                 break;
             }
         }
     }
     return $result;
 }