Skip to content
This repository has been archived by the owner on Feb 5, 2019. It is now read-only.

Latest commit

 

History

History
61 lines (46 loc) · 1.91 KB

README.md

File metadata and controls

61 lines (46 loc) · 1.91 KB

Assets middleware

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