# codelytv/php-ddd-example architecture

> Architecture diagram of the GitHub repository codelytv/php-ddd-example, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/codelytv/php-ddd-example
- Repository: https://github.com/codelytv/php-ddd-example

## Overview

##### Main Components
- Bounded Contexts: There are at least two major contexts – “Mooc” (for the public course platform) and “Backoffice” (for administrative/customer support functionality), plus a “Shared” context that implements common patterns and infrastructure.
- Applications: Within each bounded context, there are clear modules (for example, Courses, Videos, Notifications, etc.) following the hexagonal architecture style (with separate Application, Domain, and Infrastructure layers). In addition, note that there are three “apps” available from a deployment perspective – Mooc Backend, Backoffice Backend, and Backoffice Frontend.
- Infrastructure Components:
– API Endpoints (HTTP controllers) for both backends and the frontend.
– Command, Query, and Event Buses (with separate implementations using Symfony Message Bus, MySQL, or RabbitMQ).
– Persistence layers (e.g., Doctrine ORM, MySQL, Elasticsearch).
– External systems such as Prometheus for monitoring and Docker for containerization.

##### Relationships and Interactions
- For each bounded context, show how the Application layer interfaces with the Domain (which contains Aggregates, Domain Events, Repositories etc.) and the Infrastructure layer (which adapts external systems, such as databases or messaging queues).
- That the frontends (Backoffice Frontend and the other APIs) interact with their respective backends, which in turn use the bus-based mechanisms to delegate tasks.
- External dependencies such as Prometheus (for monitoring domain events) and the Dockerized environment that orchestrates the services.

##### Architectural Patterns
- Hexagonal (Ports and Adapters) Architecture: Center the domain layer (with Aggregates, Domain Events, etc.) and surround it with adapters (Entry points like HTTP controllers and exit points like the database or message queues).
- CQRS Pattern: Clearly separate “Commands” (actions that modify state) from “Queries” (read-only requests), and show that each bounded context has its own ways to handle commands and queries.
- Repository and Specification Patterns: Note that repositories have a basic interface (only methods such as save and search) and extended querying via the Criteria pattern.

##### Elements Reflecting Technologies and Frameworks
- That some modules use Symfony components (as shown in the “config” directories and the use of Symfony Message Bus).

- Bounded Context Sections:
– Mooc Context (Modules: Courses, Videos, Notifications, Steps, etc.)
– Backoffice Context (Modules: Courses, possibly Auth, etc.)
– Shared Kernel (Common Domain and Infrastructure components)
- Layered structure inside each context:
– Application Layer: Command Handlers, Query Handlers, Application Services.
– Domain Layer: Aggregates, Domain Events, Domain Models.
– Infrastructure Layer: Persistence implementations (Doctrine, MySQL), Bus implementations, External integrations (Elasticsearch, RabbitMQ).
- API and UI Layers:
– Backoffice Frontend (with HTTP controllers and templates)
– Backoffice Backend and Mooc Backend (with Console commands and API controllers)
- Buses:
– Command Bus, Query Bus, Event Bus (with variations like InMemory, MySQL, and RabbitMQ)
- Monitoring:
– Prometheus instance that collects domain event metrics.
- Deployment:
– Docker and docker-compose as orchestrators of these services.

This detailed separation into as many distinct components as possible will assist in an accurate architectural view that both highlights the implementation of Hexagonal Architecture and the principles of DDD and CQRS as applied in this PHP project.

## Diagram (Mermaid)

```mermaid
flowchart TD

    %% Apps / External Interfaces
    subgraph "Apps/External Interfaces"
        MBAPI["Mooc Backend API"]:::apps
        BBAPI["Backoffice Backend API"]:::apps
        BFUI["Backoffice Frontend UI"]:::apps
    end

    %% Mooc Bounded Context
    subgraph "Mooc Bounded Context"
        MC["Courses Module (App & Domain)"]:::context
        MS["Steps Module (App & Domain)"]:::context
        MV["Videos Module (App & Domain)"]:::context
        MCC["Courses Counter Module (App & Domain)"]:::context
        MN["Notifications Module (App Layer)"]:::context
        MCP["Courses Persistence"]:::infrastructure
    end

    %% Backoffice Bounded Context
    subgraph "Backoffice Bounded Context"
        BC["Courses Module (Domain & Infra)"]:::context
        BA["Authentication Module (App & Domain)"]:::context
        BCP["Courses Persistence"]:::infrastructure
    end

    %% Shared Kernel
    subgraph "Shared Kernel"
        SD["Shared Domain & Utilities"]:::shared
        SI["Shared Infrastructure"]:::shared
        BI["Bus Interfaces"]:::shared
        BIImpl["Bus Implementations"]:::shared
        SP["Shared Persistence"]:::infrastructure
    end

    %% Messaging Buses
    subgraph "Messaging Buses"
        CB["Command Bus"]:::bus
        QB["Query Bus"]:::bus
        EB["Event Bus"]:::bus
    end

    %% Deployment & Monitoring
    subgraph "Deployment & Monitoring"
        PM["Prometheus Monitor"]:::external
        DF["Dockerfile"]:::deploy
        DC["docker-compose"]:::deploy
    end

    %% Connections from APIs to Command Bus
    MBAPI -->|"HTTP"| CB
    BBAPI -->|"HTTP"| CB
    BFUI -->|"HTTP"| CB

    %% Command Bus dispatching to Mooc and Backoffice modules
    CB -->|"dispatch"| MC
    CB -->|"dispatch"| MS
    CB -->|"dispatch"| MV
    CB -->|"dispatch"| MCC
    CB -->|"dispatch"| MN
    CB -->|"dispatch"| BC
    CB -->|"dispatch"| BA

    %% Persistence flow in Mooc Context
    MC -->|"persist"| MCP
    MCP --> SP

    %% Persistence flow in Backoffice Context
    BC -->|"persist"| BCP
    BCP --> SP

    %% Domain events emitted by modules to Event Bus
    MC -->|"emits"| EB
    BC -->|"emits"| EB
    BA -->|"emits"| EB

    %% Event Bus notifying Monitoring
    EB -->|"notify"| PM

    %% Bus Infrastructure connections
    BIImpl --> CB
    BIImpl --> QB
    BIImpl --> EB
    BI --> BIImpl

    %% Relationship between Shared Domain and Infrastructure
    SD --- SI

    %% Styles
    classDef apps fill:#aee1f9,stroke:#333,stroke-width:1px;
    classDef context fill:#c8e6c9,stroke:#333,stroke-width:1px;
    classDef shared fill:#fff59d,stroke:#333,stroke-width:1px;
    classDef bus fill:#f8bbd0,stroke:#333,stroke-width:1px;
    classDef infrastructure fill:#ffe0b2,stroke:#333,stroke-width:1px;
    classDef deploy fill:#d7ccc8,stroke:#333,stroke-width:1px;
    classDef external fill:#ef9a9a,stroke:#333,stroke-width:1px;

    %% Click Events for Component Mapping
    click MC "https://github.com/codelytv/php-ddd-example/tree/main/src/Mooc/Courses"
    click MS "https://github.com/codelytv/php-ddd-example/tree/main/src/Mooc/Steps"
    click MV "https://github.com/codelytv/php-ddd-example/tree/main/src/Mooc/Videos"
    click MCC "https://github.com/codelytv/php-ddd-example/tree/main/src/Mooc/CoursesCounter"
    click MN "https://github.com/codelytv/php-ddd-example/tree/main/src/Mooc/Notifications"
    click BC "https://github.com/codelytv/php-ddd-example/tree/main/src/Backoffice/Courses"
    click BA "https://github.com/codelytv/php-ddd-example/tree/main/src/Backoffice/Auth"
    click SD "https://github.com/codelytv/php-ddd-example/tree/main/src/Shared"
    click SI "https://github.com/codelytv/php-ddd-example/tree/main/src/Shared/Infrastructure"
    click BI "https://github.com/codelytv/php-ddd-example/tree/main/src/Shared/Domain/Bus"
    click BIImpl "https://github.com/codelytv/php-ddd-example/tree/main/src/Shared/Infrastructure/Bus"
    click SP "https://github.com/codelytv/php-ddd-example/tree/main/src/Shared/Infrastructure/Persistence"
    click MCP "https://github.com/codelytv/php-ddd-example/tree/main/src/Mooc/Courses/Infrastructure/Persistence"
    click BCP "https://github.com/codelytv/php-ddd-example/tree/main/src/Backoffice/Courses/Infrastructure/Persistence"
    click MBAPI "https://github.com/codelytv/php-ddd-example/tree/main/apps/mooc/backend/src/Controller"
    click BBAPI "https://github.com/codelytv/php-ddd-example/tree/main/apps/backoffice/backend/src/Controller"
    click BFUI "https://github.com/codelytv/php-ddd-example/tree/main/apps/backoffice/frontend/src/Controller"
    click BFUI "https://github.com/codelytv/php-ddd-example/tree/main/apps/backoffice/frontend/templates"
    click DF "https://github.com/codelytv/php-ddd-example/tree/main/Dockerfile"
    click DC "https://github.com/codelytv/php-ddd-example/blob/main/docker-compose.yml"
    click PM "https://github.com/codelytv/php-ddd-example/blob/main/src/Shared/Infrastructure/Monitoring/PrometheusMonitor.php"
```
