VOOZH about

URL: https://deepwiki.com/hypervel/filesystem/1.2-core-concepts

⇱ Core Concepts | hypervel/filesystem | DeepWiki


Loading...
Menu

Core Concepts

This document explains the fundamental architectural concepts that underpin the Hypervel Filesystem package. It covers disks, drivers, the Factory pattern implementation, contract-based abstractions, and configuration-driven behavior. Understanding these concepts is essential for working effectively with the package.

For implementation details of specific drivers, see Filesystem Drivers. For advanced usage patterns like pooling and custom drivers, see Advanced Features and Extending and Customization.


Disks

A disk represents a named, configured storage instance in your application. Each disk is defined in the filesystems.php configuration file and corresponds to a specific storage backend (local directory, S3 bucket, GCS bucket, etc.).

Disk Characteristics

CharacteristicDescription
NameA unique identifier (e.g., 'local', 's3', 'backup') used to retrieve the disk instance
DriverThe storage backend type ('local', 's3', 'gcs', 'ftp', 'sftp')
ConfigurationDriver-specific settings like paths, credentials, visibility, pooling options
LifecycleCreated on first access and cached by FilesystemManager for reuse

The FilesystemManager maintains a cache of resolved disk instances in its $disks property src/FilesystemManager.php46 When you call disk('s3'), the manager either returns the cached instance or creates a new one by resolving the configuration.

Diagram: Disk Resolution Flow


Sources:


Drivers

A driver is the implementation adapter that handles storage operations for a specific backend technology. Each driver translates the unified Filesystem API into backend-specific operations.

Available Drivers

DriverClassBackendPoolableCloud Interface
localLocalFilesystemAdapterLocal filesystemNoYes
s3AwsS3V3AdapterAmazon S3YesYes
gcsGoogleCloudStorageAdapterGoogle Cloud StorageYesYes
ftpFilesystemAdapterFTP serverNoNo
sftpFilesystemAdapterSFTP serverNoNo
scopedDynamicPrefixed sub-diskInheritsInherits

Driver Creation Methods

The FilesystemManager uses naming conventions to create drivers. For each driver type, there is a corresponding creator method:

The manager constructs the method name dynamically: 'create' . ucfirst($driver) . 'Driver' src/FilesystemManager.php146

Diagram: Driver Creation to Code Mapping


Sources:


The Factory Pattern

The package implements the Factory pattern through the Factory contract and its concrete implementation, FilesystemManager. This pattern centralizes instance creation, configuration, and caching.

Factory Contract

The Factory interface defines a single method:

public function disk(?string $name = null): Filesystem;

This contract is implemented by FilesystemManager src/FilesystemManager.php39

FilesystemManager Responsibilities

ResponsibilityImplementation
Configuration ResolutiongetConfig() reads from filesystems.disks.{name} src/FilesystemManager.php417-421
Driver CreationDynamic method dispatch to create{Driver}Driver() methods
Instance CachingStores created instances in $disks array src/FilesystemManager.php46
Default DriversgetDefaultDriver() and getDefaultCloudDriver() src/FilesystemManager.php426-439
Custom Driversextend() method registers custom creators src/FilesystemManager.php470-479
Pooling Management$poolables array and createPoolProxy() method src/FilesystemManager.php61

Diagram: Factory Pattern Implementation


Sources:


Contracts and Interfaces

The package uses interface-based design to ensure all drivers implement a consistent API. This allows polymorphic usage - any driver can be swapped for another without changing application code.

Contract Hierarchy

1. Factory Contract

Defines how to obtain filesystem instances. Implemented by FilesystemManager.

MethodReturn TypePurpose
disk(?string $name)FilesystemGet a named disk instance

src/Contracts/Factory.php7-13

2. Filesystem Contract

The base interface defining core storage operations. All drivers must implement this interface either directly or through FilesystemAdapter.

Operation CategoryMethods
File Operationsexists(), get(), put(), delete(), copy(), move()
Stream OperationsreadStream(), readStreamRange(), writeStream()
Upload OperationsputFile(), putFileAs()
Metadata Operationssize(), lastModified(), getVisibility(), setVisibility()
Directory Operationsfiles(), allFiles(), directories(), allDirectories(), makeDirectory(), deleteDirectory()
Text Operationsprepend(), append()

src/Contracts/Filesystem.php11-155

3. Cloud Contract

Extends Filesystem with URL generation capabilities for cloud storage. Implemented by cloud drivers (AwsS3V3Adapter, GoogleCloudStorageAdapter) and LocalFilesystemAdapter.

MethodReturn TypePurpose
url(string $path)stringGet public URL for a file

src/Contracts/Cloud.php7-13

Visibility Constants

The Filesystem interface defines two visibility constants:

These constants are used throughout the system to control file access permissions.

Diagram: Contract Implementation Mapping


Sources:


Configuration-Driven Architecture

The entire system is driven by configuration arrays. Each disk's behavior is determined by its configuration, which is read from config/autoload/filesystems.php.

Configuration Structure

filesystems:
 default: 'local' # Default disk name
 cloud: 's3' # Default cloud disk name
 disks:
 disk_name:
 driver: 'local|s3|gcs|...' # Driver type
 ...driver-specific options... # Credentials, paths, etc.
 pool: [...] # Pooling config (optional)
 prefix: '...' # Path prefix (optional)
 read-only: true|false # Read-only mode (optional)

Configuration Resolution Flow

The manager resolves configuration in this order:

  1. Named Disk: disk('name') → reads filesystems.disks.name src/FilesystemManager.php417-421
  2. Default Disk: disk() → reads filesystems.default src/FilesystemManager.php426-430
  3. Cloud Disk: cloud() → reads filesystems.cloud src/FilesystemManager.php435-439
  4. On-Demand: build($config) → uses provided config directly src/FilesystemManager.php103-109

Driver Selection Logic

The manager determines which driver to create based on the driver key:

  1. Check if driver has a custom creator in $customCreators src/FilesystemManager.php135-143
  2. Build method name: 'create' . ucfirst($driver) . 'Driver' src/FilesystemManager.php146
  3. Check if method exists src/FilesystemManager.php148-150
  4. Throw exception if unsupported src/FilesystemManager.php149

Diagram: Configuration to Instance Flow


Sources:


Abstraction Layers

The system uses multiple abstraction layers to separate concerns and provide flexibility.

Layer Architecture

LayerComponentsResponsibility
Contract LayerFactory, Filesystem, CloudDefine interface contracts
Manager LayerFilesystemManagerCreate, configure, cache instances
Adapter LayerFilesystemAdapter, LocalFilesystemAdapter, etc.Implement contracts, add features
Flysystem LayerLeague\Flysystem\Filesystem, FlysystemAdapterLow-level storage operations
SDK LayerS3Client, StorageClientCloud provider APIs

Flysystem Integration

All drivers are built on top of League Flysystem src/FilesystemManager.php380-400 The createFlysystem() method:

  1. Wraps adapter in ReadOnlyFilesystemAdapter if read-only is true src/FilesystemManager.php382-385
  2. Wraps adapter in PathPrefixedAdapter if prefix is set src/FilesystemManager.php387-390
  3. Creates Flysystem instance with configuration src/FilesystemManager.php392-399

This layering allows the package to leverage Flysystem's battle-tested implementations while adding framework-specific features like pooling, URL generation, and HTTP responses.

Diagram: Abstraction Layer Stack


Sources:


Pooling and Special Drivers

Poolable Drivers

The manager maintains a $poolables array containing drivers that support connection pooling src/FilesystemManager.php61 Currently, only cloud drivers ('s3' and 'gcs') are poolable.

When resolving a poolable driver, the manager:

  1. Checks if driver is in $poolables src/FilesystemManager.php133
  2. Creates driver wrapped in FilesystemPoolProxy src/FilesystemManager.php136-141
  3. Pool configuration is read from $config['pool'] src/FilesystemManager.php140

Custom Drivers

Custom drivers can be registered using the extend() method src/FilesystemManager.php470-479:

Parameters:
- string $driver: Driver name
- Closure $callback: Creator function
- bool $poolable: Whether to enable pooling (default: false)

The callback receives ($app, $config) and must return a Filesystem instance src/FilesystemManager.php168

On-Demand Disks

The build() method creates temporary disk instances without caching src/FilesystemManager.php103-109:

  • Accepts string path → creates local driver with that root
  • Accepts array config → creates driver from config
  • Useful for temporary or one-off operations

Sources: