permafrost-dev/code-snippets
Composer 安装命令:
composer require permafrost-dev/code-snippets
包简介
Easily work with code snippets in PHP
README 文档
README
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 package.
Installation
You can install the package via composer:
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:
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:
$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:
// 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.
$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:
$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:
$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():
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:
$snippet = (new CodeSnippet()) ->surroundingLines(4, 7) ->linesBefore(3) ->linesAfter(3) ->fromFile('/path/to/a/file.php'); echo "Snippet: \n" . $snippet->toString() . PHP_EOL;
Testing
./vendor/bin/phpunit
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.
permafrost-dev/code-snippets 适用场景与选型建议
permafrost-dev/code-snippets 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 431.02k 次下载、GitHub Stars 达 14, 最近一次更新时间为 2021 年 07 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「code」 「snippets」 「permafrost」 「code-snippets」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 permafrost-dev/code-snippets 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 permafrost-dev/code-snippets 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 permafrost-dev/code-snippets 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Search PHP code for function & method calls, variable assignments, and more
Quickly scan source code for calls to Ray
Memio's PrettyPrinter, used to generate PHP code from given Model
Check a project's code coverage, optionally enforcing a minimum value
Promotional Codes Generator for Laravel 5.1
Heroicons as snippets for Kirby CMS
统计信息
- 总下载量: 431.02k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 14
- 点击次数: 26
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-07-25