public function setAssetOnResponse(ResponseInterface $response)
{
if (!$this->asset instanceof AssetInterface) {
throw new Exception\RuntimeException('Unable to set asset on response. Request has not been resolved to an asset.');
}
// @todo: Create Asset wrapper for mimetypes
if (empty($this->asset->mimetype)) {
throw new Exception\RuntimeException('Expected property "mimetype" on asset.');
}
$this->getAssetFilterManager()->setFilters($this->path, $this->asset);
$this->asset = $this->getAssetCacheManager()->setCache($this->path, $this->asset);
$mimeType = $this->asset->mimetype;
$assetContents = $this->asset->dump();
// @codeCoverageIgnoreStart
if (function_exists('mb_strlen')) {
$contentLength = mb_strlen($assetContents, '8bit');
} else {
$contentLength = strlen($assetContents);
}
// @codeCoverageIgnoreEnd
if (!empty($this->config['clear_output_buffer']) && $this->config['clear_output_buffer']) {
// Only clean the output buffer if it's turned on and something
// has been buffered.
if (ob_get_length() > 0) {
ob_clean();
}
}
$response->getHeaders()->addHeaderLine('Content-Transfer-Encoding', 'binary')->addHeaderLine('Content-Type', $mimeType)->addHeaderLine('Content-Length', $contentLength);
$response->setContent($assetContents);
$this->assetSetOnResponse = true;
return $response;
}