How oscarroman1230/php-ddd-user works
Project Type & Purpose
• This is a backend PHP application to manage user registration. It uses DDD and Clean Architecture to separate concerns between the Application, Domain, and Infrastructure layers.
• It uses Doctrine ORM for persistence with MySQL, exposes an API endpoint for user registration, and is containerized using Docker (with Docker Compose), running on Apache.
Break Down the File Structure
• Top-level configuration files (Dockerfile, docker-compose.yml, .env) indicate a containerized deployment and environment variable management.
• The “public” directory contains the web-accessible entry point (index.php) and .htaccess for Apache, setting up the API endpoint to receive HTTP requests.
• The “src” directory is organized to reflect Clean Architecture:
• The Application layer (src/Application) holds use cases like RegisterUser, handling input and application logic, including the RegisterUserUseCase and its Request model.
• The Domain layer (src/Domain) encapsulates business entities (User, Email, Name, Password, UserId), domain events (UserRegisteredEvent), and repository interfaces, ensuring core business rules are independent of external frameworks.
• The Infrastructure layer (src/Infrastructure) integrates with external systems: Controllers (e.g., RegisterUserController), event dispatching (EventDispatcher and SendWelcomeEmailListener), middleware for exception handling, and the actual implementations for persistence (DoctrineUserRepository).
• The “tests” directory includes both unit and integration tests to verify correct behavior for use cases and repository implementations.
Main Components & Their Interactions
• API Layer: The public/index.php entry point routes HTTP requests to the relevant Infrastructure Controllers, such as RegisterUserController.
• Application Layer: The controller invokes the RegisterUserUseCase, which handles the business logic for user registration.
• Domain Layer: The use case communicates with Domain entities (User, Email, etc.) and domain events (like UserRegisteredEvent) to enforce business rules.
• Persistence Layer: The Domain’s persistence abstraction (UserRepositoryInterface) is implemented by the DoctrineUserRepository in the Infrastructure. When a user is registered, this repository uses Doctrine ORM to interact with the MySQL database.
• Event Handling: Once an event is triggered (like a user registration event), the event dispatcher and linked listeners (such as SendWelcomeEmailListener) handle domain events, which can trigger further processes (like sending welcome emails).
Architectural Patterns
• DDD & Clean Architecture: Clear separation of concerns – with the Domain and Application layers isolated from external frameworks through interfaces and dependency injection.
• The use of Doctrine ORM illustrates the Repository pattern for persistence, and the implementation of clear boundaries (e.g., the infrastructure layer only implements what the domain requires).
• API endpoints are organized in a layered approach, with the “Controller” in Infrastructure managing incoming requests and delegating to Application services.