AssetManager\Service\AssetManager::setAssetOnResponse PHP Method

setAssetOnResponse() public method

Set the asset on the response, including headers and content.
public setAssetOnResponse ( Zend\Stdlib\ResponseInterface $response ) : Zend\Stdlib\ResponseInterface
$response Zend\Stdlib\ResponseInterface
return Zend\Stdlib\ResponseInterface
    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;
    }

Usage Example

 public function testClearOutputBufferInSetAssetOnResponse()
 {
     $this->expectOutputString(file_get_contents(__FILE__));
     echo "This string would definately break any image source.\n";
     echo "This one would make it even worse.\n";
     echo "They all should be gone soon...\n";
     $assetFilterManager = new AssetFilterManager();
     $assetCacheManager = $this->getAssetCacheManagerMock();
     $mimeResolver = new MimeResolver();
     $assetManager = new AssetManager($this->getResolver(), array('clear_output_buffer' => true));
     $assetFilterManager->setMimeResolver($mimeResolver);
     $assetManager->setAssetFilterManager($assetFilterManager);
     $assetManager->setAssetCacheManager($assetCacheManager);
     $assetManager->resolvesToAsset($this->getRequest());
     $response = $assetManager->setAssetOnResponse(new Response());
     echo $response->getContent();
 }
All Usage Examples Of AssetManager\Service\AssetManager::setAssetOnResponse