ajthinking/archetype
Composer 安装命令:
composer require ajthinking/archetype
包简介
Programmatically edit PHP and Laravel files.
README 文档
README
Enabling Rapid-Application-Development-tools, PR-bots, code analyzers and other things
- Programatically modify php files with an intuitive top level read/write API
- Read/write on classes, framework- and language constructs using
FileQueryBuildersandAbstractSyntaxTreeQueryBuilders
Getting started
composer require ajthinking/archetype
That's it! Check out introduction of concepts below or review the API examples
PHPFile read/write API
use Archetype\Facades\PHPFile; // Create new files PHPFile::make()->class(\Acme\Product::class) ->use('Shippable') ->public()->property('stock', -1) ->save();
// Modify existing files PHPFile::load(\App\Models\User::class) ->className('NewClassName') ->save();
LaravelFile read/write API
use Archetype\Facades\LaravelFile; // extends PHPFile // Expanding on our User model LaravelFile::user() ->add()->use(['App\Traits\Dumpable', 'App\Contracts\PlayerInterface']) ->add()->implements('PlayerInterface') ->table('gdpr_users') ->add()->fillable('nickname') ->remove()->hidden() ->empty()->casts() ->hasMany('App\Game') ->belongsTo('App\Guild') ->save() ->render();
Show output
<?php namespace App\Models; use App\Contracts\PlayerInterface; use App\Traits\Dumpable; use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use Laravel\Sanctum\HasApiTokens; class User extends Authenticatable implements PlayerInterface { use HasApiTokens, HasFactory, Notifiable; protected $table = 'gdpr_users'; /** * The attributes that are mass assignable. * * @var array<int, string> */ protected $fillable = [ 'name', 'email', 'password', 'nickname', ]; /** * The attributes that should be cast. * * @var array<string, string> */ protected $casts = []; /** * Get the associated Guild */ public function guild() { return $this->belongsTo(Guild::class); } /** * Get the associated Games */ public function games() { return $this->hasMany(Game::class); } }
File QueryBuilders
Filter and retrieve a set of files to interact with.
// find files with the query builder PHPFile::in('database/migrations') ->where('extends', 'Migration') ->andWhere('className', 'like', 'Create') ->get() // returns Collection of PHPFiles // Quickly find the Laravel User file $file = LaravelFile::user(); // Quickly find Laravel specific files LaravelFile::models()->get(); LaravelFile::controllers()->get(); LaravelFile::serviceProviders()->get(); // ...
Abstract Syntax Tree QueryBuilder
As seen in the previous examples we can query and manipulate nodes with simple or primitive values, such as strings and arrays. However, if we want to perform custom or more in dept queries we must use the ASTQueryBuilder.
Example: how can we fetch explicit column names in a migration file?
LaravelFile::load('database/migrations/2014_10_12_000000_create_users_table.php') ->astQuery() // get a ASTQueryBuilder ->classMethod() ->where('name->name', 'up') ->staticCall() ->where('class', 'Schema') ->where('name->name', 'create') ->args ->closure() ->stmts ->methodCall() ->where('var->name', 'table') ->args ->value ->value ->get();
The ASTQueryBuilder examines all possible paths and automatically terminates those that cant complete the query:
The ASTQueryBuilder relies entirely on nikic/php-parser. Available query methods mirror the PhpParser types and properties. To understand this syntax better you may want to tinker with dd($file->ast()) while building your queries. Basic conventions are listed below.
- Traverse into nodes by using methods (
method(),staticCall()...) - Traverse into node properties by accessing properties (
args,stmts...) - Filter results with
where(...) - Resolving matching paths with
get()
ASTQueryBuilderalso supports removing, replacing and injecting nodes 🔧
// Replace a node property $file->astQuery() ->class() ->name ->replaceProperty('name', $newClassName) ->commit() // updates the file's AST ->end() // exit query ->save()
Errors 😵
If a file can't be parsed, a FileParseError will be thrown. This can happen if you try to explicitly load a broken file but also when performing queries matching one or more problematic files.
To see all offending files run php artisan archetype:errors. To ignore files with problems, put them in config/archetype.php -> ignored_paths.
Configuration
php artisan vendor:publish --provider="Archetype\ServiceProvider"
Requirmenst
- UNIX filesystem
- PHP >= 7.4
- Laravel >= 7
Contributing
PRs and issues are welcome 🙏 Feel free to take a stab at an incomplete test.
Development installation
git clone git@github.com:ajthinking/archetype.git
cd archetype
composer install
./vendor/bin/pest
License
MIT
Acknowledgements
- Built with nikic/php-parser
- PSR Printing fixes borrowed from tcopestake/PHP-Parser-PSR-2-pretty-printer
Like this package?
ajthinking/archetype 适用场景与选型建议
ajthinking/archetype 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.91M 次下载、GitHub Stars 达 272, 最近一次更新时间为 2020 年 05 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「laravel」 「ast」 「abstract syntax tree」 「php-parser」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ajthinking/archetype 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ajthinking/archetype 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ajthinking/archetype 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Editable JSON AST with visitor traversal and formatting-preserving printing.
Parse Handlebars templates to a spec-compliant AST with PHP.
An AST nodes of the TypeLang
Alfabank REST API integration
Programmatically edit PHP and Laravel files.
Framework for building BNF LR(1) parsers
统计信息
- 总下载量: 3.91M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 272
- 点击次数: 31
- 依赖项目数: 10
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2020-05-23

