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 #4 from CodeIncHQ/1.x
Browse files Browse the repository at this point in the history
v1.1.1
  • Loading branch information
Joan Fabrégat authored Jul 3, 2018
2 parents 26c3e6d + 4d3336e commit 4b4a9c6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 19 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "codeinc/assets-middleware",
"version": "1.1.0",
"version": "1.1.1",
"description": "A PSR-15 middleware to server static assets (CSS, JS, images, etc.)",
"homepage": "https://github.com/CodeIncHQ/AssetsMiddleware",
"type": "library",
Expand Down
44 changes: 29 additions & 15 deletions src/Assets/AssetMinifiedResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
use CodeInc\Psr7Responses\StreamResponse;
use function GuzzleHttp\Psr7\stream_for;
use MatthiasMullie\Minify;
use Psr\Http\Message\StreamInterface;
use RuntimeException;


/**
Expand Down Expand Up @@ -69,22 +71,8 @@ public function __construct(string $filePath, string $assetName, ?string $fileNa
$mimeType = MediaTypes::getFilenameMediaType($fileName);
}

switch ($mimeType) {
case 'text/css':
$stream = stream_for((new Minify\CSS($filePath))->minify());
break;

case 'text/javascript':
$stream = stream_for((new Minify\JS($filePath))->minify());
break;

default:
$stream = stream_for($filePath);
break;
}

parent::__construct(
$stream,
$this->getStream($mimeType, $filePath),
$mimeType,
null,
$fileName,
Expand All @@ -96,6 +84,32 @@ public function __construct(string $filePath, string $assetName, ?string $fileNa
);
}

/**
* @param string $mimeType
* @param string $filePath
* @return StreamInterface
*/
private function getStream(string $mimeType, string $filePath):StreamInterface
{
switch ($mimeType) {
case 'text/css':
return stream_for((new Minify\CSS($filePath))->minify());

case 'text/javascript':
return stream_for((new Minify\JS($filePath))->minify());

default:
$f = fopen($filePath, 'r');
if ($f === false) {
throw new RuntimeException(
sprintf("Unable to open the assets file '%s'",
$filePath)
);
}
return stream_for($f);
}
}

/**
* @inheritdoc
* @return string
Expand Down
5 changes: 2 additions & 3 deletions src/AssetsMiddlewareException.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
//
declare(strict_types=1);
namespace CodeInc\AssetsMiddleware;
use RuntimeException;
use Throwable;


Expand All @@ -29,10 +30,8 @@
*
* @package CodeInc\AssetsMiddleware
* @author Joan Fabrégat <[email protected]>
* @license MIT <https://github.com/CodeIncHQ/AssetsMiddleware/blob/master/LICENSE>
* @link https://github.com/CodeIncHQ/AssetsMiddleware
*/
class AssetsMiddlewareException extends \Exception
class AssetsMiddlewareException extends RuntimeException
{
/**
* @var AssetsMiddleware
Expand Down

0 comments on commit 4b4a9c6

Please sign in to comment.