becklyn/ddd-generator-bundle
Symfony bundle to generate DDD boilerplate via Maker commands. Installs as a dev dependency and provides abstract makers (DddMaker, entity/test/command variants) plus templating support so you can create and register custom generators.
To begin leveraging becklyn/ddd-generator-bundle in a Laravel project, follow these minimal steps:
Install the Bundle Run this command in your Laravel project directory:
composer require becklyn/ddd-generator-bundle --dev
Enable the Bundle
Add the bundle to config/bundles.php:
return [
// ...
Becklyn\DddGeneratorBundle\BecklynDddGeneratorBundle::class => ['dev' => true],
];
Generate Your First Entity
Use the make:ddd-entity command to scaffold a basic DDD entity:
php artisan make:ddd-entity User
UserManagement) when prompted.src/Domain/UserManagement/Entity/User.php)src/Domain/UserManagement/Repository/UserRepositoryInterface.php)tests/Domain/UserManagement/Entity/UserTest.php)Verify Output Check the generated files for:
\App\Domain\UserManagement\Entity\User).id, createdAt, updatedAt properties).EntityTestTrait).First Use Case: Generate a command and handler for a domain action (e.g., user registration):
php artisan make:ddd-command RegisterUser
UserManagement).src/Domain/UserManagement/Command/RegisterUser.php)src/Domain/UserManagement/Command/Handler/RegisterUserHandler.php)tests/Domain/UserManagement/Command/Handler/RegisterUserHandlerTest.php).Workflow: Use the bundle to generate the core domain layer (entities, repositories, commands) in a structured way:
# Generate an entity with repository
php artisan make:ddd-entity Product --repository
# Generate a command with handler
php artisan make:ddd-command UpdateProductPrice
Integration Tips:
\App\Domain\Ecommerce\Entity\Product).DoctrineRepository or EloquentRepository (if using ORMs) to implement the generated interfaces.becklyn/ddd-event-bundle to generate domain events alongside entities:
php artisan make:ddd-event ProductCreated
Workflow: Generate test classes first, then implement the entity/command:
# Generate tests for an entity
php artisan make:ddd-entity User --test-only
# Implement the entity based on test expectations
php artisan make:ddd-entity User
Patterns:
EntityTestTrait for common assertions (e.g., assertEntityIsCreated()).$repository = $this->createMock(ProductRepositoryInterface::class);
$repository->method('find')->willReturn($product);
$this->entity->setRepository($repository);
CommandHandlerTestTrait provides fixtures for testing command execution:
public function testExecute_createsProduct(): void
{
$this->givenEntityIsCreated(Product::class);
$this->assertCommandExecutesSuccessfully(new UpdateProductPrice(...));
}
Workflow: Extend the bundle to create domain-specific generators (e.g., for value objects or aggregates):
// src/Maker/CustomValueObjectMaker.php
namespace App\Maker;
use Becklyn\DddGeneratorBundle\Maker\DddMaker;
class CustomValueObjectMaker extends DddMaker
{
protected function getTemplatePath(): string
{
return __DIR__.'/../../Resources/skeleton/ddd/value_object.tpl.php';
}
protected function getExtraVariables(): array
{
return array_merge(parent::getExtraVariables(), [
'value_object_namespace' => $this->getNamespace().'\ValueObject',
]);
}
}
Register the Maker:
# config/services.yaml
services:
App\Maker\CustomValueObjectMaker:
tags:
- { name: maker.command, command: 'make:ddd-value-object' }
Usage:
php artisan make:ddd-value-object EmailAddress
Tips:
entity.tpl.php) as a starting point.getDefaultNamespace() to enforce domain-specific paths.Pattern: Automate DDD layer generation in CI/CD pipelines for consistent scaffolding:
# .github/workflows/ddd-scaffold.yml
jobs:
scaffold:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-php@v2
with:
php-version: '8.1'
- run: composer install --dev
- run: |
php artisan make:ddd-entity User --repository
php artisan make:ddd-command RegisterUser
- run: git add .
- run: git commit -m "chore: scaffold DDD layers"
Use Case: Ensure all developers start with a standardized domain layer before feature development.
Patterns:
// app/Providers/AppServiceProvider.php
public function register(): void
{
$this->app->bind(
ProductRepositoryInterface::class,
ProductRepository::class
);
}
// config/console.php
'commands' => [
'make:ddd-entity' => 'make:entity',
'make:ddd-command' => 'make:cmd',
];
Namespace Collisions
User in Auth and UserManagement).\App\Domain\Auth\User, \App\Domain\UserManagement\User).composer dump-autoload if autoloading fails.Template Overrides
src/Resources/skeleton/ddd/ may not override bundle defaults.php artisan cache:clear
php artisan config:clear
PHP 8.0+ Requirements
php.ini or use a .php-version file in the project root.Git Ignore Conflicts
.gitignore:
/src/Domain/**/*Test.php
/tests/Domain/**/*
Command Prompt Hangs
--domain=UserManagement to bypass the prompt:
php artisan make:ddd-entity User --domain=UserManagement
Verbose Output Enable debug mode for generator commands:
php artisan make:ddd-entity User -v
Template Debugging
<?php dump($this); die; ?> to templates to inspect variables.var/dump/ for generated files during development.Service Registration
php artisan debug:makers
src/Resources/skeleton/ddd/ (e.g., entity.tpl.php).<?php if ($extra['soft_deletes'] ?? false): ?>
/**
* @var \DateTime|null
*/
protected ?\DateTimeInterface $deletedAt = null;
<?php endif;
How can I help you explore Laravel packages today?