Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions src/Minifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ class Minifier implements MiddlewareInterface
private $compressor;

/**
* @var string
* @var string[]
*/
protected $mimetype;
protected $mimetypes;

/**
* @var StreamFactoryInterface
Expand All @@ -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();
}

Expand All @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions tests/MinifyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading