maxbeckers / php-builder-generator
Generate builder patterns for PHP classes using attributes
Package info
github.com/maxbeckers/php-builder-generator
Type:composer-plugin
pkg:composer/maxbeckers/php-builder-generator
Requires
- php: ^8.2
- composer-plugin-api: ^2.0
- symfony/console: ^7.0|^8.0
- symfony/finder: ^7.0|^8.0
- twig/twig: ^3.0
Requires (Dev)
- composer/composer: ^2.0
- phpunit/phpunit: ^10.0
- symfony/filesystem: ^7.0|^8.0
README
Generate builder patterns for PHP classes — a dev-only dependency.
Features
- 🚀 Dev-only: No production dependency — the library is never needed at runtime
- 🏃 Zero Runtime Overhead: Builders generated at build time, not runtime
- 📝 IDE Friendly: Full autocomplete and type checking support
- 🔧 Highly Configurable: Customize every aspect of generation via a single config file
- 🎯 Type Safe: Preserves all type information from original classes
- 🏗️ Constructor Aware: Intelligently handles constructor parameters
Quick Start
1. Install as dev dependency
composer require --dev maxbeckers/php-builder-generator
2. Allow the Composer plugin
Add to your composer.json:
{
"config": {
"allow-plugins": {
"maxbeckers/php-builder-generator": true
}
}
}
3. Update Autoload for Generated Builders
{
"autoload": {
"psr-4": {
"App\\": ["src/", "generated/php-builder-generator/App/"]
}
}
}
Important: The path must include the namespace (e.g.
.../App/), not just the base output directory.
After updating, run:
composer dump-autoload
4. Create php-builder-generator.php
Add a config file to your project root:
<?php // php-builder-generator.php use MaxBeckers\PhpBuilderGenerator\Config\BuilderConfig; use MaxBeckers\PhpBuilderGenerator\Config\PhpBuilderGeneratorConfig; return PhpBuilderGeneratorConfig::configure() // Option A: explicit per-class config ->class(App\Model\User::class) ->class(App\Model\Company::class, new BuilderConfig(fluent: false, exclude: ['internalId'])) // Option B: scan a whole directory (generates a builder for every class found) ->scanDirectory('src/DTO') ->scanDirectory('src/Model', new BuilderConfig(fluent: true)) // Output settings ->outputDir('generated/php-builder-generator/') ->phpVersion('8.2');
Your production classes need no imports from this library — they stay completely clean:
<?php // src/Model/User.php namespace App\Model; class User { public function __construct( public string $name, public string $email, public ?int $age = null, public array $roles = [] ) {} }
5. Generate & Use
Builders are automatically generated during composer install/update, or run:
./vendor/bin/php-builder-generator
Use your generated builder:
$user = UserBuilder::builder() ->name('John Doe') ->email('john@example.com') ->age(30) ->roles(['admin']) ->build();
Documentation
Quick Links
- Installation & Setup - Detailed installation guide
- Basic Usage - Learn the fundamentals
- Configuration Guide - All configuration options
- Basic Examples - Common use cases
- Contributing - How to contribute
Requirements
- PHP 8.2 or higher
- Composer 2.0 or higher
Show Your Support
If you find this package helpful, I would be happy to get a ⭐ star on GitHub! It helps others discover the project and motivates continued development.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Questions or Issues? Please open an issue on GitHub.
Built with ❤️ for PHP developers