PHP code example of ajthinking / php-file-manipulator

1. Go to this page and download the library: Download ajthinking/php-file-manipulator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ajthinking / php-file-manipulator example snippets


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();
 example
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();



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();
// ...

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();

// Replace a node property
$file->astQuery()
    ->class()
    ->name
    ->replaceProperty('name', $newClassName)
    ->commit() // updates the file's AST
    ->end() // exit query
    ->save() 
bash
php artisan vendor:publish --provider="Archetype\ServiceProvider"