ajthinking / archetype
通过编程方式编辑PHP和Laravel文件。
v2.0.0
2024-05-11 08:05 UTC
Requires
- nikic/php-parser: ^5.0
Requires (Dev)
- laravel/laravel: ^6.0 || ^7.0 || ^8.0 || ^9.0
- orchestra/testbench: ^4.0 || ^5.0 || ^6.0
- pestphp/pest: ^1.21
- phpstan/phpstan: ^1.6
- phpunit/phpunit: ^8.0 || ^9.5
- dev-master
- v2.0.0
- v1.1.5
- v1.1.4
- v1.1.3
- v1.1.2
- v1.1.1
- v1.1.0
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- v0.2.8
- v0.2.7
- v0.2.6
- v0.2.5
- v0.2.4
- v0.2.3
- v0.2.2
- v0.2.1
- v0.1.3
- v0.1.2
- v0.1.1
- v0.1.0
- v0.0.18
- v0.0.17
- v0.0.16
- v0.0.15
- v0.0.14
- v0.0.13
- v0.0.12
- v0.0.11
- v0.0.10
- v0.0.9
- v0.0.8
- v0.0.7
- v0.0.6
- v0.0.5
- v0.0.4
- v0.0.3
- v0.0.1
- dev-add-use-trait-stuff
- dev-add-insert-stmt-closure-with-builder
- dev-fix-ast-insert-bug
- dev-improve-formatting
- dev-cleanups2
- dev-with-each
- dev-functional
- dev-di-for-input-and-output
- dev-concrete-phpparser-class-methods
- dev-fix-unstrict-comparison
- dev-larastan
- dev-refactor-to-dependency-injection
- dev-remove-unimplemented-methods
- dev-separate-maker
- dev-refactor-to-explicit-endpoints
- dev-avoid-str-macros
- dev-method-call-chain
- dev-laravel-9-support
- dev-call-chains-demo
- dev-1-x
- dev-readme-and-docs-testing
- dev-readme-experiment-2
- dev-testable-file-query-builder
- dev-top-api-review-3
- dev-review-top-level-api
- dev-add-args-to-class-traverse
- dev-improve-test-structure
- dev-auto-resolve-where-clauses
- dev-add-testable-ast-query-builder
- dev-ast-visualizer
- dev-remove-half-finished-things
- dev-fix-absolute-root
- dev-assert-spaced-class-stmts
- dev-assert-valid-php
- dev-scrap-doc-tool
- dev-improve-class-map
- dev-remove-ast-query-aliases
- dev-testable-phpfile
- dev-use-pest
- dev-improve-const
- dev-drop-schema-feature
- dev-fix-multiline-arrays
- dev-cleanups
- dev-update-deps-and-tests
This package is auto-updated.
Last update: 2024-09-11 08:58:39 UTC
README
启用快速应用程序开发工具、PR机器人、代码分析器和其他功能
- 使用直观的顶级读写API编程修改PHP文件
- 使用
FileQueryBuilders
和AbstractSyntaxTreeQueryBuilders
在类、框架和语言构造上读写
入门指南
composer require ajthinking/archetype
就这些!查看以下概念介绍或查看API示例
PHPFile
读写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
读写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();
显示输出
<?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); } }
文件查询构建器
过滤和检索一组文件进行交互。
// 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(); // ...
抽象语法树查询构建器
如前所述示例,我们可以使用简单的或原始值(如 字符串 和 数组)查询和操作节点。但是,如果我们想执行自定义或更深入的查询,我们必须使用 ASTQueryBuilder
。
示例:我们如何在迁移文件中获取显式列名?
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();
ASTQueryBuilder检查所有可能的路径,并自动终止无法完成查询的路径
ASTQueryBuilder完全依赖于 nikic/php-parser。可用的查询方法与 PhpParser
类型属性相对应。为了更好地理解此语法,您可能需要在构建查询时尝试使用 dd($file->ast())
。以下列出了一些基本约定。
- 使用方法(
method()
、staticCall()
...)遍历到 节点 - 通过访问属性(
args
、stmts
...)遍历到 节点属性 - 使用
where(...)
过滤结果 - 使用
get()
解决匹配路径
ASTQueryBuilder
还支持 删除、替换 和 注入 节点 🔧
// Replace a node property $file->astQuery() ->class() ->name ->replaceProperty('name', $newClassName) ->commit() // updates the file's AST ->end() // exit query ->save()
错误 😵
如果无法解析文件,将抛出 FileParseError
。这可能会发生如果您尝试显式加载一个损坏的文件 但也会 当执行匹配一个或多个问题文件的查询时。
要查看所有 有问题 的文件,请运行 php artisan archetype:errors
。要忽略有问题的文件,请将它们放入 config/archetype.php
-> ignored_paths
。
配置
php artisan vendor:publish --provider="Archetype\ServiceProvider"
要求
- UNIX 文件系统
- PHP >= 7.4
- Laravel >= 7
贡献
欢迎提交PR和问题 🙏 随意尝试一个 不完整的测试。
开发安装
git clone [email protected]:ajthinking/archetype.git
cd archetype
composer install
./vendor/bin/pest
许可
MIT
鸣谢
- 使用 nikic/php-parser 构建
- PSR 打印修复借用自 tcopestake/PHP-Parser-PSR-2-pretty-printer