# jsdevart/laravel-file-vault architecture

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

- Interactive diagram: https://gitdiagram.com/jsdevart/laravel-file-vault
- Repository: https://github.com/jsdevart/laravel-file-vault

## Overview

1. **What this repository is**
   - This is a Laravel package, not a standalone app.
   - Its boundary is “private file storage primitives” for Laravel apps: storing files under a structured namespace, generating URLs, checking existence, deleting, and resolving a file to a real path when needed.

2. **Package bootstrap**
   - `src/FileVaultServiceProvider.php` is the integration point with Laravel’s container and config system.
   - The package is discovered automatically by Laravel and reads `config/file-vault.php`, which controls the default disk, signed URL expiry, the route name used for serving files, and whether to force app-routed URLs instead of driver-native temporary URLs.

3. **Core registry subsystem**
   - `src/FileStorageRegistry.php` is the central lookup mechanism.
   - The application registers one storage service per “context” such as `user`, `imports`, or `avatars`.
   - This registry is the main architectural boundary between the host app and the package: callers ask for a context, and the registry returns the appropriate file-storage service.

4. **Storage-service abstraction**
   - `src/Contracts/FileStorageInterface.php` defines the contract for file storage behavior.
   - `src/Services/BaseFileStorageService.php` provides the reusable implementation used by app-specific subclasses.
   - The base service is where the package likely centralizes file naming, path composition, disk selection, URL generation, deletion, existence checks, and path resolution.

5. **Context + element path model**
   - The repository is organized around a two-level logical namespace:
     - a **context** root, e.g. `user`
     - an **element** category, e.g. `picture` or `document`
   - The README shows the physical layout as `{context}/{element_folder}/XX/XX/{uuid}.{ext}`, which means the storage service is responsible for mapping business labels into a deterministic directory structure.

6. **URL generation boundary**
   - Signed URL behavior is a key architectural feature.
   - For local/path-based disks, the package expects the host app to expose a signed route that streams the file.
   - For cloud disks such as S3, `getUrl()` normally delegates to the storage driver’s temporary URL capability.
   - `src/Support/ServeSignedRoutePolicy.php` encapsulates the decision of when to use the app route versus the storage driver’s own temporary URL, especially when `force_serve_route` is enabled.

7. **Serving private files is intentionally outside the package**
   - The package does not ship a controller.
   - That keeps authorization, middleware, cache headers, and response streaming in the host application.
   - The package only standardizes the signed-route policy and the route name; the actual serving endpoint remains an application responsibility.

8. **Real-path resolution subsystem**
   - `src/Support/FilePathResult.php` and `src/Support/TemporaryFile.php` support the `getPath()` workflow.
   - For local disks, the package can return the real filesystem path directly.
   - For remote disks, it downloads to a temp file so downstream libraries that require an actual path can still operate.
   - The result object owns the temp file’s lifetime, which is an important design boundary: keep the result object alive as long as the file is needed.

9. **Infrastructure and external storage**
   - The package is designed around Laravel’s filesystem abstraction, so the actual backend may be local storage or an S3-compatible service.
   - The architecture changes depending on the disk:
     - local/path-based disks can be streamed from disk and resolved with `Storage::path()`
     - remote disks may need signed URLs or temporary downloads to a local temp file
   - The `force_serve_route` option exists specifically for environments where the storage endpoint is not browser-reachable, such as internal MinIO or Docker-only hosts.

10. **Developer extension point**
    - `stubs/ExampleStorageService.php` is the intended template for app developers to copy and customize.
    - The usual extension model is: subclass `BaseFileStorageService`, define the context root, and optionally override how element names map to subfolders.

11. **Testing surface**
    - The test suite is small and focused, with `tests/Unit/ServeSignedRoutePolicyTest.php` validating the signed-route decision logic.
    - That suggests the package’s most important behavioral seam is the URL-serving policy rather than heavy storage integration tests.

12. **Architectural takeaway for a diagram**
    - The main flow is:
      - application code asks `FileStorageRegistry` for a context service
      - the service stores/reads/deletes via Laravel Storage
      - URL generation branches through signed-route policy or driver temporary URLs
      - `getPath()` may return either a direct path or a temp-file-backed result
    - The key boundaries are: host app vs package, context registry vs storage implementation, and local path vs remote object storage.

Main entry points: [`src/Services/BaseFileStorageService.php`](https://github.com/jsdevart/laravel-file-vault/blob/main/src/Services/BaseFileStorageService.php) (Base service), [`src/Support/ServeSignedRoutePolicy.php`](https://github.com/jsdevart/laravel-file-vault/blob/main/src/Support/ServeSignedRoutePolicy.php) (Serve policy), [`src/FileVaultServiceProvider.php`](https://github.com/jsdevart/laravel-file-vault/blob/main/src/FileVaultServiceProvider.php) (Service provider), [`src/FileStorageRegistry.php`](https://github.com/jsdevart/laravel-file-vault/blob/main/src/FileStorageRegistry.php) (Registry)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_group_laravel["Laravel integration"]
  node_node_provider["Service provider<br/>bootstrap"]
  node_node_config["Config<br/>[file-vault.php]"]
end

subgraph group_group_core["Core storage"]
  node_node_registry["Registry<br/>context lookup"]
  node_node_contract["Storage contract<br/>interface"]
  node_node_base_service["Base service<br/>implementation base"]
  node_node_example_service["Example service<br/>stub template"]
end

subgraph group_group_support["Support utilities"]
  node_node_policy["Serve policy<br/>url policy"]
  node_node_path_result["Path result<br/>path wrapper<br/>[FilePathResult.php]"]
  node_node_temp_file["Temp file<br/>temporary file<br/>[TemporaryFile.php]"]
end

subgraph group_group_external["External boundaries"]
  node_node_host_app["Host app<br/>application boundary"]
  node_node_laravel_storage["Laravel Storage<br/>filesystem facade"]
  node_node_storage_backend[("Disk backend<br/>local or object store")]
end

node_node_provider -->|"reads"| node_node_config
node_node_provider -->|"registers"| node_node_registry
node_node_registry -->|"returns"| node_node_contract
node_node_example_service -->|"extends"| node_node_base_service
node_node_base_service -->|"implements"| node_node_contract
node_node_base_service -->|"uses"| node_node_laravel_storage
node_node_base_service -->|"consults"| node_node_policy
node_node_policy -->|"uses"| node_node_config
node_node_policy -->|"targets"| node_node_host_app
node_node_base_service -->|"returns"| node_node_path_result
node_node_path_result -->|"owns"| node_node_temp_file
node_node_base_service -->|"creates"| node_node_temp_file
node_node_laravel_storage -->|"backed by"| node_node_storage_backend
node_node_base_service -->|"stores on"| node_node_storage_backend

click node_node_provider "https://github.com/jsdevart/laravel-file-vault/blob/main/src/FileVaultServiceProvider.php"
click node_node_config "https://github.com/jsdevart/laravel-file-vault/blob/main/config/file-vault.php"
click node_node_registry "https://github.com/jsdevart/laravel-file-vault/blob/main/src/FileStorageRegistry.php"
click node_node_contract "https://github.com/jsdevart/laravel-file-vault/blob/main/src/Contracts/FileStorageInterface.php"
click node_node_base_service "https://github.com/jsdevart/laravel-file-vault/blob/main/src/Services/BaseFileStorageService.php"
click node_node_example_service "https://github.com/jsdevart/laravel-file-vault/blob/main/stubs/ExampleStorageService.php"
click node_node_policy "https://github.com/jsdevart/laravel-file-vault/blob/main/src/Support/ServeSignedRoutePolicy.php"
click node_node_path_result "https://github.com/jsdevart/laravel-file-vault/blob/main/src/Support/FilePathResult.php"
click node_node_temp_file "https://github.com/jsdevart/laravel-file-vault/blob/main/src/Support/TemporaryFile.php"

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_node_provider,node_node_config toneBlue
class node_node_registry,node_node_contract,node_node_base_service,node_node_example_service toneAmber
class node_node_policy,node_node_path_result,node_node_temp_file toneMint
class node_node_host_app,node_node_laravel_storage,node_node_storage_backend toneRose
```

## Components

### Laravel integration

Package bootstrap and configuration entry points.

- [Service provider](https://github.com/jsdevart/laravel-file-vault/blob/main/src/FileVaultServiceProvider.php): bootstrap · Registers the package with Laravel and wires config/container integration. · `src/FileVaultServiceProvider.php`
- [Config](https://github.com/jsdevart/laravel-file-vault/blob/main/config/file-vault.php): package config · Controls default disk, signed URL expiry, route name, and route forcing. · `config/file-vault.php`

### Core storage

Registry and reusable file-storage implementation.

- [Registry](https://github.com/jsdevart/laravel-file-vault/blob/main/src/FileStorageRegistry.php): context lookup · Resolves a storage service for each logical context such as users or imports. · `src/FileStorageRegistry.php`
- [Storage contract](https://github.com/jsdevart/laravel-file-vault/blob/main/src/Contracts/FileStorageInterface.php): interface · Defines the file-storage behavior expected from context-specific services. · `src/Contracts/FileStorageInterface.php`
- [Base service](https://github.com/jsdevart/laravel-file-vault/blob/main/src/Services/BaseFileStorageService.php): implementation base · Reusable file-storage implementation for app-specific storage services. · `src/Services/BaseFileStorageService.php`
- [Example service](https://github.com/jsdevart/laravel-file-vault/blob/main/stubs/ExampleStorageService.php): stub template · Starter subclass developers copy and customize for a storage context. · `stubs/ExampleStorageService.php`

### Support utilities

URL policy and path-resolution helpers.

- [Serve policy](https://github.com/jsdevart/laravel-file-vault/blob/main/src/Support/ServeSignedRoutePolicy.php): url policy · Chooses between app-routed signed URLs and driver temporary URLs. · `src/Support/ServeSignedRoutePolicy.php`
- [Path result](https://github.com/jsdevart/laravel-file-vault/blob/main/src/Support/FilePathResult.php): path wrapper · Represents resolved file paths and keeps temporary-file lifetime attached. · `src/Support/FilePathResult.php`
- [Temp file](https://github.com/jsdevart/laravel-file-vault/blob/main/src/Support/TemporaryFile.php): temporary file · Local scratch file used when remote storage must be materialized on disk. · `src/Support/TemporaryFile.php`

### External boundaries

Host application and Laravel filesystem dependencies.

- **Host app**: application boundary · Owns authorization, middleware, streaming controller, and signed file route.
- **Laravel Storage**: filesystem facade · Filesystem abstraction used for local disks and remote object storage.
- **Disk backend**: local or object store · Concrete storage target such as local filesystem, S3, or MinIO.

## Connections

- Service provider → Config: reads · Loads package configuration during bootstrapping.
- Service provider → Registry: registers · Makes the registry available to the Laravel container.
- Registry → Storage contract: returns · Looks up a context service that implements the storage contract.
- Example service → Base service: extends · Template subclass builds on the reusable base implementation.
- Base service → Storage contract: implements · Provides the package’s main storage behavior behind the contract.
- Base service → Laravel Storage: uses · Performs read/write/delete and URL operations through Laravel filesystem.
- Base service → Serve policy: consults · Branches URL generation through signed-route policy.
- Serve policy → Config: uses · Respects force-route and expiry settings from config.
- Serve policy → Host app: targets · Falls back to an application-owned serving route when needed.
- Base service → Path result: returns · Produces a path result when callers need a real filesystem path.
- Path result → Temp file: owns · Keeps temporary-file lifetime tied to the result object.
- Base service → Temp file: creates · Downloads remote files to a local temp file when direct paths are unavailable.
- Laravel Storage → Disk backend: backed by · Abstracts over local and remote storage backends.
- Base service → Disk backend: stores on · Persists files into the configured disk backend.
