PHP Builder Generator offers extensive configuration options to customize how builders are generated.
Add configuration to your composer.json
under the extra
section:
{
"extra": {
"php-builder-generator": {
"src-dirs": ["src", "app"],
"output-dir": "generated/php-builder-generator/",
"namespace-suffix": "\\Builder",
"php-version": "8.2",
"auto-generate": true,
"generator-config": {}
}
}
}
Option | Type | Default | Description |
---|---|---|---|
src-dirs |
array |
["src"] |
Directories to scan for classes with Builder attributes |
output-dir |
string |
"generated/php-builder-generator/" |
Directory where generated builders will be saved |
namespace-suffix |
string |
"" |
Suffix to append to the original namespace for generated classes |
php-version |
string |
"8.2" |
PHP version to target (currently only 8.2 supported) |
auto-generate |
bool |
true |
Whether to automatically generate during composer install/update |
generator-config |
array |
{} |
Additional configuration for specific generators |
Specify which directories to scan for classes:
{
"extra": {
"php-builder-generator": {
"src-dirs": ["src", "app", "lib/models"]
}
}
}
Control where generated files are placed:
{
"extra": {
"php-builder-generator": {
"output-dir": "build/builders/"
}
}
}
Note: The output directory should typically be in generated/
. You can either add it to .gitignore
and always generate the builders or add them to git.
Add a suffix to generated builder namespaces:
{
"extra": {
"php-builder-generator": {
"namespace-suffix": "\\Builder"
}
}
}
For a class App\Model\User
, this generates App\Model\Builder\UserBuilder
.
{
"extra": {
"php-builder-generator": {
"namespace-suffix": ""
}
}
}
This generates App\Model\UserBuilder
in the same namespace as the original class.
The #[Builder]
attribute provides fine-grained control over individual builders:
#[Builder(
className: 'CustomUserBuilder',
namespace: 'App\\Builders',
fluent: true,
exclude: ['password'],
include: ['name', 'email'],
builderMethod: 'create'
)]
class User { ... }
Option | Type | Default | Description |
---|---|---|---|
className |
?string |
null |
Custom name for the generated builder class |
namespace |
?string |
null |
Custom namespace for the generated builder |
fluent |
bool |
true |
Whether setter methods should return self for chaining |
exclude |
array |
[] |
Property names to exclude from the builder |
include |
array |
[] |
Only include these properties (if set, overrides exclude) |
builderMethod |
string |
'builder' |
Name of the static factory method |
#[Builder(
className: 'UserFactory',
namespace: 'App\\Factories'
)]
class User
{
// ...
}
// Usage:
$user = \App\Factories\UserFactory::builder()
->name('John')
->build();
#[Builder(exclude: ['password', 'apiKey'])]
class User
{
public string $name;
public string $email;
public string $password; // Won't have setter
public string $apiKey; // Won't have setter
}
#[Builder(include: ['name', 'email'])]
class User
{
public string $name; // Will have setter
public string $email; // Will have setter
public string $password; // Won't have setter
public string $internal; // Won't have setter
}
#[Builder(fluent: false)]
class User
{
public string $name;
}
// Generated methods return void:
$builder = UserBuilder::builder();
$builder->name('John'); // Returns void
$user = $builder->build();
#[Builder(builderMethod: 'create')]
class User
{
public string $name;
}
// Usage:
$user = UserBuilder::create() // Instead of ::builder()
->name('John')
->build();
The generator-config
section allows for future extensibility:
{
"extra": {
"php-builder-generator": {
"generator-config": {
"template-customizations": {},
"future-features": {}
}
}
}
}
project/
├── src/
│ └── Model/
│ └── User.php # Original class
└── generated/php-builder-generator/
└── App/
└── Model/
└── Builder/ # Namespace suffix
└── UserBuilder.php # Generated builder
project/
├── src/
│ └── Model/
│ └── User.php # Original class
└── generated/php-builder-generator/
└── App/
└── Model/
└── UserBuilder.php # Generated builder
For production environments where you don’t want automatic generation:
{
"extra": {
"php-builder-generator": {
"auto-generate": false
}
}
}
Then generate manually when needed:
./vendor/bin/php-builder-generator
You might want different configurations for different environments. Consider using Composer scripts or environment variables to manage this.
auto-generate
is true
and the plugin is allowednamespace-suffix
and custom namespace
in attributesinclude
and exclude
attribute optionssrc-dirs
includes the correct directoriesThe configuration is validated when the plugin runs. Invalid configurations will show clear error messages indicating what needs to be fixed.
Next: Basic Examples