diff --git a/src/Minifier.php b/src/Minifier.php index facee6a..9ceb955 100644 --- a/src/Minifier.php +++ b/src/Minifier.php @@ -22,9 +22,9 @@ class Minifier implements MiddlewareInterface private $compressor; /** - * @var string + * @var string[] */ - protected $mimetype; + protected $mimetypes; /** * @var StreamFactoryInterface @@ -34,26 +34,26 @@ class Minifier implements MiddlewareInterface public static function html(?StreamFactoryInterface $streamFactory = null): self { /* @note We use static so that other classes can extend it and get the expected behaviour */ - return new static(HtmlFactory::construct(), 'text/html', $streamFactory); + return new static(HtmlFactory::construct(), ['text/html'], $streamFactory); } public static function css(?StreamFactoryInterface $streamFactory = null): self { - return new static(CssFactory::construct(), 'text/css', $streamFactory); + return new static(CssFactory::construct(), ['text/css'], $streamFactory); } public static function js(?StreamFactoryInterface $streamFactory = null): self { - return new static(JsFactory::construct(), 'text/javascript', $streamFactory); + return new static(JsFactory::construct(), ['text/javascript', 'application/javascript'], $streamFactory); } public function __construct( CompressorInterface $compressor, - string $mimetype, + array $mimetypes, ?StreamFactoryInterface $streamFactory = null ) { $this->compressor = $compressor; - $this->mimetype = $mimetype; + $this->mimetypes = $mimetypes; $this->streamFactory = $streamFactory ?: Factory::getStreamFactory(); } @@ -63,12 +63,15 @@ public function __construct( public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { $response = $handler->handle($request); + $contentType = $response->getHeaderLine('Content-Type'); - if (stripos($response->getHeaderLine('Content-Type'), $this->mimetype) === 0) { - $stream = $this->streamFactory->createStream($this->minify((string) $response->getBody())); + foreach ($this->mimetypes as $mimetype) { + if (stripos($contentType, $mimetype) === 0) { + $stream = $this->streamFactory->createStream($this->minify((string) $response->getBody())); - return $response->withBody($stream) - ->withoutHeader('Content-Length'); + return $response->withBody($stream) + ->withoutHeader('Content-Length'); + } } return $response; diff --git a/tests/MinifyTest.php b/tests/MinifyTest.php index a5557d9..c245bae 100644 --- a/tests/MinifyTest.php +++ b/tests/MinifyTest.php @@ -34,6 +34,12 @@ public function minifierProvider(): array /* @phpstan-ignore-next-line */ trim(file_get_contents(__DIR__.'/assets/test.min.js')), ], + [ + 'application/javascript', + file_get_contents(__DIR__.'/assets/test.js'), + /* @phpstan-ignore-next-line */ + trim(file_get_contents(__DIR__.'/assets/test.min.js')), + ], ]; return $data;