Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/permafrost-dev/code-snippets
Easily create and work with code snippets from PHP
https://github.com/permafrost-dev/code-snippets
code-snippets permafrost snippets
Last synced: 18 days ago
JSON representation
Easily create and work with code snippets from PHP
- Host: GitHub
- URL: https://github.com/permafrost-dev/code-snippets
- Owner: permafrost-dev
- License: mit
- Created: 2021-07-25T05:59:53.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-11-18T02:52:34.000Z (about 2 months ago)
- Last Synced: 2024-12-06T15:52:41.937Z (about 1 month ago)
- Topics: code-snippets, permafrost, snippets
- Language: PHP
- Homepage:
- Size: 101 KB
- Stars: 14
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: .github/CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
- Security: .github/SECURITY.md
Awesome Lists containing this project
README
# code-snippets
Easily create and work with code snippets from source code files of any type in PHP.
_The original code this package is based on was borrowed from the [`spatie/backtrace`](https://github.com/spatie/backtrace) package._
## Installation
You can install the package via composer:
```bash
composer require permafrost-dev/code-snippets
```## Usage
_Note: Although the examples here reference php files, any file type can be used when creating a `CodeSnippet`._
### Creating a snippet
Use the `surroundingLine($num)` method to select the "target" line, which will be returned as the middle line of the snippet:
```php
use Permafrost\CodeSnippets\CodeSnippet;$snippet = (new CodeSnippet())
->surroundingLine(4)
->snippetLineCount(6)
->fromFile('/path/to/a/file.php');
```Use the `surroundingLines($first, $last)` method to select a range of "target" lines, which will be returned as the middle lines of the snippet:
```php
$snippet = (new CodeSnippet())
->surroundingLines(4, 7)
->snippetLineCount(6)
->fromFile('/path/to/a/file.php');
```Use the `linesBefore()` and `linesAfter()` methods to specify the number of context lines to display before and after the "target" lines:
```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');
```### Getting the snippet contents
The `getLines()` method returns an array of `SnippetLine` instances. The keys of the resulting array are the line numbers.
The `SnippetLine` instances may be cast to strings to display the value. When working with `SnippetLine` instances, use `isSelected()` to determine if the line was selected using either the `surroundingLine()` or `surroundingLines()` method on the `CodeSnippet` instance.
To get the value of a `SnippetLine`, use the `value()` method or cast the object to a string.
```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 line count
To determine the number of lines in the snippet, use the `getSnippetLineCount()` method:
```php
$snippet = (new CodeSnippet())
->surroundingLines(4, 7)
->linesBefore(3)
->linesAfter(3)
->fromFile('/path/to/a/file.php');
echo "Snippet line count: " . $snippet->getSnippetLineCount() . PHP_EOL;
```You can also use `count()` on the result of the `getLines()` method:
```php
$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;
```To return an array containing the line numbers for the snippet, use `getLineNumbers()`:
```php
print_r($snippet->getLineNumbers());
```### Returning the snippet as a string
Return the contents of the snippet as as string using the `toString()` method or by casting the snippet to a string directly:
```php
$snippet = (new CodeSnippet())
->surroundingLines(4, 7)
->linesBefore(3)
->linesAfter(3)
->fromFile('/path/to/a/file.php');
echo "Snippet: \n" . $snippet->toString() . PHP_EOL;
```## Testing
```bash
./vendor/bin/phpunit
```## Changelog
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
## Contributing
Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.
## Security Vulnerabilities
Please review [our security policy](../../security/policy) on how to report security vulnerabilities.
## Credits
- [Patrick Organ](https://github.com/patinthehat)
- [All Contributors](../../contributors)## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.