# phpmessagebus/messagebus architecture

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

- Interactive diagram: https://gitdiagram.com/phpmessagebus/messagebus
- Repository: https://github.com/phpmessagebus/messagebus

## Overview

##### Main Components

a) Three Core Message Bus Components:
- CommandBus
- QueryBus
- EventBus

b) Middleware Layers:
- Base Middleware
- Logging Middleware
- Transactional Middleware
- Cache Middleware (for QueryBus)

c) Supporting Components:
- Resolvers (Container integration)
- Translators
- Serializers

**┌─ Application Layer ─────────────────────────────────────────┐**

- ┌─ Command Bus ──┐   ┌─ Query Bus ───┐   ┌─ Event Bus ──┐ │
- Commands       │   │ Queries        │   │ Events       │ │
- Handlers      ─┼──>│ Handlers      ─┼──>│ Handlers    │ │
- ┘   └───────────────┘   └─────────────┘ │

- Middleware Layer ──────────────────────────────────────────┤
- ┌─────────────┐  ┌──────────────┐  ┌─────────────────┐   │
- Logging     │  │ Transactional│  │ Cache (Query)   │   │
- ┘  └──────────────┘  └─────────────────┘   │

- Infrastructure Layer ───────────────────────────────────────┤
- ┌─────────────┐  ┌──────────────┐  ┌─────────────────┐   │
- Resolvers   │  │ Translators  │  │ Serializers     │   │
- ┘  └──────────────┘  └─────────────────┘   │
- ┘

##### Component Relationships

- Message flow through middleware
- Resolution of handlers through container
- Translation of messages to handlers
- Serialization points for caching/queuing

##### Specific Implementation Details

- Container support (PSR-11, Symfony, Interop)
- Supported serialization formats (Native, JSON, JsonObject)
- Middleware stack order
- Handler resolution strategies

##### Notes for Implementation

- Priority handling in Event Bus
- Transaction boundaries
- Cache integration points

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Styles
    classDef bus fill:#a8d1f0,stroke:#333
    classDef middleware fill:#90EE90,stroke:#333
    classDef infra fill:#FFE5B4,stroke:#333
    classDef layer fill:none,stroke:#333,stroke-dasharray: 5 5

    %% Application Layer
    subgraph AppLayer["Application Layer"]
        direction TB
        CommandBus["Command Bus"]:::bus
        QueryBus["Query Bus"]:::bus
        EventBus["Event Bus"]:::bus
    end

    %% Middleware Layer
    subgraph MWLayer["Middleware Layer"]
        direction TB
        LogMW["Logging Middleware"]:::middleware
        TransMW["Transactional Middleware"]:::middleware
        CacheMW["Cache Middleware"]:::middleware
        BaseMW["Base Middleware"]:::middleware
    end

    %% Infrastructure Layer
    subgraph InfraLayer["Infrastructure Layer"]
        direction TB
        Resolvers["Resolvers"]:::infra
        Translators["Translators"]:::infra
        Serializers["Serializers"]:::infra
    end

    %% Relationships
    CommandBus --> LogMW
    QueryBus --> LogMW
    EventBus --> LogMW
    
    LogMW --> TransMW
    TransMW --> CacheMW
    CacheMW --> BaseMW
    
    BaseMW --> Resolvers
    BaseMW --> Translators
    BaseMW --> Serializers

    %% Click Events
    click CommandBus "https://github.com/PHPMessageBus/messagebus/blob/master/src/CommandBus/CommandBus.php"
    click QueryBus "https://github.com/PHPMessageBus/messagebus/blob/master/src/QueryBus/QueryBus.php"
    click EventBus "https://github.com/PHPMessageBus/messagebus/blob/master/src/EventBus/EventBus.php"
    
    click LogMW "https://github.com/PHPMessageBus/messagebus/blob/master/src/CommandBus/LoggerCommandBusMiddleware.php"
    click TransMW "https://github.com/PHPMessageBus/messagebus/blob/master/src/CommandBus/TransactionalCommandBusMiddleware.php"
    click CacheMW "https://github.com/PHPMessageBus/messagebus/blob/master/src/QueryBus/CacheQueryBusMiddleware.php"
    click BaseMW "https://github.com/PHPMessageBus/messagebus/blob/master/src/CommandBus/CommandBusMiddleware.php"
    
    click Resolvers "https://github.com/PHPMessageBus/messagebus/tree/master/src/CommandBus/Resolver/"
    click Translators "https://github.com/PHPMessageBus/messagebus/tree/master/src/CommandBus/Translator/"
    click Serializers "https://github.com/PHPMessageBus/messagebus/blob/master/src/Serializer/JsonObjectSerializer.php"

    %% Legend
    subgraph Legend
        direction TB
        B1["Message Bus"]:::bus
        M1["Middleware"]:::middleware
        I1["Infrastructure Component"]:::infra
    end
```
