Exemplo n.º 1
0
 /**
  * Execute the request
  *
  * @param RequestInterface $request
  * @param ResponseInterface $response (Default: null)
  * @return \Zend\Http\PhpEnvironment\Response
  */
 public function dispatch(RequestInterface $request, ResponseInterface $response = null)
 {
     // the config hash
     $config = $this->getServiceLocator()->get('Config');
     $config = $config['TpMinify'];
     // some important stuff
     $config['serveOptions']['quiet'] = true;
     // the time correction
     Minify::$uploaderHoursBehind = $config['uploaderHoursBehind'];
     // the cache engine
     Minify::setCache($config['cachePath'] ?: '', $config['cacheFileLocking']);
     // doc root corrections
     if ($config['documentRoot']) {
         $_SERVER['DOCUMENT_ROOT'] = $config['documentRoot'];
         Minify::$isDocRootSet = true;
     }
     // check for URI versioning
     if (preg_match('~&\\d~', $request->getUriString())) {
         $config['serveOptions']['maxAge'] = 31536000;
     }
     // minify result as array of information
     $result = Minify::serve('MinApp', $config['serveOptions']);
     // some corrections
     if (isset($result['headers']['_responseCode'])) {
         unset($result['headers']['_responseCode']);
     }
     // the headers set
     $headers = new Headers();
     $headers->addHeaders($result['headers']);
     // final output
     return $response->setHeaders($headers)->setStatusCode($result['statusCode'])->setContent($result['content']);
 }
Exemplo n.º 2
0
 /**
  * @param \Zend\Stdlib\ResponseInterface $r
  * @param \Zend_Controller_Response_Abstract $response
  */
 public function renderIntoResponse(Response $r, \Zend_Controller_Response_Abstract $response)
 {
     // render ZF1 response into ZF2 response
     if ($response->isException() && $response->renderExceptions()) {
         $exceptions = '';
         foreach ($response->getException() as $e) {
             $exceptions .= $e . "\n";
         }
         $body = $exceptions;
     } else {
         $body = $response->getBody();
     }
     $r->setContent($body);
     if ($r instanceof HttpResponse) {
         $r->setStatusCode($response->getHttpResponseCode());
         $r->setHeaders($this->getHeadersFromResponse($response));
         $type = $r->getHeaders()->get('Content-Type');
         if (!$type) {
             $r->getHeaders()->addHeaderLine('Content-Type', 'text/html');
         }
     }
 }
Exemplo n.º 3
0
 /**
  * Set the asset on the response, including headers and content.
  *
  * @param    ResponseInterface $response
  * @return   ResponseInterface
  * @throws   Exception\RuntimeException
  */
 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;
 }
Exemplo n.º 4
0
 /**
  * @param ResponseInterface $response
  */
 protected function checkErrors(ResponseInterface $response)
 {
     if ($response->isServerError() || $response->isClientError()) {
         throw new Exception\RuntimeException('An error occurred sending request. Status code: ' . $response->getStatusCode());
     }
 }
Exemplo n.º 5
0
 /**
  * Basic functionality for when a page is not available
  *
  * @return array
  */
 public function notFoundAction()
 {
     $this->response->setStatusCode(404);
     return array('content' => 'Page not found');
 }