# perezcontrerasluis/apiproject architecture

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

- Interactive diagram: https://gitdiagram.com/perezcontrerasluis/apiproject
- Repository: https://github.com/perezcontrerasluis/apiproject

## Overview

1. **What this repo is**
   - A containerized PHP 8.2 REST API built without a framework.
   - The application uses a classic layered design: HTTP entrypoint, routing, middleware, controllers, services, repositories, and simple domain/DTO classes.
   - The current PHP application surface in this tree is centered on **authentication** and **categories**; the database schema also includes users, products, suppliers, and inventory movements.

2. **HTTP entrypoint and web boundary**
   - Requests enter through `public/index.php`, which acts as the front controller.
   - `public/.htaccess` supports Apache URL rewriting so all API routes are funneled into the front controller instead of hitting PHP files directly.
   - This is the main boundary between the web server and the application code.

3. **Routing and request dispatch**
   - Route definitions live in `src/routes.php`.
   - `src/Core/Router.php` is responsible for matching the incoming path/method to a controller action.
   - `src/Core/Request.php` and `src/Core/Response.php` likely wrap request parsing and JSON response formatting, keeping HTTP concerns out of business logic.

4. **Middleware pipeline**
   - `src/Core/Middleware.php` provides the pipeline mechanism.
   - Concrete middleware lives in `src/Middleware/`:
     - `CorsMiddleware.php` handles cross-origin policy.
     - `AuthMiddleware.php` protects authenticated endpoints via JWT validation.
   - This sits between routing and controller execution, so cross-cutting concerns are enforced consistently.

5. **Controller layer**
   - Controllers are the HTTP-facing adaptors: `src/Controllers/AuthController.php` and `src/Controllers/CategoryController.php`.
   - They translate request input into service calls and return structured responses.
   - Controllers do not appear to contain persistence logic; they delegate that to services.

6. **Service layer**
   - Business rules are concentrated in `src/Services/AuthService.php` and `src/Services/CategoryService.php`.
   - This layer coordinates validation, token generation, data shaping, and repository access.
   - It is the main orchestration layer between HTTP and storage.

7. **Persistence layer**
   - Data access is isolated in `src/Repositories/UserRepository.php` and `src/Repositories/CategoryRepository.php`.
   - Repositories are the only layer that should talk directly to the database abstraction.
   - `src/Core/Database.php` is the shared DB connection/abstraction used underneath.

8. **Auth and token flow**
   - `src/Core/JwtService.php` encapsulates JWT creation and verification.
   - Login follows the path: controller → auth service → user repository → JWT service → response.
   - Protected requests rely on `AuthMiddleware` to validate the bearer token before the controller runs.

9. **Domain objects and response shaping**
   - Models in `src/Models/` represent core entities such as `User` and `Category`.
   - DTOs in `src/DTOs/` (`UserDTO`, `CategoryDTO`, `CategoryCollectionDTO`) are used to return stable, API-friendly shapes instead of raw database rows.
   - This separation helps keep the API contract independent from schema details.

10. **Manual dependency wiring**
   - `src/Factories/RepositoryFactory.php` and `src/Factories/ServiceFactory.php` centralize object creation.
   - This is the repo’s lightweight dependency injection boundary: controllers and middleware should receive ready-made services, not construct them ad hoc.
   - It keeps the architecture decoupled without bringing in a framework container.

11. **Database structure and lifecycle**
   - Schema creation lives in `database/migrations/*.sql`.
   - Initial data lives in `database/seeders/*.sql`.
   - The migration set covers five tables: users, categories, suppliers, products, and inventory movements.
   - `cli/setup.php` is the operational bootstrap for applying migrations and seed data.

12. **Infrastructure and runtime**
   - `docker-compose.yml` defines the runtime stack, with PHP/Apache and MySQL running in containers.
   - `docker/php/Dockerfile`, `entrypoint.sh`, and `php.ini` define the PHP runtime behavior.
   - The application is designed to run consistently across dev/testing/prod-like environments through Docker rather than local host installs.

13. **Testing and operational tooling**
   - `postman/` contains a Postman collection and environment for exercising the API.
   - The README documents example endpoints for categories and auth, plus JWT-based testing credentials.
   - These assets are not part of runtime architecture, but they define how the API is validated and consumed during development.

14. **Overall request/data flow**
   - The main flow is: browser/client → Apache/front controller → router → middleware → controller → service → repository → database → DTO/response → client.
   - The main trust boundary is JWT-authenticated access, enforced before protected controller actions.
   - The main architectural separation is between HTTP concerns, business logic, and persistence, with the database and Docker stack kept outside the application core.

Main entry points: [`src/Services/AuthService.php`](https://github.com/perezcontrerasluis/apiproject/blob/main/src/Services/AuthService.php) (Auth service), [`src/Services/CategoryService.php`](https://github.com/perezcontrerasluis/apiproject/blob/main/src/Services/CategoryService.php) (Category service), [`public/index.php`](https://github.com/perezcontrerasluis/apiproject/blob/main/public/index.php) (Front controller), [`src/Core/Router.php`](https://github.com/perezcontrerasluis/apiproject/blob/main/src/Core/Router.php) (Router)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_web["Web boundary"]
  node_public_index["Front controller<br/>http entrypoint<br/>[index.php]"]
  node_public_htaccess["Apache rewrite<br/>web config<br/>[.htaccess]"]
end

subgraph group_app["Application core"]
  node_routes["Routes<br/>route map<br/>[routes.php]"]
  node_router(("Router<br/>dispatcher<br/>[Router.php]"))
  node_request["Request<br/>http wrapper<br/>[Request.php]"]
  node_response["Response<br/>[Response.php]"]
  node_middleware_base(("Pipeline<br/>middleware core<br/>[Middleware.php]"))
  node_cors["CORS<br/>middleware<br/>[CorsMiddleware.php]"]
  node_auth_mw["Auth guard<br/>middleware<br/>[AuthMiddleware.php]"]
  node_auth_controller["Auth controller<br/>[AuthController.php]"]
  node_category_controller["Category controller"]
  node_auth_service["Auth service<br/>[AuthService.php]"]
  node_category_service["Category service"]
  node_service_factory(("Service factory<br/>[ServiceFactory.php]"))
  node_repository_factory(("Repository factory"))
end

subgraph group_data["Domain and data"]
  node_user_repo["User repo<br/>[UserRepository.php]"]
  node_category_repo["Category repo"]
  node_database[("Database<br/>db abstraction<br/>[Database.php]")]
  node_jwt{{"JWT service<br/>token service<br/>[JwtService.php]"}}
  node_user_model["User model<br/>domain model<br/>[User.php]"]
  node_category_model["Category model<br/>domain model<br/>[Category.php]"]
  node_user_dto["User DTO<br/>[UserDTO.php]"]
  node_category_dto["Category DTO<br/>[CategoryDTO.php]"]
  node_category_list_dto["Category list DTO"]
end

subgraph group_ops["Runtime and setup"]
  node_docker_compose["Compose stack<br/>runtime stack<br/>[docker-compose.yml]"]
  node_php_dockerfile["PHP image<br/>container build"]
  node_entrypoint["Entrypoint<br/>container startup<br/>[entrypoint.sh]"]
  node_php_ini["PHP config<br/>runtime config<br/>[php.ini]"]
  node_setup_cli["Setup CLI<br/>bootstrap tool<br/>[setup.php]"]
  node_migrations["Migrations<br/>schema scripts"]
  node_seeders["Seed data<br/>seed scripts<br/>[001_seed_users.sql]"]
end

node_public_htaccess -->|"rewrites to"| node_public_index
node_public_index -->|"loads"| node_routes
node_public_index -->|"dispatches via"| node_router
node_public_index -->|"wraps"| node_request
node_router -->|"passes through"| node_middleware_base
node_middleware_base -->|"includes"| node_cors
node_middleware_base -->|"includes"| node_auth_mw
node_router -->|"invokes"| node_auth_controller
node_router -->|"invokes"| node_category_controller
node_auth_mw -->|"verifies"| node_jwt
node_auth_controller -->|"delegates to"| node_auth_service
node_category_controller -->|"delegates to"| node_category_service
node_service_factory -->|"builds"| node_auth_service
node_service_factory -->|"builds"| node_category_service
node_repository_factory -->|"builds"| node_user_repo
node_repository_factory -->|"builds"| node_category_repo
node_auth_service -->|"reads from"| node_user_repo
node_auth_service -->|"issues"| node_jwt
node_category_service -->|"reads from"| node_category_repo
node_user_repo -->|"uses"| node_database
node_category_repo -->|"uses"| node_database
node_auth_service -.->|"uses"| node_user_model
node_category_service -.->|"uses"| node_category_model
node_auth_service -.->|"returns"| node_user_dto
node_category_service -.->|"returns"| node_category_dto
node_category_service -.->|"returns"| node_category_list_dto
node_setup_cli -->|"applies"| node_migrations
node_setup_cli -->|"loads"| node_seeders
node_docker_compose -->|"builds with"| node_php_dockerfile
node_docker_compose -->|"starts with"| node_entrypoint
node_php_dockerfile -->|"configures"| node_php_ini

click node_public_index "https://github.com/perezcontrerasluis/apiproject/blob/main/public/index.php"
click node_public_htaccess "https://github.com/perezcontrerasluis/apiproject/blob/main/public/.htaccess"
click node_routes "https://github.com/perezcontrerasluis/apiproject/blob/main/src/routes.php"
click node_router "https://github.com/perezcontrerasluis/apiproject/blob/main/src/Core/Router.php"
click node_request "https://github.com/perezcontrerasluis/apiproject/blob/main/src/Core/Request.php"
click node_response "https://github.com/perezcontrerasluis/apiproject/blob/main/src/Core/Response.php"
click node_middleware_base "https://github.com/perezcontrerasluis/apiproject/blob/main/src/Core/Middleware.php"
click node_cors "https://github.com/perezcontrerasluis/apiproject/blob/main/src/Middleware/CorsMiddleware.php"
click node_auth_mw "https://github.com/perezcontrerasluis/apiproject/blob/main/src/Middleware/AuthMiddleware.php"
click node_auth_controller "https://github.com/perezcontrerasluis/apiproject/blob/main/src/Controllers/AuthController.php"
click node_category_controller "https://github.com/perezcontrerasluis/apiproject/blob/main/src/Controllers/CategoryController.php"
click node_auth_service "https://github.com/perezcontrerasluis/apiproject/blob/main/src/Services/AuthService.php"
click node_category_service "https://github.com/perezcontrerasluis/apiproject/blob/main/src/Services/CategoryService.php"
click node_service_factory "https://github.com/perezcontrerasluis/apiproject/blob/main/src/Factories/ServiceFactory.php"
click node_repository_factory "https://github.com/perezcontrerasluis/apiproject/blob/main/src/Factories/RepositoryFactory.php"
click node_user_repo "https://github.com/perezcontrerasluis/apiproject/blob/main/src/Repositories/UserRepository.php"
click node_category_repo "https://github.com/perezcontrerasluis/apiproject/blob/main/src/Repositories/CategoryRepository.php"
click node_database "https://github.com/perezcontrerasluis/apiproject/blob/main/src/Core/Database.php"
click node_jwt "https://github.com/perezcontrerasluis/apiproject/blob/main/src/Core/JwtService.php"
click node_user_model "https://github.com/perezcontrerasluis/apiproject/blob/main/src/Models/User.php"
click node_category_model "https://github.com/perezcontrerasluis/apiproject/blob/main/src/Models/Category.php"
click node_user_dto "https://github.com/perezcontrerasluis/apiproject/blob/main/src/DTOs/UserDTO.php"
click node_category_dto "https://github.com/perezcontrerasluis/apiproject/blob/main/src/DTOs/CategoryDTO.php"
click node_category_list_dto "https://github.com/perezcontrerasluis/apiproject/blob/main/src/DTOs/CategoryCollectionDTO.php"
click node_docker_compose "https://github.com/perezcontrerasluis/apiproject/blob/main/docker-compose.yml"
click node_php_dockerfile "https://github.com/perezcontrerasluis/apiproject/tree/main/docker/php/Dockerfile"
click node_entrypoint "https://github.com/perezcontrerasluis/apiproject/blob/main/docker/php/entrypoint.sh"
click node_php_ini "https://github.com/perezcontrerasluis/apiproject/blob/main/docker/php/php.ini"
click node_setup_cli "https://github.com/perezcontrerasluis/apiproject/blob/main/cli/setup.php"
click node_migrations "https://github.com/perezcontrerasluis/apiproject/blob/main/database/migrations/001_create_users_table.sql"
click node_seeders "https://github.com/perezcontrerasluis/apiproject/blob/main/database/seeders/001_seed_users.sql"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_public_index,node_public_htaccess toneBlue
class node_routes,node_router,node_request,node_response,node_middleware_base,node_cors,node_auth_mw,node_auth_controller,node_category_controller,node_auth_service,node_category_service,node_service_factory,node_repository_factory toneAmber
class node_user_repo,node_category_repo,node_database,node_jwt,node_user_model,node_category_model,node_user_dto,node_category_dto,node_category_list_dto toneMint
class node_docker_compose,node_php_dockerfile,node_entrypoint,node_php_ini,node_setup_cli,node_migrations,node_seeders toneRose
```

## Components

### Web boundary

HTTP entrypoint and request routing into the API

- [Front controller](https://github.com/perezcontrerasluis/apiproject/blob/main/public/index.php): http entrypoint · Main HTTP entrypoint for the API · `public/index.php`
- [Apache rewrite](https://github.com/perezcontrerasluis/apiproject/blob/main/public/.htaccess): web config · Routes all requests into the front controller · `public/.htaccess`

### Application core

Routing, middleware, controllers, services, and factories

- [Routes](https://github.com/perezcontrerasluis/apiproject/blob/main/src/routes.php): route map · Application route definitions · `src/routes.php`
- [Router](https://github.com/perezcontrerasluis/apiproject/blob/main/src/Core/Router.php): dispatcher · Matches HTTP requests to controller actions · `src/Core/Router.php`
- [Request](https://github.com/perezcontrerasluis/apiproject/blob/main/src/Core/Request.php): http wrapper · Encapsulates incoming request data · `src/Core/Request.php`
- [Response](https://github.com/perezcontrerasluis/apiproject/blob/main/src/Core/Response.php): response wrapper · Formats outgoing HTTP responses · `src/Core/Response.php`
- [Pipeline](https://github.com/perezcontrerasluis/apiproject/blob/main/src/Core/Middleware.php): middleware core · Executes middleware around request handling · `src/Core/Middleware.php`
- [CORS](https://github.com/perezcontrerasluis/apiproject/blob/main/src/Middleware/CorsMiddleware.php): middleware · Applies cross-origin response policy · `src/Middleware/CorsMiddleware.php`
- [Auth guard](https://github.com/perezcontrerasluis/apiproject/blob/main/src/Middleware/AuthMiddleware.php): middleware · Validates JWT bearer access for protected routes · `src/Middleware/AuthMiddleware.php`
- [Auth controller](https://github.com/perezcontrerasluis/apiproject/blob/main/src/Controllers/AuthController.php): controller · HTTP adaptor for authentication actions · `src/Controllers/AuthController.php`
- [Category controller](https://github.com/perezcontrerasluis/apiproject/blob/main/src/Controllers/CategoryController.php): controller · HTTP adaptor for category endpoints · `src/Controllers/CategoryController.php`
- [Auth service](https://github.com/perezcontrerasluis/apiproject/blob/main/src/Services/AuthService.php): service · Coordinates login, token creation, and user lookup · `src/Services/AuthService.php`
- [Category service](https://github.com/perezcontrerasluis/apiproject/blob/main/src/Services/CategoryService.php): service · Coordinates category business rules and shaping · `src/Services/CategoryService.php`
- [Service factory](https://github.com/perezcontrerasluis/apiproject/blob/main/src/Factories/ServiceFactory.php): factory · Builds service instances for the app · `src/Factories/ServiceFactory.php`
- [Repository factory](https://github.com/perezcontrerasluis/apiproject/blob/main/src/Factories/RepositoryFactory.php): factory · Builds repository instances for the app · `src/Factories/RepositoryFactory.php`

### Domain and data

Models, DTOs, repositories, and shared database access

- [User repo](https://github.com/perezcontrerasluis/apiproject/blob/main/src/Repositories/UserRepository.php): repository · Database access for users · `src/Repositories/UserRepository.php`
- [Category repo](https://github.com/perezcontrerasluis/apiproject/blob/main/src/Repositories/CategoryRepository.php): repository · Database access for categories · `src/Repositories/CategoryRepository.php`
- [Database](https://github.com/perezcontrerasluis/apiproject/blob/main/src/Core/Database.php): db abstraction · Shared database connection and access layer · `src/Core/Database.php`
- [JWT service](https://github.com/perezcontrerasluis/apiproject/blob/main/src/Core/JwtService.php): token service · Creates and verifies JSON Web Tokens · `src/Core/JwtService.php`
- [User model](https://github.com/perezcontrerasluis/apiproject/blob/main/src/Models/User.php): domain model · User entity representation · `src/Models/User.php`
- [Category model](https://github.com/perezcontrerasluis/apiproject/blob/main/src/Models/Category.php): domain model · Category entity representation · `src/Models/Category.php`
- [User DTO](https://github.com/perezcontrerasluis/apiproject/blob/main/src/DTOs/UserDTO.php): dto · Stable API shape for user data · `src/DTOs/UserDTO.php`
- [Category DTO](https://github.com/perezcontrerasluis/apiproject/blob/main/src/DTOs/CategoryDTO.php): dto · Stable API shape for category data · `src/DTOs/CategoryDTO.php`
- [Category list DTO](https://github.com/perezcontrerasluis/apiproject/blob/main/src/DTOs/CategoryCollectionDTO.php): dto · Stable API shape for category collections · `src/DTOs/CategoryCollectionDTO.php`

### Runtime and setup

Container runtime, migrations, seeders, and bootstrap tooling

- [Compose stack](https://github.com/perezcontrerasluis/apiproject/blob/main/docker-compose.yml): runtime stack · Defines the PHP and MySQL container runtime · `docker-compose.yml`
- [PHP image](https://github.com/perezcontrerasluis/apiproject/tree/main/docker/php/Dockerfile): container build · Builds the PHP/Apache runtime image · `docker/php/Dockerfile`
- [Entrypoint](https://github.com/perezcontrerasluis/apiproject/blob/main/docker/php/entrypoint.sh): container startup · Container startup script · `docker/php/entrypoint.sh`
- [PHP config](https://github.com/perezcontrerasluis/apiproject/blob/main/docker/php/php.ini): runtime config · PHP runtime tuning and settings · `docker/php/php.ini`
- [Setup CLI](https://github.com/perezcontrerasluis/apiproject/blob/main/cli/setup.php): bootstrap tool · Applies migrations and seed data · `cli/setup.php`
- [Migrations](https://github.com/perezcontrerasluis/apiproject/blob/main/database/migrations/001_create_users_table.sql): schema scripts · SQL files that create the database schema · `database/migrations/001_create_users_table.sql`
- [Seed data](https://github.com/perezcontrerasluis/apiproject/blob/main/database/seeders/001_seed_users.sql): seed scripts · SQL files that load initial data · `database/seeders/001_seed_users.sql`

## Connections

- Apache rewrite → Front controller: rewrites to · Apache funnels API requests into the front controller
- Front controller → Routes: loads · Bootstraps route definitions
- Front controller → Router: dispatches via · Front controller hands requests to the router
- Front controller → Request: wraps · Creates request abstraction from incoming HTTP data
- Router → Pipeline: passes through · Routes execute through the middleware pipeline
- Pipeline → CORS: includes · Applies cross-origin middleware
- Pipeline → Auth guard: includes · Applies authentication middleware where required
- Router → Auth controller: invokes · Resolves auth endpoints to controller actions
- Router → Category controller: invokes · Resolves category endpoints to controller actions
- Auth guard → JWT service: verifies · Checks bearer tokens before protected actions
- Auth controller → Auth service: delegates to · Controller forwards auth work to the service layer
- Category controller → Category service: delegates to · Controller forwards category work to the service layer
- Service factory → Auth service: builds · Provides configured auth service instances
- Service factory → Category service: builds · Provides configured category service instances
- Repository factory → User repo: builds · Provides user repository instances
- Repository factory → Category repo: builds · Provides category repository instances
- Auth service → User repo: reads from · Auth depends on user lookup for login
- Auth service → JWT service: issues · Creates tokens after successful authentication
- Category service → Category repo: reads from · Category operations depend on repository access
- User repo → Database: uses · Repository talks to the shared database layer
- Category repo → Database: uses · Repository talks to the shared database layer
- Auth service → User model: uses · Maps auth-related user data into domain objects
- Category service → Category model: uses · Works with category domain objects
- Auth service → User DTO: returns · Shapes user-facing auth responses
- Category service → Category DTO: returns · Shapes single-category responses
- Category service → Category list DTO: returns · Shapes category collection responses
- Setup CLI → Migrations: applies · Bootstrap process runs schema migrations
- Setup CLI → Seed data: loads · Bootstrap process loads initial seed data
- Compose stack → PHP image: builds with · Compose stack uses the PHP image build
- Compose stack → Entrypoint: starts with · Container startup is controlled by the entrypoint script
- PHP image → PHP config: configures · Image includes PHP runtime settings
