Coder Social home page Coder Social logo

laravel-vfs-adapter's Introduction

Laravel Storage Virtual File System Adapter

Sometimes, when we are testing, it would be very nice to just easily swap out the various Storage:disk() calls with a virtual files system like mikey179/vfsStream.

Installation

composer require stechstudio/laravel-vfs-adapter

Then register the provider in your App\Providers\AppServiceProvider like so:

public function register()
{
    if ($this->app->environment() === 'testing') {
        if (class_exists(VfsFilesystemServiceProvider::class)) {
            $this->app->register(VfsFilesystemServiceProvider::class);
        }
    }
}

This assumes you stick with the default APP_ENV=testing settings that Laravel comes with out of the box. Of course, you may also register the provider in the more traditional manner if you want the virtual file system adapter available in other environments, of it you just don't like the way we do it.

Configuration

All configuration can be done in a combination of your .env, phpunit.xml, and config/filesystems.php.

Suppose you had a config/filesystems.php that looked something like this:

return [
    ....

    'disks' => [
        'data' => [
            'driver' => 'local',
            'root'   => env('STORAGE_DATA_DIR'),
        ],

        'archive' => [
            'driver'   => 's3'
            'key'      => env('S3_KEY'),
            'secret'   => env('S3_SECRET'),
            'region'   => env('S3_REGION'),
            'bucket'   => env('S3_DEVELOP_BUCKET')
        ]
    ],
];

Simply make a few modifications:

return [
    ....

    'disks' => [

        'data' => [
            'driver' => env('STORAGE_DATA_DRIVER', 'local'),
            'root'   => env('STORAGE_DATA_DIR'),
            'dir_name' => 'data'
        ],

        'archive' => [
            'driver'   => env('S3_ARCHIVE_DRIVER', 's3')
            'key'      => env('S3_KEY'),
            'secret'   => env('S3_SECRET'),
            'region'   => env('S3_REGION'),
            'bucket'   => env('S3_DEVELOP_BUCKET'),
            'dir_name' => 'archive'
        ]
    ],
];

The dir_name determines the root directory of the vfsStream for that particular Storage::disk(), I like to name it something relative to the actual disk name. Setting up the driver with env() allows us to default to our standard drivers or allow us to override that in phpunit.xml to switch over to the virtual filesystem driver.

Now, in your phpunit.xml add:

<env name="STORAGE_DATA_DRIVER" value="vfs"/>
<env name="S3_ARCHIVE_DRIVER" value="vfs"/>

That is all there is to it, those drives will now use the virtual filesystem adapter.

laravel-vfs-adapter's People

Contributors

bubba-h57 avatar rdarcy1 avatar jocodomino avatar rafaelkr avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.