permafrost-dev/php-code-search
Composer 安装命令:
composer require permafrost-dev/php-code-search
包简介
Search PHP code for function & method calls, variable assignments, and more
README 文档
README
Search PHP source code for function & method calls, variable assignments, classes and more directly from PHP.
Installation
composer require permafrost-dev/php-code-search
Searching
To search a file, use the search method. Its only parameter may be either a string containing a valid filename or an instance of \Permafrost\PhpCodeSearch\Support\File.
To search a string instead, use the searchCode method.
The search methods return an instance of Permafrost\PhpCodeSearch\Results\FileSearchResults, which has a results property.
Each result is an instance of Permafrost\PhpCodeSearch\Results\SearchResult with the following properties:
node- the specific item that was foundnode->name(): string
location- the location in the file that the item was foundlocation->startLine(): intlocation->endLine(): int
snippet- a snippet of code lines from the file with the result line in the middlesnippet->toString(): string
file()(method) - provides access to the file that was searched
Searching
To search through the code in a string or file, use the Searcher class:
use Permafrost\PhpCodeSearch\Searcher; $searcher = new Searcher();
To search a file, use the search method, and the searchCode method to search a string of code.
$searcher ->functions(['strtolower', 'strtoupper']) ->search('./file1.php'); $searcher ->variables(['/^one[A-Z]$/']) ->searchCode('<?php $oneA = "1a";');
When searching using any of the available methods, regular expressions can be used by surrounding the name with slashes /, i.e. /test\d+/.
Variable names
To search for variables by name, use the variables method.
$results = $searcher ->variables(['twoA', '/^one.$/']) ->searchCode('<?php '. ' $oneA = "1a";'. ' $oneB = "1b";'. ' $twoA = "2a";'. ' $twoB = "2b";'. ''); foreach($results->results as $result) { echo "Found '{$result->node->name()}' on line {$result->location->startLine}" . PHP_EOL; }
Functions
To search for function calls or definitions, use the functions method.
// search for references AND definitions for 'strtolower' and/or 'myfunc' $searcher ->functions(['strtolower', 'myfunc']) ->search('file1.php');
Method calls
To search for a method call by name, use the methods method.
Method call nodes have an args property that can be looped through to retrieve the arguments for the method call.
$results = $searcher ->methods(['/test(One|Two)/']) ->searchCode('<?php '. ' $obj->testOne("hello world 1"); '. ' $obj->testTwo("hello world", 2); '. '' ); foreach($results->results as $result) { echo "Found '{$result->node->name()}' on line {$result->location->startLine}" . PHP_EOL; foreach($result->node->args as $arg) { echo " argument: '{$arg->value}'" . PHP_EOL; } }
Static calls
To search for static method or property calls, use the static method.
Valid search terms are either a class name like Cache, or a class name and a method name like Cache::remember.
$searcher ->static(['Ray', 'Cache::has', 'Request::$myProperty']) ->search('./app/Http/Controllers/MyController.php');
Classes
To search for either a class definition or a class created by the new keyword, use the classes method.
$searcher ->classes(['MyController']) ->search('./app/Http/Controllers/MyController.php');
Variable assignments
To search for a variable assignment by variable name, use the assignments method. Note: The $ should be omitted.
$searcher ->assignments(['myVar']) ->search('./app/Http/Controllers/MyController.php');
Results without code snippets
To return search results without associated code snippets, use the withoutSnippets method:
$searcher ->withoutSnippets() ->functions(['strtolower']) ->search('file1.php');
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/php-code-search 适用场景与选型建议
permafrost-dev/php-code-search 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 434.72k 次下载、GitHub Stars 达 28, 最近一次更新时间为 2021 年 07 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「search」 「code」 「permafrost」 「sourcecode」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 permafrost-dev/php-code-search 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 permafrost-dev/php-code-search 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 permafrost-dev/php-code-search 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Memio's PrettyPrinter, used to generate PHP code from given Model
A Laravel package to retrieve data from Google Search Console
Indexed Search Autocomplete - Extends the TYPO3 Core Extension Indexed_Search searchform with an autocomplete feature.
Abstraction Layer to index and search entities
Promotional Codes Generator for Laravel 5.1
Pimcore 10.x Website Indexer (powered by Zend Search Lucene)
统计信息
- 总下载量: 434.72k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 28
- 点击次数: 17
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-07-04