Get file code in specified range

@param  string $file The file name
@param  int $line The file line
@param  int $range The line range
@return string
181291
public function getFileCode($file, $line, $range = 20)
    {
        $code = file($file);
        $half = (int)($range / 2);

        $start = $line - $half;
        0 > $start && $start = 0;

        $total = count($code);
        $end = $line + $half;
        $total < $end && $end = $total;

        $len = strlen($end);

        array_unshift($code, null);
        $content = '';
        for ($i = $start; $i < $end; $i++) {
            $temp = str_pad($i, $len, 0, STR_PAD_LEFT) . ':  ' . $code[$i];
            if ($line != $i) {
                $content .= htmlspecialchars($temp, ENT_QUOTES);
            } else {
                $content .= '<strong class="text-danger">' . htmlspecialchars($temp, ENT_QUOTES) . '</strong>';
            }
        }

        return $content;
    }
33439
private function getExamplePathFromSource(string $file): string
    {
        return sprintf(
            '%s%s%s',
            trim($this->getSourceDirectory(), '\\/'),
            DIRECTORY_SEPARATOR,
            trim($file, '"')
        );
    }
862811
public function headersSent(&$file, &$line)
    {
        $file = $this->headers_sent_file;
        $line = $this->headers_sent_line;
        return $this->headers_sent;
    }
616419
public static function get(
        $file
    ) {
        if (is_string($file)) {
            $file = new \SplFileInfo($file);
        }

        $extension = mb_strtolower($file->getExtension());
        if ('' === $extension) {
            return static::MIME_TYPE_IF_UNKNOWN;
        }

        if (array_key_exists($extension, static::$mimeTypes)) {
            return static::$mimeTypes[$extension];
        }

        if (function_exists('finfo_open') && $file->isFile()) {
            $path = $file->getPath();

            $fileInfo = finfo_open(FILEINFO_MIME);
            $mimeType = finfo_file($fileInfo, $path);
            finfo_close($fileInfo);

            return $mimeType;
        }

        return static::MIME_TYPE_IF_UNKNOWN;
    }
1275273
public static function getMime($file)
    {
        $fileInfo = finfo_open(FILEINFO_MIME_TYPE);
        $mime = finfo_file($fileInfo, $file);

        return empty($mime) ? : $mime;
    }
119285
public static function getFileContents(string $file) : string
    {
        if (!is_readable($file)) {
            throw new RuntimeException(sprintf(
                'File "%s" does not exist or is not readable',
                $file
            ));
        }

        $contents = file_get_contents($file);
        if ($contents === false) {
            throw new RuntimeException(sprintf(
                'Could not read from existing and readable file "%s"',
                $file
            ));
        }

        return $contents;
    }
188448
public static function getContent($file)
    {
        if (!File::exists($file)) {
            throw new IOException(sprintf("File %s does not exist", $file));
        }

        $content = file_get_contents($file);

        if (false === $content) {
            throw new IOException(sprintf("Cannot get content from file %s", $file));
        }

        return $content;
    }
654477
private function renderPHP($file, $line)
    {
        try {
            $count = count(file($file));
            $code  = highlight_file($file, true);
            return $this->wrapLineNumbers($count, $code, $line);
        }
        catch(Throwable $e) { // Error or Exception (executed only in PHP 7, will not match in PHP 5)
            return null;
        }
        catch(Exception $e) { // Once PHP 5 support is no longer needed, this block can be removed.
            return null;
        }
    }
987090
public function findMethodRange(File $file, LineRange $range)
    {
        $methodStartLine = $this->getMethodStartLine($file, $range);
        $methodEndLine = $this->getMethodEndLine($file, $range);

        return LineRange::fromLines($methodStartLine, $methodEndLine);
    }
951510
public function load($file, $range = '') {
        //$range = $range ?: $this->range;
        //if (!isset($this->lang[$range])) {
        //    $this->lang[$range] = [];
        //}

        // 批量定义
        if (is_string($file)) {
            $file = [$file];
        }

        $lang = [];

        foreach ($file as $_file) {
            if (is_file($_file)) {
                // 记录加载信息
                $_lang = include $_file;
                if (is_array($_lang)) {
                    $lang = $_lang+ $lang;
                }
            }
        }

        if(!$lang) {
            return false;
        }

        if($range) {
            if (!isset($this->range[$range])) {
                $this->range[$range] = [];
            }
            $this->range[$range] = $lang + $this->range[$range];
        }
        else {
            $this->lang = $lang + $this->lang;
        }
    }
1242234
public function getFileIcon($file = '')
    {
        $extension = File::extension($file);

        foreach ($this->fileTypes as $type => $regex) {
            if (preg_match("/^($regex)$/i", $extension) !== 0) {
                return "fa-file-{$type}-o";
            }
        }

        return 'fa-file-o';
    }
138397
public function getCode($file, $comments = true)
    {
        if (!is_string($file) || empty($file)) {
            throw new RuntimeException('Invalid filename provided.');
        }

        if (!is_readable($file)) {
            throw new RuntimeException("Cannot open $file for reading.");
        }

        if ($comments) {
            $content = file_get_contents($file);
        } else {
            $content = php_strip_whitespace($file);
        }

        $parsed = $this->parser->parse($content);
        $stmts = $this->traverser->traverseFile($parsed, $file);
        $pretty = $this->printer->prettyPrint($stmts);

        $pretty = preg_replace(
            '#^(<\?php)?[\s]*(/\*\*?.*?\*/)?[\s]*(declare[\s]*\([\s]*strict_types[\s]*=[\s]*1[\s]*\);)?#s',
            '',
            $pretty
        );

        return $this->getCodeWrappedIntoNamespace($parsed, $pretty);
    }
754511
private function FileGetContents($file) {
    $fileObj = new SplFileObject($file, 'r');
    while (!$fileObj->flock(LOCK_EX)) {
      usleep(1);
    }
    $str = $fileObj->fread($fileObj->getSize());
    $fileObj->flock(LOCK_UN);
    return $str;
  }
1119201
public function getUrl($file, $expire_at = 3600)
    {
        try {
            $accessUrl = $this->getOss()->signUrl($this->bucket, $file, $expire_at);
        } catch (OssException $e) {
            return false;
        }

        return $accessUrl;
    }
1141470
public function getModeByFile($file, $extension = null)
    {
        if (!$extension) {
            $extension = strtolower(substr($file, strrpos($file, '.')));
        }
        if (strpos($extension, '.') !== false) {
            if ($extension == '.csv') {
                return DATA_MANIPULATION_CREATE_MODE_CSV;
            }
            if ($extension == '.xls') {
                return DATA_MANIPULATION_CREATE_MODE_XLS;
            }
            if ($extension == '.xlsx') {
                return DATA_MANIPULATION_CREATE_MODE_XLSX;
            }
            if ($extension == '.ods') {
                return DATA_MANIPULATION_CREATE_MODE_ODS;
            }
        }
    }
1273228
protected function readFile(string $file) : string
    {
        if (is_file($file) === false) {
            throw new BadFileException("The file \"{$file}\" is not a file.", $file);
        }

        if (is_readable($file) === false) {
            throw new BadFileException("Unable to open \"{$file}\" as the file is not readable.", $file);
        }

        return file_get_contents($file);
    }
1026276
def get_lines_from_file(filename, lineno, context_lines):
    

    def get_lines(start, end):
        return [linecache.getline(filename, l).rstrip() for l in range(start, end)]

    lower_bound = max(1, lineno - context_lines)
    upper_bound = lineno + context_lines
    linecache.checkcache(filename)
    pre_context = get_lines(lower_bound, lineno)
    context_line = linecache.getline(filename, lineno).rstrip()
    post_context = get_lines(lineno + 1, upper_bound)
    return lower_bound, pre_context, context_line, post_context