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

Commit

Permalink
Merge pull request #5 from CodeIncHQ/1.x
Browse files Browse the repository at this point in the history
v1.2.0
  • Loading branch information
Joan Fabrégat authored Jul 3, 2018
2 parents 4b4a9c6 + e9907de commit a312c58
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "codeinc/assets-middleware",
"version": "1.1.1",
"version": "1.2.0",
"description": "A PSR-15 middleware to server static assets (CSS, JS, images, etc.)",
"homepage": "https://github.com/CodeIncHQ/AssetsMiddleware",
"type": "library",
Expand All @@ -13,7 +13,8 @@
"micheh/psr7-cache": "^0.5.0",
"codeinc/psr7-responses": "^1.2",
"matthiasmullie/minify": "^1.3",
"codeinc/media-types": "^1.0"
"codeinc/media-types": "^1.0",
"enshrined/svg-sanitize": "^0.8.2"
},
"require-dev": {
"phpunit/phpunit": "^7",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,28 @@
namespace CodeInc\AssetsMiddleware\Assets;
use CodeInc\MediaTypes\MediaTypes;
use CodeInc\Psr7Responses\StreamResponse;
use enshrined\svgSanitize\Sanitizer;
use function GuzzleHttp\Psr7\stream_for;
use MatthiasMullie\Minify;
use Psr\Http\Message\StreamInterface;
use RuntimeException;


/**
* Class AssetMinifiedResponse
* Class AssetCompressedResponse
*
* @uses Minify\CSS
* @uses Minify\JS
* @package CodeInc\AssetsMiddleware\Assets
* @author Joan Fabrégat <[email protected]>
*/
class AssetMinifiedResponse extends StreamResponse implements AssetResponseInterface
class AssetCompressedResponse extends StreamResponse implements AssetResponseInterface
{
/**
* @var string
*/
private $assetName;

/**
* AssetResponse constructor.
* AssetCompressedResponse constructor.
*
* @param string $filePath
* @param string $assetName
Expand Down Expand Up @@ -98,12 +97,20 @@ private function getStream(string $mimeType, string $filePath):StreamInterface
case 'text/javascript':
return stream_for((new Minify\JS($filePath))->minify());

case 'image/svg+xml':
$svgContent = file_get_contents($filePath);
if ($svgContent === false) {
throw new RuntimeException(
sprintf("Unable to read the SCF assets file '%s'", $filePath)
);
}
return stream_for((new Sanitizer())->sanitize($svgContent));

default:
$f = fopen($filePath, 'r');
if ($f === false) {
throw new RuntimeException(
sprintf("Unable to open the assets file '%s'",
$filePath)
sprintf("Unable to open the assets file '%s'", $filePath)
);
}
return stream_for($f);
Expand Down
16 changes: 8 additions & 8 deletions src/AssetsMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
//
declare(strict_types = 1);
namespace CodeInc\AssetsMiddleware;
use CodeInc\AssetsMiddleware\Assets\AssetMinifiedResponse;
use CodeInc\AssetsMiddleware\Assets\AssetCompressedResponse;
use CodeInc\AssetsMiddleware\Assets\AssetNotModifiedResponse;
use CodeInc\AssetsMiddleware\Assets\AssetResponse;
use CodeInc\AssetsMiddleware\Test\AssetsMiddlewareTest;
Expand Down Expand Up @@ -61,19 +61,19 @@ class AssetsMiddleware implements MiddlewareInterface
/**
* @var bool
*/
private $minifyJsAndCss;
private $allowAssetsCompression;

/**
* AssetsMiddleware constructor.
*
* @param string $assetsLocalPath
* @param string $assetsUriPath
* @param bool $allowAssetsCache
* @param bool $minifyJsAndCss
* @param bool $allowAssetsCache Allows assets cache through HTTP headers
* @param bool $allowAssetsCompression Compresses CSS, JS and SVG files
* @throws AssetsMiddlewareException
*/
public function __construct(string $assetsLocalPath, string $assetsUriPath,
bool $allowAssetsCache = true, bool $minifyJsAndCss = false)
bool $allowAssetsCache = true, bool $allowAssetsCompression = false)
{
if (!is_dir($assetsLocalPath) || ($assetsLocalPath = realpath($assetsLocalPath)) === null) {
throw new AssetsMiddlewareException(
Expand All @@ -84,7 +84,7 @@ public function __construct(string $assetsLocalPath, string $assetsUriPath,
$this->assetsLocalPath = $assetsLocalPath;
$this->assetsUriPath = $assetsUriPath;
$this->allowAssetsCache = $allowAssetsCache;
$this->minifyJsAndCss = $minifyJsAndCss;
$this->allowAssetsCompression = $allowAssetsCompression;
}

/**
Expand All @@ -103,11 +103,11 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
if (file_exists($assetPath)) {

// builds the response
if (!$this->minifyJsAndCss) {
if (!$this->allowAssetsCompression) {
$response = new AssetResponse($assetPath, $assetName);
}
else {
$response = new AssetMinifiedResponse($assetPath, $assetName);
$response = new AssetCompressedResponse($assetPath, $assetName);
}

// enables the cache
Expand Down

0 comments on commit a312c58

Please sign in to comment.