Two Laravel devs that won't disappear on you. Finally! Hire Joel and Aaron from No Compromises.

Archetype

ajthinking/archetype image

Archetype stats

Downloads
1.1M
Stars
237
Open Issues
2
Forks
17

View on GitHub →

Programmatically edit PHP and Laravel files.

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 and AbstractSyntaxTreeQueryBuilders

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 :wrench:

// 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 :pray: 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

Like this package?

Star it :star:

Say hi: @ajthinking :gem:

Github Sponsors :octocat::heart:

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Ajthinking Archetype Related Articles

AI Generative Engine Optimization for Laravel image

AI Generative Engine Optimization for Laravel

Read article
Prism Workers AI — A Cloudflare Workers AI Provider for Prism PHP image

Prism Workers AI — A Cloudflare Workers AI Provider for Prism PHP

Read article
Claude Opus 4.6 adds adaptive thinking, 128K output, compaction API, and more image

Claude Opus 4.6 adds adaptive thinking, 128K output, compaction API, and more

Read article
Everything new in Livewire 4 image

Everything new in Livewire 4

Read article
PHP SDK for Anthropic Claude image

PHP SDK for Anthropic Claude

Read article
PostgreSQL vs. MongoDB for Laravel: Choosing the Right Database image

PostgreSQL vs. MongoDB for Laravel: Choosing the Right Database

Read article
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
Curotec logo

Curotec

World class Laravel experts with GenAI dev skills. LATAM-based, embedded engineers that ship fast, communicate clearly, and elevate your product. No bloat, no BS.

Curotec
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Blastup logo

Blastup

Blastup provides social media enhancement services including buying Instagram likes, followers, and views, with features like instant delivery and a variety of packages to suit different needs.

Blastup
Celebian logo

Celebian

Celebian is a social media marketing agency specializing in helping their clients go viral on TikTok. Whether you're looking to reach a bigger audience or gain more Tiktok followers, likes, and views, they've got you covered.

Celebian