Neos\Flow\Error\Debugger::getCodeSnippet PHP 메소드

getCodeSnippet() 공개 정적인 메소드

Returns a code snippet from the specified file.
public static getCodeSnippet ( string $filePathAndName, integer $lineNumber, boolean $plaintext = false ) : string
$filePathAndName string Absolute path and filename of the PHP file
$lineNumber integer Line number defining the center of the code snippet
$plaintext boolean
리턴 string The code snippet
    public static function getCodeSnippet($filePathAndName, $lineNumber, $plaintext = false)
    {
        $pathPosition = strpos($filePathAndName, 'Packages/');
        if ($plaintext) {
            $codeSnippet = PHP_EOL;
        } else {
            $codeSnippet = '<br />';
        }
        if (@file_exists($filePathAndName)) {
            $phpFile = @file($filePathAndName);
            if (is_array($phpFile)) {
                $startLine = $lineNumber > 2 ? $lineNumber - 2 : 1;
                $endLine = $lineNumber < count($phpFile) - 2 ? $lineNumber + 3 : count($phpFile) + 1;
                if ($endLine > $startLine) {
                    if ($pathPosition !== false) {
                        if ($plaintext) {
                            $codeSnippet = PHP_EOL . substr($filePathAndName, $pathPosition) . ':' . PHP_EOL;
                        } else {
                            $codeSnippet = '<br /><span style="font-size:10px;">' . substr($filePathAndName, $pathPosition) . ':</span><br /><pre>';
                        }
                    } else {
                        if ($plaintext) {
                            $codeSnippet = PHP_EOL . $filePathAndName . ':' . PHP_EOL;
                        } else {
                            $codeSnippet = '<br /><span style="font-size:10px;">' . $filePathAndName . ':</span><br /><pre>';
                        }
                    }
                    for ($line = $startLine; $line < $endLine; $line++) {
                        $codeLine = str_replace("\t", ' ', $phpFile[$line - 1]);
                        if ($line === $lineNumber) {
                            if (!$plaintext) {
                                $codeSnippet .= '</pre><pre style="background-color: #F1F1F1; color: black;">';
                            }
                        }
                        $codeSnippet .= sprintf('%05d', $line) . ': ';
                        if ($plaintext) {
                            $codeSnippet .= $codeLine;
                        } else {
                            $codeSnippet .= htmlspecialchars($codeLine);
                        }
                        if ($line === $lineNumber && !$plaintext) {
                            $codeSnippet .= '</pre><pre>';
                        }
                    }
                    if (!$plaintext) {
                        $codeSnippet .= '</pre>';
                    }
                }
            }
        }
        return $codeSnippet;
    }