# tempestphp/tempest-framework architecture

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

- Interactive diagram: https://gitdiagram.com/tempestphp/tempest-framework
- Repository: https://github.com/tempestphp/tempest-framework

## Overview

##### Project type and purpose
- Tempest is not a single full‑stack application but a modular PHP framework (monorepo) comprising multiple reusable packages (Core, Router, Database, View, Console, EventBus, CommandBus, etc.) plus a Vite plugin in JavaScript.
- Its README emphasizes “zero config, zero overhead” framework components that auto‑discover and wire application code (controllers, commands, migrations, view components) via dependency injection, middleware, events, and initializers.

##### File structure
- Top‑level directories:
– src/Tempest – each subfolder is a standalone Composer package (Auth, Cache, Clock, …, Vite).
– packages/vite‑plugin‑tempest – JavaScript plugin for integrating Vite into Tempest apps.
– bin/ and monorepo‑builder.php – CLI scripts to build, test, release packages.
– .github/workflows – CI pipelines for linting, testing, publishing.
- Composer.json and package.json define PHP and JS package metadata.
- public/index.php is a sample HTTP entrypoint.
- Tests directories (tests/, tests/Integration) validate each package’s functionality and end‑to‑end behavior.

##### README for additional insights
- Tempest is composed of:
a. Core kernel & configuration loader
b. HTTP stack (Router, Http, HttpClient)
c. Console (CLI command discovery/execution)
d. Database (ORM, migrations)
e. EventBus & CommandBus
f. View rendering (Blade/Twig-like)
g. Support libraries (Logging, Validation, Filesystem, Cache, Generation, Reflection, Container)
h. Vite integration (JS asset pipeline)

##### Details
- Package boundary and show how Initializers register services in the Container.
- Middleware ordering and how Console or HTTP layers invoke the same underlying EventBus/CommandBus.
- Vite plugin as a sidecar JS component that integrates at view‑rendering time.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Entry Points
    subgraph "Entrypoints"
        HTTPRequest["HTTP Request"]
        CLIInvocation["CLI Invocation"]
    end

    %% Core & DI Layer
    subgraph "Core & DI" 
        direction TB
        Kernel["Framework Kernel"]:::coreDI
        Container["Dependency Injection Container"]:::coreDI
        LoadConfig["LoadConfig"]:::coreDI
        LoadDiscovery["LoadDiscoveryClasses"]:::coreDI
    end

    %% HTTP & Routing Layer
    subgraph "HTTP & Routing"
        direction TB
        HttpApp["HttpApplication"]:::http
        MiddlewarePipeline["HTTP Middleware Pipeline"]:::http
        Router["Router & Route Matching"]:::http
        RequestMapper["Request/Response Mappers"]:::http
    end

    %% Console Layer
    subgraph "Console"
        direction TB
        ConsoleApp["ConsoleApplication"]:::console
        CommandDiscovery["ConsoleCommandDiscovery"]:::console
        ConsoleMiddleware["ConsoleMiddlewareDiscovery"]:::console
    end

    %% Database & ORM Layer
    subgraph "Database & ORM"
        direction TB
        PDOInit["PDO Connection Initializer"]:::db
        QueryBuilder["Query Builder & ORM"]:::db
        MigrationMgr["Migration Manager"]:::db
    end

    %% EventBus & CommandBus
    subgraph "EventBus & CommandBus"
        direction TB
        EventBusCore["EventBus"]:::support
        EventBusInit["EventBusInitializer"]:::support
        CommandBusCore["CommandBus"]:::support
        CommandBusInit["CommandBusInitializer"]:::support
    end

    %% View & Asset Pipeline
    subgraph "View & Asset Pipeline"
        direction TB
        ViewCompiler["View Compiler (Lexer, Parser, AST)"]:::view
        ViewRenderer["View Renderer"]:::view
        VitePlugin["Vite JS Plugin"]:::plugin
        PHPVite["PHP Vite Integration"]:::plugin
    end

    %% Support Services
    subgraph "Support Services"
        direction TB
        Logger["Logging"]:::support
        CacheSvc["Cache"]:::support
        Filesystem["Filesystem"]:::support
        Validation["Validation"]:::support
        Clock["Clock"]:::support
        Reflection["Reflection"]:::support
        Generation["Generation"]:::support
    end

    %% Container Registration Flow
    Kernel -->|loads| LoadConfig
    Kernel -->|loads| LoadDiscovery
    LoadConfig -->|registers services| Container
    LoadDiscovery -->|registers services| Container

    %% HTTP Flow
    HTTPRequest --> HttpApp
    HttpApp --> MiddlewarePipeline
    MiddlewarePipeline --> Router
    Router --> RequestMapper
    RequestMapper -->|maps to PSR7| HttpApp
    Router -->|dispatch to| Kernel
    Kernel -->|resolves Controller| Container
    Kernel -->|inject deps| Controller["Controller"]:::http
    Controller -->|uses| QueryBuilder
    Controller -->|uses| EventBusCore
    Controller -->|uses| CommandBusCore
    Controller -->|uses| ViewRenderer
    Controller -->|uses| Logger
    Controller -->|uses| CacheSvc
    Controller -->|uses| Filesystem
    Controller --> Response["Response"]:::http

    %% Console Flow
    CLIInvocation --> ConsoleApp
    ConsoleApp --> CommandDiscovery
    ConsoleApp --> ConsoleMiddleware
    CommandDiscovery -->|registers commands| Container
    ConsoleApp -->|resolve command| Kernel
    Kernel -->|executes| Command["Command"]:::console
    Command -->|uses| EventBusCore
    Command -->|emits| EventBusCore

    %% Database Flow
    Command -->|migration command| MigrationMgr
    MigrationMgr --> QueryBuilder
    QueryBuilder --> PDOInit
    PDOInit --> Database["Database (PDO)"]:::db

    %% View & Vite Integration
    ViewRenderer -->|compiles| ViewCompiler
    ViewCompiler -->|generates AST| ViewRenderer
    VitePlugin -->|injects assets| ViewRenderer
    PHPVite -->|compiles tags| ViewRenderer

    %% EventBus & CommandBus initialization
    Kernel --> EventBusInit
    Kernel --> CommandBusInit
    EventBusInit --> EventBusCore
    CommandBusInit --> CommandBusCore

    %% Support Services initialization
    Kernel --> Logger
    Kernel --> CacheSvc
    Kernel --> Filesystem
    Kernel --> Validation
    Kernel --> Clock
    Kernel --> Reflection
    Kernel --> Generation

    %% Click Events
    click Kernel "https://github.com/tempestphp/tempest-framework/blob/main/src/Tempest/Core/src/FrameworkKernel.php"
    click Container "https://github.com/tempestphp/tempest-framework/blob/main/src/Tempest/Container/src/Container.php"
    click LoadConfig "https://github.com/tempestphp/tempest-framework/blob/main/src/Tempest/Core/src/Kernel/LoadConfig.php"
    click LoadDiscovery "https://github.com/tempestphp/tempest-framework/blob/main/src/Tempest/Core/src/Kernel/LoadDiscoveryClasses.php"
    click HttpApp "https://github.com/tempestphp/tempest-framework/blob/main/src/Tempest/Router/src/HttpApplication.php"
    click MiddlewarePipeline "https://github.com/tempestphp/tempest-framework/blob/main/src/Tempest/Router/src/HttpMiddleware.php"
    click MiddlewarePipeline "https://github.com/tempestphp/tempest-framework/blob/main/src/Tempest/Router/src/HttpMiddlewareDiscovery.php"
    click Router "https://github.com/tempestphp/tempest-framework/blob/main/src/Tempest/Router/src/Router.php"
    click Router "https://github.com/tempestphp/tempest-framework/blob/main/src/Tempest/Router/src/Routing/Matching/GenericRouteMatcher.php"
    click RequestMapper "https://github.com/tempestphp/tempest-framework/blob/main/src/Tempest/Router/src/Mappers/PsrRequestToGenericRequestMapper.php"
    click RequestMapper "https://github.com/tempestphp/tempest-framework/blob/main/src/Tempest/Router/src/Mappers/RequestToPsrRequestMapper.php"
    click ConsoleApp "https://github.com/tempestphp/tempest-framework/blob/main/src/Tempest/Console/src/ConsoleApplication.php"
    click CommandDiscovery "https://github.com/tempestphp/tempest-framework/blob/main/src/Tempest/Console/src/Discovery/ConsoleCommandDiscovery.php"
    click ConsoleMiddleware "https://github.com/tempestphp/tempest-framework/blob/main/src/Tempest/Console/src/Discovery/ConsoleMiddlewareDiscovery.php"
    click PDOInit "https://github.com/tempestphp/tempest-framework/blob/main/src/Tempest/Database/src/Connection/ConnectionInitializer.php"
    click QueryBuilder "https://github.com/tempestphp/tempest-framework/blob/main/src/Tempest/Database/src/Builder/QueryBuilders/QueryBuilder.php"
    click QueryBuilder "https://github.com/tempestphp/tempest-framework/blob/main/src/Tempest/Database/src/GenericDatabase.php"
    click MigrationMgr "https://github.com/tempestphp/tempest-framework/blob/main/src/Tempest/Database/src/Migrations/MigrationManager.php"
    click EventBusCore "https://github.com/tempestphp/tempest-framework/blob/main/src/Tempest/EventBus/src/EventBus.php"
    click EventBusInit "https://github.com/tempestphp/tempest-framework/blob/main/src/Tempest/EventBus/src/EventBusInitializer.php"
    click CommandBusCore "https://github.com/tempestphp/tempest-framework/blob/main/src/Tempest/CommandBus/src/CommandBus.php"
    click CommandBusInit "https://github.com/tempestphp/tempest-framework/blob/main/src/Tempest/CommandBus/src/CommandBusInitializer.php"
    click ViewCompiler "https://github.com/tempestphp/tempest-framework/blob/main/src/Tempest/View/src/Parser/TempestViewCompiler.php"
    click ViewCompiler "https://github.com/tempestphp/tempest-framework/blob/main/src/Tempest/View/src/Parser/TempestViewLexer.php"
    click ViewCompiler "https://github.com/tempestphp/tempest-framework/blob/main/src/Tempest/View/src/Parser/TempestViewParser.php"
    click ViewRenderer "https://github.com/tempestphp/tempest-framework/blob/main/src/Tempest/View/src/Renderers/TempestViewRenderer.php"
    click Logger "https://github.com/tempestphp/tempest-framework/blob/main/src/Tempest/Log/src/Logger.php"
    click Logger "https://github.com/tempestphp/tempest-framework/blob/main/src/Tempest/Log/src/LoggerInitializer.php"
    click CacheSvc "https://github.com/tempestphp/tempest-framework/blob/main/src/Tempest/Cache/src/Cache.php"
    click CacheSvc "https://github.com/tempestphp/tempest-framework/blob/main/src/Tempest/Cache/src/CacheInitializer.php"
    click Filesystem "https://github.com/tempestphp/tempest-framework/blob/main/src/Tempest/Filesystem/src/Filesystem.php"
    click Validation "https://github.com/tempestphp/tempest-framework/blob/main/src/Tempest/Validation/src/Validator.php"
    click Clock "https://github.com/tempestphp/tempest-framework/blob/main/src/Tempest/Clock/src/Clock.php"
    click Clock "https://github.com/tempestphp/tempest-framework/blob/main/src/Tempest/Clock/src/ClockInitializer.php"
    click Reflection "https://github.com/tempestphp/tempest-framework/blob/main/src/Tempest/Reflection/src/ClassReflector.php"
    click Generation "https://github.com/tempestphp/tempest-framework/blob/main/src/Tempest/Generation/src/ClassGenerator.php"
    click VitePlugin "https://github.com/tempestphp/tempest-framework/blob/main/packages/vite-plugin-tempest/src/plugin.ts"
    click PHPVite "https://github.com/tempestphp/tempest-framework/blob/main/src/Tempest/Vite/src/Vite.php"
    click PHPVite "https://github.com/tempestphp/tempest-framework/blob/main/src/Tempest/Vite/src/TagCompiler/GenericTagCompiler.php"

    %% Styles
    classDef coreDI fill:#e0f7e9,stroke:#2ecc71,color:#000
    classDef http fill:#e0f1ff,stroke:#3498db,color:#000
    classDef console fill:#fff4e0,stroke:#e67e22,color:#000
    classDef db fill:#f3e0ff,stroke:#9b59b6,color:#000
    classDef view fill:#fff8e0,stroke:#f1c40f,color:#000
    classDef support fill:#f0f0f0,stroke:#95a5a6,color:#000
    classDef plugin fill:#fff0b3,stroke:#f39c12,color:#000
```
