danhunsaker/laravel-flysystem-service

Automatically registers every Flysystem adapter it recognizes as a Laravel Filesystem Driver.

v1.2.2 2018-08-13 22:27 UTC

README

Software License Gitter Liberapay receiving

Latest Stable Version Latest Unstable Version Build Status Codecov Total Downloads

Registers recognized Flysystem adapters with Laravel automatically.

This lets you use other adapters without having to write your own service providers to load them properly. It automatically detects which adapters are available, and registers only the ones actually installed. It also detects whether the Eventable version of Flysystem is available, and if so, it switches to it, letting you listen in on Flysystem events and affect them accordingly.

Note: This package only recognizes the adapters officially supported by The PHP League (except AWS-S3-v2) - for other adapters, install danhunsaker/laravel-flysystem-others instead (it will pull in this package as a dependency).

Installation

The usual methods for using Composer apply here:

composer require danhunsaker/laravel-flysystem-service

You do still have to register one service, but only one, and at least you don't have to write it. Be sure to REPLACE the Illuminate\Filesystem\FilesystemServiceProvider::class line with the new one:

// In config/app.php

    'providers' => [
        // ...
        Danhunsaker\Laravel\Flysystem\FlysystemServiceProvider::class,
        // ...
    ],

Setup

You can get example definitions for all supported filesystem drivers by publishing the replacement filesystems config - just run the following Artisan command:

php artisan vendor:publish --provider=Danhunsaker\\Laravel\\Flysystem\\FlysystemServiceProvider --force

The --force flag is required to overwrite the existing filesystems config that ships with Laravel. You can also rename the existing file, then run the command without the --force flag, if you'd like to preserve the existing contents for transfer to the new file.

Cache Decorator

Flysystem provides support for adding decorators to filesystem adapters, complete with an abstract implementation that other implementations can extend, reducing the number of methods they have to implement themselves if they don't particularly care about all of them. They also provide a complete decorator that provides support for caching metadata, which can greatly speed up several operations on slow filesystems, such as cloud storage.

Since this cache decorator is one of the official PHP League packages designed to be used with Flysystem, this package supports it as well. Ensure you have league/flysystem-cached-adapter, then simply add a cache array to your drive definition. Multiple cache drivers are supported directly, and each has unique options you can configure alongside it, so we'll break those down, below.

Laravel Cache

Thanks to the madewithlove/illuminate-psr-cache-bridge package, you can just use Laravel's own cache to store cached filesystem data.

    'cache' => [
        'driver' => 'laravel',
        'key'    => 'flysystem',
        'expire' => 300,
    ],

Flysystem Adapter

Store the cached data in a file on one of the disks defined in your config.

    'cache' => [
        'driver' => 'adapter',
        'disk'   => 'local',
        'file'   => 'flysystem.cache',
        'expire' => 300,
    ],

Memcached

Store the data on a Memcache server.

    'cache' => [
        'driver' => 'memcached',
        'host'   => 'localhost',
        'port'   => 11211,
        'key'    => 'flysystem',
        'expire' => 300,
    ],

Memory

Just store the cached data in a class instance ('application memory'). When the application shuts down, the cache will be lost.

    'cache' => [
        'driver' => 'memory',
    ],

No Op

Don't store the cached data at all. Essentially the same as not providing a cache array at all.

Note: This driver does not actually cache any data.

    'cache' => [
        'driver' => 'noop',
    ],

Redis

Store the cached data on a Redis server. Specify a Redis connection name from your database config.

    'cache' => [
        'driver'     => 'redis',
        'connection' => 'default',
        'key'        => 'flysystem',
        'expire'     => 300,
    ],

Stash

Store the cached data using the Stash caching framework. This is easily the most complex cache driver supported here. Each backend is the full class name of a Stash cache driver, and the options array varies between which one you choose to use. Alternately, you can set backend to a preconfigured instance of the driver, which is useful in cases such as the Composite driver, which is otherwise unsupported. More information on these options is available on the Stash site.

    'cache' => [
        'driver'  => 'stash',
        'backend' => 'Stash\Driver\Filesystem',
        'options' => [
            'dirSplit'        => 500,
            'path'            => storage_path('stash'),
            'filePermissions' => 0660,
            'dirPermissions'  => 0770,
        ],
        'key'     => 'flysystem',
        'expire'  => 300,
    ],
        'backend' => 'Stash\Driver\Sqlite',
        'options' => [
            'extension'       => 'pdo',
            'version'         => 3,
            'nesting'         => 0,
            'path'            => storage_path('stash.db'),
            'filePermissions' => 0660,
            'dirPermissions'  => 0770,
        ],
        'backend' => 'Stash\Driver\Apc',
        'options' => [
            'ttl'       => 3600,
            'namespace' => 'stash',
        ],
        'backend' => 'Stash\Driver\Memcache',
        'options' => [
            'servers'   => ['localhost', '11211'],
            'extension' => 'memcached',
            // Plus any other options Memcache might want...
        ],
        'backend' => 'Stash\Driver\Redis',
        'options' => [
            'servers' => ['localhost', '6379'],
        ],
        'backend' => 'Stash\Driver\Ephemeral',

Use An Instance

You can also pass a preconfigured instance of your preferred cache driver instead of a driver name, if you like. This is useful for third-party adapters, and for using external libraries through the Psr6Cache adapter.

Contributions

Pull requests, bug reports, and so forth are all welcome on GitHub.

Security issues should be reported directly to danhunsaker (plus) laraflyserv (at) gmail (dot) com.

And head to GitHub for everything else.