ajthinking / archetype
Programmatically edit PHP and Laravel files.
Installs: 1 818 544
Dependents: 10
Suggesters: 0
Security: 0
Stars: 255
Watchers: 4
Forks: 19
Open Issues: 2
Type:package
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-10-11 09:12:47 UTC
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
FileQueryBuilders
andAbstractSyntaxTreeQueryBuilders
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()
ASTQueryBuilder
also 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