# silexphp/silex-skeleton architecture

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

- Interactive diagram: https://gitdiagram.com/silexphp/silex-skeleton
- Repository: https://github.com/silexphp/silex-skeleton

## Overview

##### Project type and purpose
- This is a PHP web application skeleton built on the Silex micro-framework. It provides both an HTTP front-end (via web/index.php and index_dev.php) and a CLI interface (bin/console). It is meant to be a starting point for building small to medium Silex-based applications with standard service providers (Twig, Monolog, Validator, WebProfiler) and environment-specific configurations.

##### File structure
- Top-level
- bin/console – the CLI entry point for console commands
- web/index.php, web/index_dev.php – HTTP front controllers for production and development
- config/dev.php, config/prod.php – environment-specific service configuration
- src/app.php – application bootstrap (creates the Silex\Application, registers service providers, loads controllers)
- src/console.php – CLI bootstrap (registers console commands as services)
- src/controllers.php – controller definitions wired into the service container
- templates/ – Twig templates for pages and error handlers
- var/cache, var/logs – runtime cache and log directories
- tests/controllersTest.php – PHPUnit tests for controller behavior

##### README for additional insights
- Service providers used:
- ValidatorServiceProvider
- ServiceControllerServiceProvider (controllers as lazy DI services)
- TwigServiceProvider (template rendering)
- WebProfilerServiceProvider (development profiling toolbar)
- MonologServiceProvider (logging)
- No database provider is included by default; persistence is left to the developer to add if needed (e.g., Doctrine).

a. Main components to represent
1. HTTP Layer
– Front Controllers: web/index.php (prod), web/index_dev.php (dev)
– PHP Built-in Server or Apache/Nginx
2. Application Core
– Silex\Application instance (bootstrapped in src/app.php)
– Service Container (DI container holding all services)
3. Service Providers
– Twig (templates/)
– Monolog (var/logs/)
– Validator
– Web Profiler (only dev)
– ServiceController (controllers wired as services)
4. Controllers (src/controllers.php)
5. Templates (templates/*.twig)
6. Cache (var/cache/)
7. CLI Layer
– bin/console → src/console.php → Console commands as services
8. Tests (tests/) – external to runtime, for validation
b. Relationships and interactions
- HTTP Request → Front Controller → src/app.php → Service Container
- Service Container resolves Controller (via ServiceController) → Controller returns Response
- Response → Twig Renderer if view, or direct output
- Monolog logs events (errors, requests) to var/logs
- Validator is injected into controllers or form handlers
- Web Profiler hooks into request/response in dev mode
- CLI commands resolved by console.php via same Service Container
- Cache provider (PSR-6/PSR-16) used by Twig or other services writing to var/cache
c. Architectural patterns and design principles
- Micro-framework structure using Dependency Injection and Service Container
- Separation of concerns: HTTP vs CLI layers, config vs code, templates vs controllers
- Environment segregation (dev vs prod) via separate config files and front controllers
- Convention over configuration: standard directories for cache, logs, templates
d. Key technologies, frameworks, libraries
- PHP 5.5+
- Silex 2.x (Symfony components under the hood)
- Twig for templating
- Monolog for logging
- Symfony Validator
- Symfony WebProfiler (dev only)
- Composer for autoloading and dependency management

##### Essential details to capture
- Clearly distinguish environments (dev vs prod) and how that affects configuration and service providers
- That both web and CLI entry points share the same core bootstrapping logic and services
- Cache and log directories as service targets, even though they are file-based
- Where PHPUnit tests plug in (tests/ directory) as validation of controller logic rather than a runtime component

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% HTTP Layer
    subgraph "HTTP Layer" 
        direction TB
        ProdIndex["web/index.php"]:::entry
        DevIndex["web/index_dev.php"]:::entry
    end

    %% CLI Layer
    subgraph "CLI Layer"
        direction TB
        CLI["bin/console"]:::entry
    end

    %% Environment Configurations
    subgraph "Configurations"
        direction TB
        ConfigProd["config/prod.php"]:::config
        ConfigDev["config/dev.php"]:::config
    end

    %% Application Core
    subgraph "Application Core" 
        direction TB
        AppBootstrap(("Silex Application / Service Container")):::core
        CLIBootstrap["src/console.php"]:::core
        Controllers["src/controllers.php"]:::controller
    end

    %% Service Providers
    subgraph "Service Providers"
        direction TB
        TwigProv["TwigServiceProvider"]:::provider
        MonologProv["MonologServiceProvider"]:::provider
        ValidatorProv["ValidatorServiceProvider"]:::provider
        ServiceControllerProv["ServiceControllerServiceProvider"]:::provider
        WebProfilerProv["WebProfilerServiceProvider (dev)"]:::provider
    end

    %% Storage and Templates
    subgraph "Storage & Templates"
        direction TB
        Templates["templates/"]:::storage
        ErrorTemplates["templates/errors/"]:::storage
        Cache["var/cache/"]:::storage
        Logs["var/logs/"]:::storage
    end

    %% Tests
    Tests["tests/controllersTest.php"]:::test

    %% Connections
    ProdIndex -->|"HTTP (prod)"| AppBootstrap
    DevIndex -->|"HTTP (dev)"| AppBootstrap
    CLI -->|"CLI" | CLIBootstrap
    ConfigProd -->|loads| AppBootstrap
    ConfigDev -->|loads| AppBootstrap
    CLIBootstrap -->|bootstraps via src/app.php| AppBootstrap
    AppBootstrap -->|registers| ServiceControllerProv
    AppBootstrap -->|registers| ValidatorProv
    AppBootstrap -->|registers| TwigProv
    AppBootstrap -->|registers| MonologProv
    AppBootstrap -->|registers| WebProfilerProv
    AppBootstrap -->|loads| Controllers
    Controllers -->|renders views via| TwigProv
    Controllers -->|logs events via| MonologProv
    Controllers -->|validates via| ValidatorProv
    TwigProv -->|reads templates from| Templates
    TwigProv -->|reads errors from| ErrorTemplates
    MonologProv -->|writes logs to| Logs
    AppBootstrap -->|caches data in| Cache
    Tests -->|validates| Controllers

    %% Click Events
    click ProdIndex "https://github.com/silexphp/silex-skeleton/blob/master/web/index.php"
    click DevIndex "https://github.com/silexphp/silex-skeleton/blob/master/web/index_dev.php"
    click CLI "https://github.com/silexphp/silex-skeleton/tree/master/bin/console"
    click AppBootstrap "https://github.com/silexphp/silex-skeleton/blob/master/src/app.php"
    click CLIBootstrap "https://github.com/silexphp/silex-skeleton/blob/master/src/console.php"
    click Controllers "https://github.com/silexphp/silex-skeleton/blob/master/src/controllers.php"
    click ConfigDev "https://github.com/silexphp/silex-skeleton/blob/master/config/dev.php"
    click ConfigProd "https://github.com/silexphp/silex-skeleton/blob/master/config/prod.php"
    click Templates "https://github.com/silexphp/silex-skeleton/tree/master/templates/"
    click ErrorTemplates "https://github.com/silexphp/silex-skeleton/tree/master/templates/errors/"
    click Cache "https://github.com/silexphp/silex-skeleton/tree/master/var/cache/"
    click Logs "https://github.com/silexphp/silex-skeleton/tree/master/var/logs/"
    click Tests "https://github.com/silexphp/silex-skeleton/blob/master/tests/controllersTest.php"

    %% Styles
    classDef entry fill:#a2d5c6,stroke:#333,stroke-width:1px
    classDef core fill:#69a3f3,stroke:#333,stroke-width:1px,shape:ellipse
    classDef provider fill:#f4a261,stroke:#333,stroke-width:1px
    classDef storage fill:#9d94ff,stroke:#333,stroke-width:1px,shape:cylinder
    classDef controller fill:#2a9d8f,stroke:#333,stroke-width:1px,shape:parallelogram
    classDef config fill:#e76f51,stroke:#333,stroke-width:1px
    classDef test fill:#8d99ae,stroke:#333,stroke-width:1px,stroke-dasharray: 5 5
```
