spiral/files
Spiral Files is a lightweight PHP file toolkit for managing files and directories with a clean API, plus virtual stream wrapper support. Includes strong typing, PHPUnit/Psalm coverage, and integrates with the Spiral Framework or works standalone.
Start by installing the package via Composer (composer require spiral/files) and examining the Spiral\Files\FilesInterface and Spiral\Files\IOException classes. The core functionality centers around the Files class, which provides atomic file operations (e.g., write(), append(), delete(), copy(), move()). The first practical use case is replacing standard PHP file functions with atomic alternatives to avoid partial writes or race conditions — e.g., write($path, $content) creates a temp file, writes to it, then renames atomically.
The package now adheres to PSR-12 coding standards, ensuring consistent code style and readability.
Files::write() instead of file_put_contents() for critical data (e.g., config snapshots, cache regeneration) to guarantee consistency.Spiral\Files\Stream\Stream to abstract filesystem operations (e.g., mocking in tests via in-memory streams or testing S3 via PSR-18-compatible adapters).getContents($dir), listFiles($dir), and removeDirectory($dir) for recursive operations.FilesInterface (not concrete Files) to inject mock implementations in unit tests. Use Stream::factory('memory://') for zero-I/O test scenarios.Files::setRoot() or extend Files to set scoped roots per module (e.g., app/storage vs config/files).IOException — the component wraps low-level errors (e.g., permission denied, disk full) consistently... to prevent unexpected behavior (use normalizePath() for safe inputs).removeDirectory() only delete empty directories; use removeDirectory($dir, true) to force recursive removal.Stream to Psr\Http\Message\StreamInterface via stream_get_meta_data() and stream wrappers.Spiral\Files\Files to inject custom logic (e.g., logging, metrics) in callMethod() — note that this component predates Laravel’s Filesystem enhancements, so consider using Laravel’s Storage for newer apps.How can I help you explore Laravel packages today?