codeinc/assets-middleware

This package is abandoned and no longer maintained. No replacement package was suggested.

A PSR-15 middleware to server static assets (CSS, JS, images, etc.)

2.1.1 2018-10-11 17:16 UTC

This package is auto-updated.

Last update: 2020-02-11 20:09:10 UTC


README

This PHP 7.1 library is a PSR-15 middleware dedicated to manage static assets like CSS, JS, or image files.

Usage

<?php
use CodeInc\AssetsMiddleware\AssetsMiddleware;
use CodeInc\AssetsMiddleware\Resolvers\AssetsDirectoryResolver;

$assetsMiddleware = new AssetsMiddleware(
    new AssetsDirectoryResolver(
        '/path/to/my/assets/assets/', // <-- directory path
        '/assets/' // <-- assets URI prefix
    )
);
// optionally you can limit the acceptable media types
$assetsMiddleware->setAllowMediaTypes([
    'image/*', // supports shell patterns through fnmatch()
    'text/css',
    'application/javascript'
]);

// processed a PSR-7 server request as a PSR-15 middleware
$assetsMiddleware->process($aPsr7ServerRequest, $aPsr15RequestHandler); // <-- returns a PSR-7 response

Using multiple resolvers

<?php
use CodeInc\AssetsMiddleware\AssetsMiddleware;
use CodeInc\AssetsMiddleware\Resolvers\AssetsDirectoryResolver;
use CodeInc\AssetsMiddleware\Resolvers\StaticAssetsResolver;
use CodeInc\AssetsMiddleware\Resolvers\AssetResolverAggregator;

$assetsMiddleware = new AssetsMiddleware(
    new AssetResolverAggregator([
        new StaticAssetsResolver(['/favicon.ico' => '/local/favicon/file.ico']),
        new AssetsDirectoryResolver('/path/to/my/css/', '/css/'),
        new AssetsDirectoryResolver('/path/to/my/images/', '/images/')
    ])
);

// processed a PSR-7 server request as a PSR-15 middleware
$assetsMiddleware->process($aPsr7ServerRequest, $aPsr15RequestHandler); // <-- returns a PSR-7 response

Installation

This library is available through Packagist and can be installed using Composer:

composer require codeinc/assets-middleware

License

The library is published under the MIT license (see LICENSE file).