league/flysystem-memory
Flysystem in-memory storage adapter (sub-split). Provides an in-memory filesystem for fast tests, temporary storage, and sandboxed usage. Install via composer require league/flysystem-memory; docs at flysystem.thephpleague.com.
Install the package via Composer:
composer require league/flysystem-memory
Then instantiate the MemoryAdapter and wrap it in a Filesystem instance — identical to standard Flysystem usage:
use League\Flysystem\Filesystem;
use League\Flysystem\Memory\MemoryAdapter;
$filesystem = new Filesystem(new MemoryAdapter());
$filesystem->write('test.txt', 'Hello, memory!');
Its most common first use: unit testing filesystem-dependent code — e.g., mocking a LocalAdapter in an UploadService without writing actual files to disk. Write assertions against the in-memory store using methods like has(), read(), or listContents().
Test isolation: Inject MemoryAdapter into services during tests to avoid file I/O, cross-test contamination, and cleanup overhead. Use it in Laravel’s TestCase to mock storage disks:
$filesystem = new Filesystem(new MemoryAdapter());
$service = new AvatarProcessor($filesystem);
// … assertions …
$this->assertTrue($filesystem->has('avatars/user_123.png'));
Request-scoped temporary storage: In Laravel middleware or controllers, cache transient results (e.g., processed images, PDF previews) for downstream consumers in the same request lifecycle — faster than disk and avoids cleanup.
$filesystem->write('temp/report.pdf', $pdfStream);
// Later in same request: $filesystem->read('temp/report.pdf');
Drop-in replacement in CLI tools: For short-lived artisan commands or cron jobs, use MemoryAdapter to simulate file operations (e.g., generating and inspecting CSV exports) without side effects or cleanup code.
Integration with Laravel’s Storage facade: While not a native disk, you can wrap the MemoryAdapter in a custom League\Flysystem\FilesystemAdapter and register it via Storage::extend(). Ideal for feature tests mocking Storage::disk('memory').
Allowed memory size exhausted. Monitor usage via memory_get_usage(true) during test suites — cap file sizes or use delete() aggressively.lastModified() returns 0 by default: If timestamp precision matters (e.g., for cache invalidation), extend MemoryAdapter and override store()/update() to set microtime(true) via the protected write() method.Session::get() do not apply. Not usable for inter-process communication.league/flysystem. Report issues and send PRs to the main Flysystem repo, not this memory-only repo. Check closed issues for adapter-specific bugs.MemoryAdapter methods like readStream() or has() to inject logging, metrics, or synthetic failures (e.g., for fault tolerance testing).fileinfo extension. If stuck on PHP 7.x or Flysystem 2.x, this adapter is incompatible — upgrade path required.How can I help you explore Laravel packages today?