appique / laravel-class-generator
Class generator with customizable stubs and placeholders
Requires
- php: ^8.3
- illuminate/contracts: ^11.0||^12.0
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1
- orchestra/testbench: ^10.0.0||^9.0.0
- pestphp/pest: ^3.0
- pestphp/pest-plugin-arch: ^3.0
- pestphp/pest-plugin-laravel: ^3.0
- phpstan/extension-installer: ^1.3
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-phpunit: ^2.0
- spatie/laravel-ray: ^1.35
README
Introduction
A lightweight Laravel package that simplifies class generation from stub files with dynamic placeholder replacement.
Installation
You can install the package via composer:
composer require appique/laravel-class-generator
Usage
Stub Files
First, create stub files in your project's stubs/
directory. Stub files are templates that use double curly braces for placeholders:
// stubs/service.stub <?php namespace {{ namespace }}; use App\Models\{{ model }}; use App\Repositories\{{ repository }}; class {{ class }} { public function __construct( private {{ repository }} $repository ) {} public function process({{ model }} $model): void { // Implementation } }
The following placeholders are always available:
{{ namespace }}
- The class namespace{{ class }}
- The class name{{ fqcn }}
- Fully qualified class name
You can add any custom placeholders you need, and pass their values when generating classes.
Basic Usage
Once you have your stub files, generate classes from them:
use Appique\ClassGenerator\Console\ClassGenerator; // Create a service class in the App namespace ClassGenerator::make(className: 'UserService', stub: 'service') ->inAppNamespace('Services') ->placeholders([ 'model' => 'User', 'repository' => 'UserRepository' ]) ->create();
This will:
- Load the
stubs/service.stub
file - Replace all placeholders with provided values
- Create
app/Services/UserService.php
with the generated content
Working with Different Namespaces
// Create a test class ClassGenerator::make(className: 'UserServiceTest', stub: 'test') ->inTestNamespace('Unit', 'Services') ->placeholders(['class_under_test' => 'UserService']) ->create(); // Creates: tests/Unit/Services/UserServiceTest.php // Create a factory class ClassGenerator::make(className: 'ProductFactory', stub: 'model-factory') ->inFactoriesNamespace() ->placeholders(['model' => 'Product']) ->create(); // Creates: database/factories/ProductFactory.php // Create a class with custom namespace ClassGenerator::make(className: 'Services\\OrderProcessor', stub: 'service') ->placeholders(['author' => 'John Doe']) ->create(); // Creates: src/Services/OrderProcessor.php
Additional Options
// Force overwrite existing files ClassGenerator::make(className: 'ExistingClass', stub: 'class') ->inAppNamespace() ->create(force: true); // Execute callback after class generation ClassGenerator::make(className: 'MyClass', stub: 'class') ->inAppNamespace() ->create(callback: fn(string $path) => Process::run("vendor/bin/pint {$path}");
Stub Resolution
The package looks for stub files in the following order:
- Published stubs:
base_path('stubs/your-stub.stub')
- Package stubs: Auto-detected from vendor packages
- Custom fallback location (if specified)
Using in Your Own Package
When using this generator in your own package, it automatically detects the package's stubs directory:
// In your package's command or service class namespace YourVendor\YourPackage\Commands; use Appique\ClassGenerator\Console\ClassGenerator; class MakeModelCommand extends Command { public function handle() { // Automatically looks in vendor/your-vendor/your-package/stubs/ ClassGenerator::make($this->argument('name'), 'model') ->inAppNamespace('Models') ->placeholders([ 'table' => Str::snake(Str::plural($this->argument('name'))) ]) ->create(); } }
Testing
Run the test suite:
composer test
Run tests with coverage:
composer test-coverage
Run static analysis:
composer analyse
Fix code style:
composer format
Run all checks in one go:
composer check
Changelog
Please see CHANGELOG for more information on recent changes.
Contributing
Contributions are welcome! Please see CONTRIBUTING for details.
Development Setup
- Fork the repository
- Clone your fork
- Install dependencies:
composer install
- Create a feature branch
- Make your changes
- Run checks:
composer check
(formats code, runs static analysis, and tests) - Submit a pull request
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.
Acknowledgments
This package was inspired by the Laravel community, particularly:
- Spatie for their excellent Laravel packages and Package Skeleton
- The Laravel framework team for creating this amazing ecosystem