| @@ 14-68 (lines=55) @@ | ||
| 11 | use function React\Promise\resolve; |
|
| 12 | use function RingCentral\Psr7\stream_for; |
|
| 13 | ||
| 14 | final class CssCompressMiddleware |
|
| 15 | { |
|
| 16 | const MIME_TYPE = 'text/css'; |
|
| 17 | ||
| 18 | /** |
|
| 19 | * @var Parser |
|
| 20 | */ |
|
| 21 | private $compressor; |
|
| 22 | ||
| 23 | /** |
|
| 24 | * @param Parser $compressor |
|
| 25 | */ |
|
| 26 | public function __construct(Parser $compressor = null) |
|
| 27 | { |
|
| 28 | if ($compressor === null) { |
|
| 29 | $compressor = Factory::constructSmallest(false); |
|
| 30 | } |
|
| 31 | ||
| 32 | $this->compressor = $compressor; |
|
| 33 | } |
|
| 34 | ||
| 35 | public function __invoke(ServerRequestInterface $request, callable $next) |
|
| 36 | { |
|
| 37 | $response = $next($request); |
|
| 38 | ||
| 39 | if (!($response instanceof PromiseInterface)) { |
|
| 40 | return resolve($this->handleResponse($response)); |
|
| 41 | } |
|
| 42 | ||
| 43 | return $response->then(function (ResponseInterface $response) { |
|
| 44 | return $this->handleResponse($response); |
|
| 45 | }); |
|
| 46 | } |
|
| 47 | ||
| 48 | private function handleResponse(ResponseInterface $response) |
|
| 49 | { |
|
| 50 | if ($response->getBody() instanceof HttpBodyStream) { |
|
| 51 | return $response; |
|
| 52 | } |
|
| 53 | ||
| 54 | if (!$response->hasHeader('content-type')) { |
|
| 55 | return $response; |
|
| 56 | } |
|
| 57 | ||
| 58 | list($contentType) = explode(';', $response->getHeaderLine('content-type')); |
|
| 59 | if ($contentType !== self::MIME_TYPE) { |
|
| 60 | return $response; |
|
| 61 | } |
|
| 62 | ||
| 63 | $body = (string)$response->getBody(); |
|
| 64 | $compressedBody = substr($this->compressor->compress('<style>' . $body . '</style>'), 7, -8); |
|
| 65 | ||
| 66 | return $response->withBody(stream_for($compressedBody))->withHeader('Content-Length', strlen($compressedBody)); |
|
| 67 | } |
|
| 68 | } |
|
| 69 | ||
| @@ 14-68 (lines=55) @@ | ||
| 11 | use function React\Promise\resolve; |
|
| 12 | use function RingCentral\Psr7\stream_for; |
|
| 13 | ||
| 14 | final class JsCompressMiddleware |
|
| 15 | { |
|
| 16 | const MIME_TYPE = 'application/javascript'; |
|
| 17 | ||
| 18 | /** |
|
| 19 | * @var Parser |
|
| 20 | */ |
|
| 21 | private $compressor; |
|
| 22 | ||
| 23 | /** |
|
| 24 | * @param Parser $compressor |
|
| 25 | */ |
|
| 26 | public function __construct(Parser $compressor = null) |
|
| 27 | { |
|
| 28 | if ($compressor === null) { |
|
| 29 | $compressor = Factory::construct(); |
|
| 30 | } |
|
| 31 | ||
| 32 | $this->compressor = $compressor; |
|
| 33 | } |
|
| 34 | ||
| 35 | public function __invoke(ServerRequestInterface $request, callable $next) |
|
| 36 | { |
|
| 37 | $response = $next($request); |
|
| 38 | ||
| 39 | if (!($response instanceof PromiseInterface)) { |
|
| 40 | return resolve($this->handleResponse($response)); |
|
| 41 | } |
|
| 42 | ||
| 43 | return $response->then(function (ResponseInterface $response) { |
|
| 44 | return $this->handleResponse($response); |
|
| 45 | }); |
|
| 46 | } |
|
| 47 | ||
| 48 | private function handleResponse(ResponseInterface $response) |
|
| 49 | { |
|
| 50 | if ($response->getBody() instanceof HttpBodyStream) { |
|
| 51 | return $response; |
|
| 52 | } |
|
| 53 | ||
| 54 | if (!$response->hasHeader('content-type')) { |
|
| 55 | return $response; |
|
| 56 | } |
|
| 57 | ||
| 58 | list($contentType) = explode(';', $response->getHeaderLine('content-type')); |
|
| 59 | if ($contentType !== self::MIME_TYPE) { |
|
| 60 | return $response; |
|
| 61 | } |
|
| 62 | ||
| 63 | $body = (string)$response->getBody(); |
|
| 64 | $compressedBody = substr($this->compressor->compress('<script>' . $body . '</script>'), 8, -9); |
|
| 65 | ||
| 66 | return $response->withBody(stream_for($compressedBody))->withHeader('Content-Length', strlen($compressedBody)); |
|
| 67 | } |
|
| 68 | } |
|
| 69 | ||