PHP code example of permafrost-dev / code-snippets

1. Go to this page and download the library: Download permafrost-dev/code-snippets library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

permafrost-dev / code-snippets example snippets


use Permafrost\CodeSnippets\CodeSnippet;

$snippet = (new CodeSnippet())
    ->surroundingLine(4)
    ->snippetLineCount(6)
    ->fromFile('/path/to/a/file.php');

$snippet = (new CodeSnippet())
    ->surroundingLines(4, 7)
    ->snippetLineCount(6)
    ->fromFile('/path/to/a/file.php');

// the "target" line isn't displayed in the middle, but as the second line
$snippet = (new CodeSnippet())
    ->surroundingLine(4)
    ->linesBefore(1)
    ->linesAfter(3)
    ->fromFile('/path/to/a/file.php');

$snippet = (new CodeSnippet())
    ->surroundingLine(4)
    ->snippetLineCount(5)
    ->fromFile('/path/to/a/file.php');
    
foreach($snippet->getLines() as $lineNum => $line) {
    $prefix = $line->isSelected() ? ' * ' : '   ';
    
    echo $prefix . $line->lineNumber() . ' - ' . $line->value() . PHP_EOL;
}

$snippet = (new CodeSnippet())
    ->surroundingLines(4, 7)
    ->linesBefore(3)
    ->linesAfter(3)
    ->fromFile('/path/to/a/file.php');
    
echo "Snippet line count: " . $snippet->getSnippetLineCount() . PHP_EOL;

$snippet = (new CodeSnippet())
    ->surroundingLines(4, 7)
    ->linesBefore(3)
    ->linesAfter(3)
    ->fromFile('/path/to/a/file.php');
    
echo "Snippet line count: " . count($snippet->getLines()) . PHP_EOL;

print_r($snippet->getLineNumbers());

$snippet = (new CodeSnippet())
    ->surroundingLines(4, 7)
    ->linesBefore(3)
    ->linesAfter(3)
    ->fromFile('/path/to/a/file.php');
    
echo "Snippet: \n" . $snippet->toString() . PHP_EOL;