# shahghasiadil/laravel-clean-architecture-ddd-cqrs architecture

> Architecture diagram of the GitHub repository shahghasiadil/laravel-clean-architecture-ddd-cqrs, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-04). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/shahghasiadil/laravel-clean-architecture-ddd-cqrs
- Repository: https://github.com/shahghasiadil/laravel-clean-architecture-ddd-cqrs

## Overview

##### Main Architectural Components
- Application Layer – This is mainly represented in the “src/Application” directory. It contains the command and query buses (for dispatching commands and queries), service providers, and user-specific business logic (commands, queries, command handlers, services, and data transfer objects).
- Domain Layer – Found under “src/Domain”, it represents your core business logic, including entities, domain events, custom exceptions, policies, and repositories interfaces. This is where your DDD principles shine with a focus on encapsulating business rules.
- Infrastructure Layer – In “src/Infrastructure”, this layer provides concrete implementations for domain interfaces (e.g., repositories), deals with persistence, external systems, asynchronous jobs, and notifications. This layer connects the core domain logic with real-world concerns.
- Presentation Layer – Located within “src/Presentation”, it includes controllers, middleware, request validations, resource transformers, and routes (both web and API). It is responsible for handling user requests and formatting responses.
- Shared Components – The “src/Shared” directory encapsulates contracts, common enums, and traits used across multiple layers.

##### Relationships & Interactions
- Data Flow: The Presentation layer (via controllers) receives incoming HTTP/API requests. These requests pass through middleware and are then forwarded to the Application layer. In the Application layer, the Command Bus and Query Bus coordinate the handling of commands and queries.
- Domain Communication: Once a command or query is dispatched, the corresponding command/query handler will coordinate with Domain services and entities. Here, business rules are applied, and domain events might be triggered.
- Infrastructure Role: When the domain needs to persist or retrieve data, it does so via repository interfaces defined in the Domain layer. The Infrastructure layer provides the concrete implementations for these repositories, handling data storage, external API calls, or integration concerns.
- Service Providers: Across layers, service providers (found in App, Application, Domain, and Infrastructure directories) are responsible for binding interfaces to their implementations, enabling the inversion of control (dependency injection).

##### Important Architectural Patterns
- Clean Architecture – Emphasize the dependency rule: the core (Domain) is independent of outer layers. Show that outer layers (Infrastructure, Presentation) depend on the inner layers.
- Domain-Driven Design (DDD) – Mark out Domain Entities, Value Objects, and Domain Events, showing clear separation from application logic and technical concerns.
- Command Query Responsibility Segregation (CQRS) – Clearly illustrate the separation between Command operations (writes) and Query operations (reads). You might show two distinct flows emerging from the Application layer: one for handling commands (with Command Handlers) and one for queries (with Query Handlers).

##### Relevant Technologies and Frameworks
- Laravel and PHP version details should be noted.
- Mention Bundled Tools/Packages like Spatie Laravel Data (for DTOs).
- Build configurations and workflows (e.g., .github/workflows/laravel.yml) can be noted as part of the CI/CD or deployment ecosystem.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Presentation Layer
    subgraph "Presentation Layer"
        routes["Routes"]:::presentation
        userController["UserController"]:::presentation
        userFormRequest["UserFormRequest"]:::presentation
    end

    %% Application Layer
    subgraph "Application Layer"
        cmdBus["Command Bus"]:::application
        queryBus["Query Bus"]:::application
        userCommands["User Commands"]:::application
        userCmdHandlers["User Command Handlers"]:::application
        userQueries["User Queries"]:::application
        userServices["User Services"]:::application
    end

    %% Domain Layer
    subgraph "Domain Layer"
        userEntity["User Entity"]:::domain
        domainEP["Domain Events/Policies"]:::domain
        domainExceptions["Domain Exceptions"]:::domain
        repoInterfaces["Repository Interfaces"]:::domain
        domainCommon["Domain Common"]:::domain
    end

    %% Infrastructure Layer
    subgraph "Infrastructure Layer"
        userRepoImpl["UserRepository Implementation"]:::infrastructure
        infraSP["Infrastructure Service Provider"]:::infrastructure
    end

    %% Shared Components
    subgraph "Shared Components"
        sharedComponents["Contracts & Common Utilities"]:::shared
    end

    %% Data Flow
    routes -->|"handles"| userController
    userController -->|"dispatches"| cmdBus
    userController -->|"dispatches"| queryBus

    cmdBus -->|"executes"| userCommands
    userCommands -->|"triggers"| userCmdHandlers
    queryBus -->|"executes"| userQueries

    userCmdHandlers -->|"invokes"| userEntity
    userQueries -->|"fetches"| userEntity

    userCmdHandlers -->|"uses"| userServices
    userServices -->|"validates"| sharedComponents

    userEntity -->|"persists"| repoInterfaces
    repoInterfaces -->|"implementedby"| userRepoImpl

    infraSP -->|"binds"| userRepoImpl

    userEntity -->|"triggers"| domainEP

    %% Click Events for Presentation Layer
    click routes "https://github.com/shahghasiadil/laravel-clean-architecture-ddd-cqrs/tree/main/src/Presentation/UserManagement/routes"
    click userController "https://github.com/shahghasiadil/laravel-clean-architecture-ddd-cqrs/blob/main/src/Presentation/UserManagement/Controllers/UserController.php"
    click userFormRequest "https://github.com/shahghasiadil/laravel-clean-architecture-ddd-cqrs/blob/main/src/Presentation/UserManagement/Requests/UserFormRequest.php"

    %% Click Events for Application Layer
    click cmdBus "https://github.com/shahghasiadil/laravel-clean-architecture-ddd-cqrs/tree/main/src/Application/Bus"
    click userCommands "https://github.com/shahghasiadil/laravel-clean-architecture-ddd-cqrs/tree/main/src/Application/User/Commands"
    click userCmdHandlers "https://github.com/shahghasiadil/laravel-clean-architecture-ddd-cqrs/tree/main/src/Application/User/CommandHandlers"
    click userQueries "https://github.com/shahghasiadil/laravel-clean-architecture-ddd-cqrs/tree/main/src/Application/User/Queries"
    click userServices "https://github.com/shahghasiadil/laravel-clean-architecture-ddd-cqrs/tree/main/src/Application/User/Services"

    %% Click Events for Domain Layer
    click userEntity "https://github.com/shahghasiadil/laravel-clean-architecture-ddd-cqrs/blob/main/src/Domain/User/Entities/User.php"
    click domainExceptions "https://github.com/shahghasiadil/laravel-clean-architecture-ddd-cqrs/tree/main/src/Domain/User/Exceptions"
    click repoInterfaces "https://github.com/shahghasiadil/laravel-clean-architecture-ddd-cqrs/blob/main/src/Domain/User/Repositories/UserRepositoryContract.php"
    click domainCommon "https://github.com/shahghasiadil/laravel-clean-architecture-ddd-cqrs/tree/main/src/Domain/Common"

    %% Click Events for Infrastructure Layer
    click userRepoImpl "https://github.com/shahghasiadil/laravel-clean-architecture-ddd-cqrs/blob/main/src/Infrastructure/User/Persistence/Repositories/UserRepository.php"
    click infraSP "https://github.com/shahghasiadil/laravel-clean-architecture-ddd-cqrs/blob/main/src/Infrastructure/Providers/InfrastructureServiceProvider.php"

    %% Click Events for Shared Components
    click sharedComponents "https://github.com/shahghasiadil/laravel-clean-architecture-ddd-cqrs/tree/main/src/Shared"

    %% Styles
    classDef presentation fill:#f9c74f,stroke:#333,stroke-width:2px;
    classDef application fill:#90be6d,stroke:#333,stroke-width:2px;
    classDef domain fill:#577590,stroke:#333,stroke-width:2px;
    classDef infrastructure fill:#f94144,stroke:#333,stroke-width:2px;
    classDef shared fill:#277da1,stroke:#333,stroke-width:2px;
```
