# vgalvoso/spm architecture

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

- Interactive diagram: https://gitdiagram.com/vgalvoso/spm
- Repository: https://github.com/vgalvoso/spm

## Overview

##### Project Type and Purpose
- This is a minimal PHP MVC “framework” designed to help you quickly spin up small web applications.
- It provides a front controller (index.php), a lightweight router (routes.php), controllers, views, models, and database connectivity, all without heavy dependencies.

##### Key Architectural Elements from File Structure
- app/Config/Database.php: database configuration, loaded by a DotEnv library
- app/Libraries: core framework helpers (DotEnv loader, base Model, general Helper functions)
- app/Controller: your HTTP-facing controllers (e.g., Home.php)
- app/Model: your data-access classes (e.g., HomeModel.php)
- app/Views: presentation layer (templates, header/footer sections)
- public/: static assets (CSS, JS) served by the web server
- index.php + autoload.php: front controller and PSR-4 autoloader bootstrap
- routes.php: maps GET/POST/CLI endpoints to controller methods

##### Main Components & Their Relationships
a. Client & Transport Layer
- Browser or CLI client makes HTTP/CLI requests to the Web Server.
- Web Server (Apache/Nginx) points all traffic to index.php (via .htaccess or vhost).
b. Front Controller & Router
- index.php initializes Composer autoloader, loads DotEnv, reads app/Config/Database.
- routes.php defines endpoints; router dispatches request to the correct controller method.
c. Controllers
- Receive request data (GET/POST/CLI), invoke business logic, call Models, and select Views.
d. Models & Database
- Base Model in app/Libraries/Model.php handles connection, query execution, transactions.
- HomeModel (and others) extend base Model, perform CRUD via getItem(), getItems(), exec() methods.
- Database drivers: MySQL, MSSQL, SQLite.
e. Views & Presentation
- Controllers call view() helper to render header, page body, footer.
- Templates in app/Views produce HTML + embedded PHP variables.
f. Static Assets
- public/css/style.css (and potentially JS/images) are referenced by Views, served directly by the web server.

##### Architectural Patterns & Principles
- MVC separation: Controllers only coordinate, Models only handle data, Views only present.
- Front Controller pattern: single entry point (index.php).
- Router abstraction: declarative routes in routes.php.
- Dependency injection via DotEnv and config file.
- PSR-4 autoloading (via composer.json) ensures class mapping.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Layer
    subgraph "Client Layer" 
        direction TB
        Browser(["Browser"]):::external
        CLIClient(["CLI Client"]):::external
    end

    %% Web Server Layer
    subgraph "Web Server" 
        direction TB
        WebServer(["Web Server<br/>Apache/Nginx"]):::core
        indexphp(["index.php<br/>Front Controller<br/>PHP 7+"]):::core
        htaccess[".htaccess"]:::core
    end

    %% Core Framework
    subgraph "Core Framework" 
        direction TB
        autoload(["autoload.php<br/>PSR-4 Autoloader"]):::core
        composer(["composer.json<br/>Composer Config"]):::core
        dotenv(["DotEnv.php<br/>Env Loader"]):::core
        routes(["routes.php<br/>Router"]):::core
        DatabaseConfig(["Database.php<br/>DB Config"]):::db
        ModelBase(["Model.php<br/>Base Model"]):::core
        HelperLib(["Helper.php<br/>Helpers"]):::core
    end

    %% Application Layer
    subgraph "Application MVC" 
        direction TB
        Controllers["app/Controller/*"]:::app
        Models["app/Model/*"]:::app
        Views["app/Views/*"]:::app
    end

    %% Database
    subgraph "Database" 
        direction TB
        DBCylinder(["MySQL/MSSQL/SQLite"]):::db
    end

    %% Public Assets
    subgraph "Public Assets" 
        direction TB
        CSS(["public/css/style.css"]):::assets
    end

    %% Data Flows
    Browser -->|HTTP Request| WebServer
    CLIClient -->|CLI Request| indexphp
    WebServer -->|forwards to| indexphp
    indexphp -->|load| autoload
    indexphp -->|load| dotenv
    indexphp -->|load config| DatabaseConfig
    indexphp -->|include| routes
    routes -->|dispatch| Controllers
    Controllers -->|invoke| ModelBase
    Controllers -->|call| Models
    ModelBase -->|connect| DBCylinder
    Models -->|SQL Queries| DBCylinder
    DBCylinder -->|results| Models
    Controllers -->|render| Views
    Views -->|serve HTML| Browser
    Browser <-->|assets GET| CSS

    %% Click Events
    click indexphp "https://github.com/vgalvoso/spm/blob/master/index.php"
    click routes "https://github.com/vgalvoso/spm/blob/master/routes.php"
    click autoload "https://github.com/vgalvoso/spm/blob/master/autoload.php"
    click composer "https://github.com/vgalvoso/spm/blob/master/composer.json"
    click dotenv "https://github.com/vgalvoso/spm/blob/master/app/Libraries/DotEnv.php"
    click DatabaseConfig "https://github.com/vgalvoso/spm/blob/master/app/Config/Database.php"
    click ModelBase "https://github.com/vgalvoso/spm/blob/master/app/Libraries/Model.php"
    click HelperLib "https://github.com/vgalvoso/spm/blob/master/app/Libraries/Helper.php"
    click Controllers "https://github.com/vgalvoso/spm/blob/master/app/Controller/Home.php"
    click Models "https://github.com/vgalvoso/spm/blob/master/app/Model/HomeModel.php"
    click Views "https://github.com/vgalvoso/spm/blob/master/app/Views/home.php"
    click Views "https://github.com/vgalvoso/spm/blob/master/app/Views/section/header.php"
    click Views "https://github.com/vgalvoso/spm/blob/master/app/Views/section/footer.php"
    click CSS "https://github.com/vgalvoso/spm/blob/master/public/css/style.css"
    click htaccess "https://github.com/vgalvoso/spm/blob/master/.htaccess"
    click dotenv "https://github.com/vgalvoso/spm/blob/master/.env"
    click README "https://github.com/vgalvoso/spm/blob/master/README.md"
    click LICENSE "https://github.com/vgalvoso/spm/tree/master/LICENSE"

    %% Styles
    classDef external fill:#f9f,stroke:#333,stroke-width:1px
    classDef core fill:#bbf,stroke:#333,stroke-width:1px
    classDef app fill:#bfb,stroke:#333,stroke-width:1px
    classDef db fill:#fbf,stroke:#333,stroke-width:1px
    classDef assets fill:#ffb,stroke:#333,stroke-width:1px
```
