# slimphp/slim architecture

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

- Interactive diagram: https://gitdiagram.com/slimphp/slim
- Repository: https://github.com/slimphp/slim

## Overview

##### Main Components

a. Core Application Layer
- App (Central component)
- MiddlewareDispatcher
- CallableResolver
- ResponseEmitter

b. Routing System
- RouteCollector
- RouteParser
- RouteResolver
- Dispatcher (FastRoute integration)
- RouteGroup

c. Middleware System
- Built-in Middleware Components:
- RoutingMiddleware
- ErrorMiddleware
- BodyParsingMiddleware
- ContentLengthMiddleware
- OutputBufferingMiddleware
- MethodOverrideMiddleware

d. Error Handling System
- ErrorHandler
- Error Renderers:
- HTML
- JSON
- XML
- PlainText

e. PSR-7 Integration Layer
- PSR-17 Factories
- ServerRequestCreator
- Various PSR-7 Implementation Support

##### Relationship Flows

- Incoming HTTP Request → ServerRequestCreator → App
- App → MiddlewareDispatcher → Middleware Chain
- RouteCollector → RouteParser → RouteResolver → Dispatcher
- ErrorHandler → Error Renderers
- App → ResponseEmitter → HTTP Response

##### Implementation Details

Layer 1 - Request Processing:
**HTTP Request → ServerRequestCreator → App → MiddlewareDispatcher**

Layer 2 - Middleware Chain:
**MiddlewareDispatcher → [Middleware 1 → Middleware 2 → ... → Route Handler]**

Layer 3 - Routing:
**RouteCollector**
- ↓
**RouteParser → RouteResolver → Dispatcher**
- ↓
**Route Handler**

Layer 4 - Error Handling:
**ErrorMiddleware → ErrorHandler → Error Renderers**

##### Additional Elements

- Version compatibility information
- Extension points for custom implementations
- PSR-7 compliance points

##### Notes for Implementation

- Keep the main flow clear and centered
- Related components together
- Use consistent spacing and alignment
- Brief component descriptions
- Both synchronous and asynchronous flows

Remember to maintain a balance between detail and clarity, focusing on the architectural relationships rather than implementation details.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Main Components
    Request(["HTTP Request"])
    Response(["HTTP Response"])
    
    subgraph Core["Core Application Layer"]
        App["App"]:::core
        MwDispatch["MiddlewareDispatcher"]:::core
        CallRes["CallableResolver"]:::core
        RespEmit["ResponseEmitter"]:::core
    end
    
    subgraph Routing["Routing System"]
        RouteColl["RouteCollector"]:::routing
        RouteP["RouteParser"]:::routing
        RouteRes["RouteResolver"]:::routing
        Dispatch["Dispatcher"]:::routing
        RouteGrp["RouteGroup"]:::routing
    end
    
    subgraph Middleware["Middleware System"]
        direction TB
        RouteMw["RoutingMiddleware"]:::middleware
        ErrorMw["ErrorMiddleware"]:::middleware
        BodyMw["BodyParsingMiddleware"]:::middleware
        ContentMw["ContentLengthMiddleware"]:::middleware
        OutputMw["OutputBufferingMiddleware"]:::middleware
        MethodMw["MethodOverrideMiddleware"]:::middleware
    end
    
    subgraph ErrorHandling["Error Handling System"]
        direction TB
        ErrHandler["ErrorHandler"]:::error
        subgraph Renderers["Error Renderers"]
            direction TB
            HtmlRend["HTML Renderer"]:::renderer
            JsonRend["JSON Renderer"]:::renderer
            XmlRend["XML Renderer"]:::renderer
            PlainRend["PlainText Renderer"]:::renderer
        end
    end
    
    subgraph PSR7["PSR-7 Integration Layer"]
        Psr17Fact["PSR-17 Factories"]:::psr
        ServReqCreator["ServerRequestCreator"]:::psr
        FactProvider["Factory Provider"]:::psr
    end
    
    %% Relationships
    Request --> ServReqCreator
    ServReqCreator --> App
    App --> MwDispatch
    MwDispatch --> RouteMw
    RouteMw --> ErrorMw
    ErrorMw --> BodyMw
    BodyMw --> ContentMw
    ContentMw --> OutputMw
    OutputMw --> MethodMw
    
    RouteColl --> RouteP
    RouteP --> RouteRes
    RouteRes --> Dispatch
    RouteColl --> RouteGrp
    
    ErrorMw --> ErrHandler
    ErrHandler --> Renderers
    
    App --> RespEmit
    RespEmit --> Response
    
    %% Click Events
    click App "https://github.com/slimphp/slim/blob/4.x/Slim/App.php"
    click MwDispatch "https://github.com/slimphp/slim/blob/4.x/Slim/MiddlewareDispatcher.php"
    click CallRes "https://github.com/slimphp/slim/blob/4.x/Slim/CallableResolver.php"
    click RespEmit "https://github.com/slimphp/slim/blob/4.x/Slim/ResponseEmitter.php"
    click RouteColl "https://github.com/slimphp/slim/blob/4.x/Slim/Routing/RouteCollector.php"
    click RouteP "https://github.com/slimphp/slim/blob/4.x/Slim/Routing/RouteParser.php"
    click RouteRes "https://github.com/slimphp/slim/blob/4.x/Slim/Routing/RouteResolver.php"
    click Dispatch "https://github.com/slimphp/slim/blob/4.x/Slim/Routing/Dispatcher.php"
    click RouteGrp "https://github.com/slimphp/slim/blob/4.x/Slim/Routing/RouteGroup.php"
    click RouteMw "https://github.com/slimphp/slim/blob/4.x/Slim/Middleware/RoutingMiddleware.php"
    click ErrorMw "https://github.com/slimphp/slim/blob/4.x/Slim/Middleware/ErrorMiddleware.php"
    click BodyMw "https://github.com/slimphp/slim/blob/4.x/Slim/Middleware/BodyParsingMiddleware.php"
    click ContentMw "https://github.com/slimphp/slim/blob/4.x/Slim/Middleware/ContentLengthMiddleware.php"
    click OutputMw "https://github.com/slimphp/slim/blob/4.x/Slim/Middleware/OutputBufferingMiddleware.php"
    click MethodMw "https://github.com/slimphp/slim/blob/4.x/Slim/Middleware/MethodOverrideMiddleware.php"
    click ErrHandler "https://github.com/slimphp/slim/blob/4.x/Slim/Handlers/ErrorHandler.php"
    click HtmlRend "https://github.com/slimphp/slim/blob/4.x/Slim/Error/Renderers/HtmlErrorRenderer.php"
    click JsonRend "https://github.com/slimphp/slim/blob/4.x/Slim/Error/Renderers/JsonErrorRenderer.php"
    click XmlRend "https://github.com/slimphp/slim/blob/4.x/Slim/Error/Renderers/XmlErrorRenderer.php"
    click PlainRend "https://github.com/slimphp/slim/blob/4.x/Slim/Error/Renderers/PlainTextErrorRenderer.php"
    click Psr17Fact "https://github.com/slimphp/slim/tree/4.x/Slim/Factory/Psr17"
    click ServReqCreator "https://github.com/slimphp/slim/blob/4.x/Slim/Factory/Psr17/ServerRequestCreator.php"
    click FactProvider "https://github.com/slimphp/slim/blob/4.x/Slim/Factory/Psr17/Psr17FactoryProvider.php"
    
    %% Styles
    classDef core fill:#2374ab
    classDef routing fill:#48a9a6
    classDef middleware fill:#4ea5d9
    classDef error fill:#e63946
    classDef renderer fill:#e85f5c
    classDef psr fill:#8338ec
    
    %% Legend
    subgraph Legend
        CoreL["Core Components"]:::core
        RoutingL["Routing Components"]:::routing
        MwL["Middleware Components"]:::middleware
        ErrorL["Error Handling"]:::error
        RendL["Renderers"]:::renderer
        PsrL["PSR-7 Integration"]:::psr
    end
```
