hkt/psr7-asset

Managing assets via PSR-7

0.5.0 2018-08-08 06:05 UTC

This package is auto-updated.

Last update: 2024-12-07 20:33:28 UTC


README

Asset management for PHP. This package is a fork of Aura.Asset_Bundle.

Foreword

Requirements

Installation

composer require hkt/psr7-asset http-interop/http-factory-diactoros

You can use any psr-17 libraries.

Tests

Build Status

composer install
vendor/bin/phpunit

or you may use;

composer install
composer check

PSR Compliance

This attempts to comply with PSR-1, PSR-2, PSR-4, PSR-7, PSR-15 and PSR-17. If you notice compliance oversights, please send a patch via pull request.

Structure of Package

Assume you have a Vendor.Package. Your assets can be any where. Consider it is in the public folder. The folder names css, images, js can be according to your preffered name.

├── src
│   ├── Cli
│   └── Web
├── tests
└── public
    ├── css
    │   └── some.css
    ├── images
    │   ├── another.jpg
    │   └── some.png
    └── js
        └── hello.js

Assuming you have the same structure, now in your template you can point to /asset/vendor/package/css/some.css, /asset/vendor/package/js/hello.js, /asset/vendor/package/images/another.jpg.

Routing

The library can be used with any framework. So it makes use of preg_match under the hood. The default regular expression is /\/asset\/([a-zA-Z0-9-_]+)\/([a-zA-Z0-9-_]+)\/(.*)/ .

You can modify the regular expression when intantiating the Router object which is passed as 3rd argument to AssetAction.

<?php
$locator   = new Hkt\Psr7Asset\AssetLocator();
$service   = new Hkt\Psr7Asset\AssetService($locator);
$responder = new Hkt\Psr7Asset\AssetResponder(new Http\Factory\Diactoros\ResponseFactory());
$router    = new Hkt\Psr7Asset\Router();
$assetAction = new Hkt\Psr7Asset\AssetAction($service, $responder, $router);

// ... more code
$assetAction->process($request, $requestHandler)

Zend Expressive

If you are using `zend expressive you can configure,

AuraRouter

<?php
$route = new \Zend\Expressive\Router\Route('/asset/{vendor}/{package}/{file}', 'Hkt\Psr7Asset\AssetAction', ['GET'], 'hkt/psr7-asset:route');
$route->setOptions([
    'tokens' => [
        'file' => '(.*)'
    ]
]);
$router->addRoute($route);

FastRoute

<?php
$router->addRoute(new \Zend\Expressive\Router\Route('/asset/{vendor}/{package}/{file:.*}', 'Hkt\Psr7Asset\AssetAction', ['GET'], 'hkt/psr7-asset:route'));

NB : Make sure you have set the service Hkt\Psr7Asset\AssetAction to the Di container.

From your view you can use as

<?php
$this->url('hkt/psr7-asset:route', [
    'vendor' => 'vendor',
    'package' => 'package',
    'file' => '/css/bootstrap.min.css'
]);

This will return /asset/vendor/package/css/bootstrap.min.css.

Mapping

With the help of mapping the vendor/package or directly the path you can alter the result it returns.

Eg :

$locator->set('vendor/package', '/full/path/to/vendor/package');

Overriding css, js, images

Like puli it is possible that you can override the style sheet, images, js etc for the downloaded package. You just need to map it. No magic under the hood.

Eg :

$locator->set('vendor/package', '/full/path/to/vendor/package');
$locator->set('vendor/package/css/style.css', '/full/path/to/vendor/package/public/css/style.css');
// override vendor/package style sheet, same applies for js and images
$locator->set('vendor/package/css/style.css', '/full/path/to/application/specific/public/css/style.css');

Caching

The asset files are served by PHP. There is an experimental repo https://github.com/harikt/psr7-asset-cache that can do caching. So files can be served by the web server itself.

Configuration via Aura.Di

Pass Hkt\Psr7Asset\Container\AssetConfig to your Container Builder. Don't forget to set the service Interop\Http\Factory\ResponseFactoryInterface as below.

<?php
namespace Vendor\Package;

use Aura\Di\Container;
use Aura\Di\ContainerConfigInterface;

class AppConfig implements ContainerConfigInterface
{
    public function define(Container $di)
    {
        // add one of the http-interop/http-factory library
        $di->set('Interop\Http\Factory\ResponseFactoryInterface', $di->lazyNew('Http\Factory\Diactoros\ResponseFactory'));
    }

    public function modify(Container $di)
    {
        // Map more paths and location as above.
        $assetLocator = $di->get('Hkt\Psr7Asset\AssetLocator');
        // path to exact location
        $assetLocator->set('vendor/package/css/hello.css', '/path/to/web/css/test.css');
        // path to folder
        $assetLocator->set('vendor/package', dirname(dirname(__DIR__)) . '/public');
    }
}