Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Flysystem Memory Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

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().


Implementation Patterns

  • 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').


Gotchas and Tips

  • Ephemeral only: Data vanishes when the PHP process ends — never rely on it for persistence or production data. Use exclusively for testing, short-lived caching, or throwaway workflows.
  • Memory pressure: Large files (e.g., 100MB+ uploads) or bulk operations can trigger 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.
  • No process sharing: Each PHP request/fork has isolated memory — invariants like Session::get() do not apply. Not usable for inter-process communication.
  • GitHub workflow: This is a sub-split of league/flysystem. Report issues and send PRs to the main Flysystem repo, not this memory-only repo. Check closed issues for adapter-specific bugs.
  • Extension point: Override MemoryAdapter methods like readStream() or has() to inject logging, metrics, or synthetic failures (e.g., for fault tolerance testing).
  • PHP version requirement: Requires PHP ≥8.0.2 and fileinfo extension. If stuck on PHP 7.x or Flysystem 2.x, this adapter is incompatible — upgrade path required.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport