Provides implementation patterns for Clean Architecture, Hexagonal Architecture (Ports & Adapters), and Domain-Driven Design in PHP 8.3+ with Symfony 7.x. Use when architecting enterprise PHP applications with entities/value objects/aggregates, refactoring legacy code to modern patterns, implementing domain-driven design with Symfony, or creating testable backends with clear separation of concerns.
Instalar com Codex ou Claude Copie este prompt, cole no Codex, Claude ou outro assistente e deixe que ele revise a página da skill e instale para você.
Um comando direto ignora o prompt de revisão. Verifique a origem antes de executá-lo.
Provides implementation patterns for Clean Architecture, Hexagonal Architecture (Ports & Adapters), and Domain-Driven Design in PHP 8.3+ with Symfony 7.x. Use when architecting enterprise PHP applications with entities/value objects/aggregates, refactoring legacy code to modern patterns, implementing domain-driven design with Symfony, or creating testable backends with clear separation of concerns.
allowed-tools
Read, Write, Bash, Edit, Glob, Grep
Clean Architecture, Hexagonal Architecture & DDD for PHP/Symfony
Overview
This skill provides guidance for implementing Clean Architecture, Hexagonal Architecture (Ports & Adapters), and Domain-Driven Design patterns in PHP 8.3+ applications using Symfony 7.x. It ensures clear separation of concerns, framework-independent business logic, and highly testable code through layered architecture with inward-only dependencies.
When to Use
Architecting new enterprise PHP applications with Symfony 7.x
Refactoring legacy PHP code to modern, testable patterns
Implementing Domain-Driven Design in PHP projects
Creating maintainable applications with clear separation of concerns
Building testable business logic independent of frameworks
Designing modular PHP systems with swappable infrastructure
Instructions
1. Understand the Architecture Layers
Clean Architecture follows the dependency rule: dependencies only point inward.
# config/services.yamlservices:_defaults:autowire:trueautoconfigure:trueApp\:resource:'../src/'exclude:-'../src/Domain/Entity/'-'../src/Kernel.php'# Repository binding - Port to AdapterApp\Domain\Repository\UserRepositoryInterface:class:App\Adapter\Persistence\Doctrine\Repository\DoctrineUserRepository# In-memory repository for testsApp\Domain\Repository\UserRepositoryInterface$inMemoryUserRepository:class:App\Tests\Infrastructure\Repository\InMemoryUserRepository
Dependency Rule: Dependencies only point inward - domain knows nothing of application or infrastructure
Immutability: Value Objects MUST be immutable using readonly in PHP 8.1+ - never allow mutable state
Rich Domain Models: Put business logic in entities with factory methods like create() - avoid anemic models
Interface Segregation: Keep repository interfaces small and focused - do not create god interfaces
Framework Independence: Domain and application layers MUST be testable without Symfony or Doctrine
Validation at Construction: Validate in Value Objects at construction time - never allow invalid state
Symfony Attributes: Use PHP 8 attributes for routing (#[Route]), validation (#[Assert\]), and DI
Test Doubles: Always provide In-Memory implementations for repositories to enable fast unit tests
Domain Events: Dispatch domain events to decouple side effects - do not call external services from entities
XML/YAML Mappings: Use XML or YAML for Doctrine mappings instead of annotations in domain entities
Constraints and Warnings
Architecture Constraints
Dependency Rule: Dependencies only point inward. Domain knows nothing of Application, Application knows nothing of Infrastructure. Violating this breaks the architecture.
No Anemic Domain: Entities should encapsulate behavior, not just be data bags. Avoid getters/setters without business logic.
Interface Segregation: Keep repository interfaces small and focused. Do not create god interfaces.
PHP Implementation Constraints
Immutability: Value Objects MUST be immutable using readonly in PHP 8.1+. Never allow mutable state in Value Objects.
Validation: Validate in Value Objects at construction time. Never allow invalid state to exist.
Symfony Attributes: Use PHP 8 attributes for routing, validation, and DI (#[Route], #[Assert\Email], #[Autowire]).
Testing Constraints
Framework Independence: Domain and Application layers MUST be testable without Symfony, Doctrine, or database.
Test Doubles: Always provide In-Memory implementations for repository interfaces to enable fast unit tests.
Warnings
Avoid Rich Domain Models in Controllers: Controllers should only coordinate, not contain business logic.
Beware of Leaky Abstractions: Infrastructure concerns (like Doctrine annotations) should not leak into Domain entities. Use XML/YAML mappings instead.
Command Bus Consideration: For complex applications, use Symfony Messenger for async processing. Do not inline complex orchestrations in handlers.
Domain Events: Dispatch domain events to decouple side effects from core business logic. Do not call external services directly from entities.