VOOZH about

URL: https://deepwiki.com/hypervel/filesystem/3.1-driver-architecture-and-filesystemadapter

⇱ Driver Architecture and FilesystemAdapter | hypervel/filesystem | DeepWiki


Loading...
Menu

Driver Architecture and FilesystemAdapter

Purpose and Scope

This page documents the base driver architecture and the FilesystemAdapter class, which serves as the foundation for all filesystem drivers in the package. It explains how the adapter pattern integrates with League Flysystem, the common functionality provided to all drivers, and how concrete driver implementations extend this base.

For information about specific driver implementations, see Local Filesystem Driver and Cloud Storage Drivers. For details on how the FilesystemManager orchestrates driver creation, see Factory Pattern and FilesystemManager.

The Adapter Pattern

The filesystem package uses the Adapter Pattern to wrap League Flysystem's functionality while providing additional features specific to the Hypervel/Hyperf ecosystem. This design allows the package to leverage Flysystem's mature storage abstractions while adding capabilities like connection pooling, signed URL generation, HTTP response streaming, and framework integration.

Layered Architecture


Sources: src/FilesystemAdapter.php1-92 composer.json31-51

The architecture consists of three layers:

  1. Hypervel Abstraction Layer - Provides framework-specific features and a unified API
  2. Flysystem Layer - Core storage abstraction from League Flysystem
  3. Storage Backends - Actual storage systems (local disk, cloud services)

FilesystemAdapter Class Structure

Two-Level Wrapping

The FilesystemAdapter class maintains references to both a FilesystemOperator (Flysystem's operational interface) and a FlysystemAdapter (Flysystem's storage-specific implementation). This two-level wrapping enables the adapter to delegate operations efficiently while retaining access to adapter-specific features.


Sources: src/FilesystemAdapter.php50-92 src/Contracts/Cloud.php1-50

Core Components

The FilesystemAdapter constructor initializes four key components:

ComponentTypePurpose
$driverFilesystemOperatorPrimary interface for file operations, delegates to Flysystem
$adapterFlysystemAdapterLow-level storage adapter, provides driver-specific features
$configarrayConfiguration array containing driver settings, credentials, and options
$prefixerPathPrefixerHandles path prefixing for root directories and custom prefixes

Constructor Implementation:

The constructor at src/FilesystemAdapter.php75-92 performs the following initialization:

  1. Stores the FilesystemOperator driver reference
  2. Stores the FlysystemAdapter adapter reference
  3. Stores the configuration array
  4. Creates a PathPrefixer from the root and directory_separator config values
  5. If a prefix is specified in config, applies an additional prefix layer

Sources: src/FilesystemAdapter.php75-92

Common Functionality

The FilesystemAdapter provides a comprehensive set of methods that are available to all driver implementations. These methods are organized into functional categories.

Path Management


Sources: src/FilesystemAdapter.php84-213

The path() method at src/FilesystemAdapter.php210-213 uses the PathPrefixer to convert relative paths to absolute paths based on the configured root and prefix values. The prefixer is initialized in the constructor at src/FilesystemAdapter.php84-91

File Operations (CRUD)

MethodLine RangeDescription
exists()162-165Checks if a file or directory exists via $driver->has()
fileExists()178-181Specifically checks file existence
directoryExists()194-197Specifically checks directory existence
get()218-227Reads file contents via $driver->read()
put()307-337Writes content with support for strings, resources, streams, and UploadedFile
delete()444-461Deletes one or multiple files
copy()466-477Copies a file to a new location
move()483-493Moves a file to a new location

Sources: src/FilesystemAdapter.php162-493

Multi-Type Content Handling

The put() method at src/FilesystemAdapter.php307-337 demonstrates the adapter's ability to handle multiple content types:


Sources: src/FilesystemAdapter.php307-337

Stream Operations

MethodLine RangeDescription
readStream()546-555Returns a resource handle for reading file contents
readStreamRange()563-566Base implementation throws RuntimeException (overridden by cloud drivers)
writeStream()573-584Writes a file from a resource handle

Sources: src/FilesystemAdapter.php546-584

URL Generation

The adapter provides flexible URL generation with fallback mechanisms:


Sources: src/FilesystemAdapter.php591-646

The URL generation logic at src/FilesystemAdapter.php591-646 checks multiple sources in order:

  1. Apply path prefix if configured
  2. Check if adapter has getUrl() method
  3. Check if driver has getUrl() method
  4. Fall back to FTP-specific URL logic for FtpAdapter/SftpAdapter
  5. Fall back to local URL logic for LocalAdapter
  6. Throw RuntimeException if no URL generation method is available

Metadata Operations

MethodLine RangeReturn TypeDescription
size()498-501intFile size in bytes via $driver->fileSize()
mimeType()522-531string|falseMIME type detection
lastModified()536-539intUnix timestamp of last modification
checksum()508-517string|falseFile checksum/hash
getVisibility()392-399stringReturns public or private
setVisibility()404-415boolSets file visibility

Sources: src/FilesystemAdapter.php392-539

Directory Operations

MethodLine RangeDescription
files()716-727Lists files in a directory (optionally recursive)
allFiles()732-735Lists all files recursively
directories()740-750Lists subdirectories (optionally recursive)
allDirectories()755-758Lists all subdirectories recursively
makeDirectory()763-774Creates a directory
deleteDirectory()779-790Recursively deletes a directory

Sources: src/FilesystemAdapter.php716-790

Exception Handling

All operations that can fail wrap Flysystem exceptions and respect the throw configuration option:


Sources: src/FilesystemAdapter.php845-848

The throwsExceptions() method at src/FilesystemAdapter.php845-848 checks the throw configuration option. When false, operations return false or null instead of throwing exceptions. This pattern appears throughout the adapter in methods like:

HTTP Response Integration

The FilesystemAdapter provides seamless integration with HTTP responses for serving files:

MethodLine RangePurpose
response()242-284Creates a streamed HTTP response with proper headers
download()289-292Creates a download response with Content-Disposition: attachment

Sources: src/FilesystemAdapter.php242-292

Response Flow


Sources: src/FilesystemAdapter.php242-284

The response generation at src/FilesystemAdapter.php242-284 includes:

  1. Automatic MIME type detection via mimeType()
  2. Content-Disposition header generation with filename encoding
  3. Range request support for partial content delivery
  4. Chunked streaming with configurable buffer size (64KB)

Method Delegation and Magic Methods

The FilesystemAdapter uses PHP's magic __call() method to delegate unknown method calls directly to the underlying Flysystem driver:


Sources: src/FilesystemAdapter.php859-866

The delegation at src/FilesystemAdapter.php859-866 enables:

  1. Access to all Flysystem FilesystemOperator methods without explicit wrapper methods
  2. Forward compatibility with future Flysystem versions
  3. Support for custom methods via the Macroable trait

This means methods like listContents(), publicUrl(), and others from Flysystem are automatically available on any FilesystemAdapter instance.

Traits and Extensibility

Macroable Trait

The FilesystemAdapter uses the Macroable trait from src/FilesystemAdapter.php53-55 to allow runtime method additions:


Sources: src/FilesystemAdapter.php53-55

Conditionable Trait

The Conditionable trait at src/FilesystemAdapter.php52 enables fluent conditional execution:


Sources: src/FilesystemAdapter.php52

Driver Implementation Pattern

Concrete driver implementations extend FilesystemAdapter and override specific methods to provide driver-specific functionality:


Sources: src/FilesystemAdapter.php50 src/LocalFilesystemAdapter.php1-100 src/AwsS3V3Adapter.php1-100 src/GoogleCloudStorageAdapter.php1-100

Common Override Patterns

Concrete drivers typically override the following methods:

MethodWhy OverrideDrivers That Override
temporaryUrl()Provide signed URL generationLocalFilesystemAdapter, AwsS3V3Adapter, GoogleCloudStorageAdapter
url()Provide driver-specific public URL logicLocalFilesystemAdapter, AwsS3V3Adapter, GoogleCloudStorageAdapter
readStreamRange()Enable partial content readingGoogleCloudStorageAdapter
temporaryUploadUrl()Generate pre-signed upload URLsAwsS3V3Adapter

Method Call Resolution Order

When a method is called on a driver instance:


Sources: src/FilesystemAdapter.php859-866

This resolution order allows:

  1. Driver-specific implementations to override base behavior
  2. Base functionality to be shared across all drivers
  3. Macros to extend functionality at runtime
  4. Direct access to Flysystem features via delegation

Temporary URL Callback

The adapter supports custom temporary URL generation logic via the buildTemporaryUrlsUsing() method:


Sources: src/FilesystemAdapter.php70-840

The callback mechanism at src/FilesystemAdapter.php837-840 allows applications to override default temporary URL generation without subclassing. The callback is bound to the adapter instance, providing access to $this->driver, $this->adapter, and $this->config.

Configuration Integration

The FilesystemAdapter respects various configuration options from the $config array:

Config KeyTypePurposeUsed In
rootstringRoot directory pathConstructor84-87
prefixstringAdditional path prefixConstructor89-91
directory_separatorstringPath separator (default: DIRECTORY_SEPARATOR)Constructor86
throwboolWhether to throw exceptions on errorsthrowsExceptions()847
urlstringBase URL for public file accessgetLocalUrl()632-634 getFtpUrl()619-621

Sources: src/FilesystemAdapter.php60-848

The configuration array is accessible via getConfig() at src/FilesystemAdapter.php811-814 and is passed through to driver implementations for use in specialized functionality.


The FilesystemAdapter serves as the critical bridge between the Hypervel framework features and League Flysystem's storage abstractions. By providing a rich set of common functionality while maintaining flexibility for driver-specific implementations, it enables a consistent API across all storage backends while allowing specialized capabilities where needed.