middlewares/filesystem

Middleware to save or read responses using Flysystem

v1.1.0 2018-08-04 10:41 UTC

README

Latest Version on Packagist Software License Build Status Quality Score Total Downloads SensioLabs Insight

Middleware to save or read responses from files. It uses Flysystem as filesystem handler, so you can use not only a local directories, but also any other adapter like ftp, sftp, dropbox, etc... This package includes the following components:

Requirements

Installation

This package is installable and autoloadable via Composer as middlewares/filesystem.

composer require middlewares/filesystem

Example

$dispatcher = new Dispatcher([
    Middlewares\Reader::createFromDirectory(__DIR__.'/assets')
]);

$response = $dispatcher->dispatch(new ServerRequest());

Reader

To read the response body from a file under the following conditions:

  • Only GET methods are allowed, returning a 405 code otherwise.
  • If the request path has no extension, assume it's a directory and append /index.html. For example: if the request path is /post/23, the file used is /post/23/index.html.
  • It can handle gzipped files. For example, if /post/23/index.html does not exists but /post/23/index.html.gz is available and the request header Accept-Encoding contains gzip, returns it.
  • Accept-Ranges is also supported, useful to server big files like videos.

__construct(League\Flysystem\FilesystemInterface $filesystem)

Set the filesystem manager. Example using a ftp storage:

use League\Flysystem\Filesystem;
use League\Flysystem\Adapter\Ftp;

$filesystem = new Filesystem(new Ftp([
    'host' => 'ftp.example.com',
    'username' => 'username',
    'password' => 'password',
    'port' => 21,
    'root' => '/path/to/root',
    'passive' => true,
    'ssl' => true,
    'timeout' => 30,
]));

$dispatcher = new Dispatcher([
    new Middlewares\Reader($filesystem)
]);

$response = $dispatcher->dispatch(new ServerRequest());

continueOnError(true)

Allows to continue to the next middleware on error (file not found, method not allowed, etc). This allows to create a simple caching system as the following:

$cache = '/path/to/files';

$dispatcher = new Dispatcher([
    (new Middlewares\Reader($cache))    //read and returns the cached response...
        ->continueOnError(),            //...but continue if the file does not exists

    new Middlewares\Writer($cache),     //save the response in the cache

    new Middlewares\AuraRouter($route), //create a response using, for example, Aura.Router
]);

$response = $dispatcher->dispatch(new ServerRequest());

responseFactory(Psr\Http\Message\ResponseFactoryInterface $responseFactory)

A PSR-17 factory to create the responses.

streamFactory(Psr\Http\Message\StreamFactoryInterface $streamFactory)

A PSR-17 factory to create the response bodies.

Writer

Saves the response content into a file if all of the following conditions are met:

  • The method is GET
  • The status code is 200
  • The Cache-Control header does not contain no-cache and no-store

To be compatible with Reader behaviour:

  • If the request path has no extension, assume it's a directory and append /index.html. For example: if the request path is /post/23, the file saved is /post/23/index.html.
  • If the response is gzipped (has the header Content-Encoding: gzip) the file is saved with the extension .gz. For example /post/23/index.html.gz (instead /post/23/index.html).

__construct(League\Flysystem\FilesystemInterface $filesystem)

Set the filesystem manager.

$filesystem = new Flysystem(new Local(__DIR__.'/storage'));

$dispatcher = new Dispatcher([
    new Middlewares\Writer($filesystem)
]);

$response = $dispatcher->dispatch(new ServerRequest());

streamFactory(Psr\Http\Message\StreamFactoryInterface $streamFactory)

A PSR-17 factory to create the response bodies.

Helpers

createFromDirectory(string $path)

Both Reader and Writer have a static method as a shortcut to create instances using a directory in the local filesystem, due this is the most common case:

$dispatcher = new Dispatcher([
    Middlewares\Writer::createFromDirectory(__DIR__.'/assets')
    Middlewares\Reader::createFromDirectory(__DIR__.'/assets')
]);

Please see CHANGELOG for more information about recent changes and CONTRIBUTING for contributing details.

The MIT License (MIT). Please see LICENSE for more information.