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

Commit

Permalink
Merge branch '2.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
Joan Fabrégat committed Oct 9, 2018
2 parents b79f312 + 1766cf4 commit b7185a7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
39 changes: 29 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,43 @@ This PHP 7.1 library is a [PSR-15](https://www.php-fig.org/psr/psr-15/) middlewa
```php
<?php
use CodeInc\AssetsMiddleware\AssetsMiddleware;
use CodeInc\AssetsMiddleware\Resolvers\AssetsDirectoryResolver;

$assetsMiddleware = new AssetsMiddleware(
'/assets/' // <-- specifies the assets base URI path
);

// adding web assets directories
$assetsMiddleware->addAssetsDirectory('/path/to/my/first/web-assets-directory');
$assetsMiddleware->addAssetsDirectory('/path/to/another/web-assets-directory');

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

// returns the computed path to the assets directory
$assetsMiddleware->getAssetUri('/path/to/another/web-assets-directory/an-image.jpg');
// processed a PSR-7 server request as a PSR-15 middleware
$assetsMiddleware->process($aPsr7ServerRequest, $aPsr15RequestHandler); // <-- returns a PSR-7 response
```

### Using multiple resolver

```php
<?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/assets/assets/', // <-- directory path
'/assets/' // <-- assets URI prefix
)
])
);

// processed a PSR-7 server request as a PSR-15 middleware
$assetsMiddleware->process($aPsr7ServerRequest, $aPsr15RequestHandler); // <-- returns a PSR-7 response
Expand Down
3 changes: 1 addition & 2 deletions src/AssetsMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
use CodeInc\AssetsMiddleware\Responses\AssetMinifiedResponse;
use CodeInc\AssetsMiddleware\Responses\NotModifiedAssetResponse;
use Micheh\Cache\CacheUtil;
use function PHPSTORM_META\elementType;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
Expand Down Expand Up @@ -88,7 +87,7 @@ class AssetsMiddleware implements MiddlewareInterface
* @param bool $cacheAssets Allows the assets to the cached in the web browser
* @param bool $minifyAssets Minimizes the assets before sending them (@see AssetCompressedResponse)
*/
public function __construct(AssetResolverInterface $resolver, string $assetsUriPrefix,
public function __construct(AssetResolverInterface $resolver, string $assetsUriPrefix = '/',
bool $cacheAssets = true, bool $minifyAssets = false)
{
$this->resolver = $resolver;
Expand Down

0 comments on commit b7185a7

Please sign in to comment.