/** * Injects the web debug toolbar into a given HTML string. * * @param string $content The HTML content * * @return Response A Response instance */ protected function injectToolbar(Request $request, Response $response) { $data = ''; foreach ($this->profiler->getCollectors() as $name => $collector) { $data .= $collector->getSummary(); } $position = false === strpos($request->headers->get('user-agent'), 'Mobile') ? 'fixed' : 'absolute'; $toolbar = <<<EOF <!-- START of Symfony 2 Web Debug Toolbar --> <div style="clear: both; height: 40px;"></div> <div style="position: {$position}; bottom: 0px; left:0; z-index: 6000000; width: 100%; background: #dde4eb; border-top: 1px solid #bbb; padding: 5px; margin: 0; font: 11px Verdana, Arial, sans-serif; color: #222;"> {$data} </div> <!-- END of Symfony 2 Web Debug Toolbar --> EOF; $toolbar = "\n" . str_replace("\n", '', $toolbar) . "\n"; $count = 0; $content = str_ireplace('</body>', $toolbar . '</body>', $response->getContent(), $count); if (!$count) { $content .= $toolbar; } return $content; }
/** * Replaces a Response ESI tags with the included resource content. * * @param Symfony\Components\HttpFoundation\Request $request A Request instance * @param Symfony\Components\HttpFoundation\Response $response A Response instance */ public function process(Request $request, Response $response) { $this->request = $request; $type = $response->headers->get('Content-Type'); if (empty($type)) { $type = 'text/html'; } if (!in_array($type, $this->contentTypes)) { return $response; } // we don't use a proper XML parser here as we can have ESI tags in a plain text response $content = $response->getContent(); $content = preg_replace_callback('#<esi\\:include\\s+(.+?)\\s*/>#', array($this, 'handleEsiIncludeTag'), $content); $content = preg_replace('#<esi\\:comment[^>]*/>#', '', $content); $content = preg_replace('#<esi\\:remove>.*?</esi\\:remove>#', '', $content); $response->setContent($content); $response->headers->set('X-Body-Eval', 'ESI'); // remove ESI/1.0 from the Surrogate-Control header $value = $response->headers->get('Surrogate-Control'); if (preg_match('#^content="ESI/1.0"$#', $value)) { $response->headers->delete('Surrogate-Control'); } else { $response->headers->set('Surrogate-Control', preg_replace('#ESI/1.0#', '', $value)); } }
/** * Writes a cache entry to the store for the given Request and Response. * * Existing entries are read and any that match the response are removed. This * method calls write with the new list of cache entries. * * @param Symfony\Components\HttpFoundation\Request $request A Request instance * @param Symfony\Components\HttpFoundation\Response $response A Response instance * * @return string The key under which the response is stored */ public function write(Request $request, Response $response) { $key = $this->getCacheKey($request); $storedEnv = $this->persistRequest($request); // write the response body to the entity store if this is the original response if (!$response->headers->has('X-Content-Digest')) { $digest = 'en' . sha1($response->getContent()); if (false === $this->save($digest, $response->getContent())) { throw new \RuntimeException(sprintf('Unable to store the entity (%s).', $e->getMessage())); } $response->headers->set('X-Content-Digest', $digest); if (!$response->headers->has('Transfer-Encoding')) { $response->headers->set('Content-Length', strlen($response->getContent())); } } // read existing cache entries, remove non-varying, and add this one to the list $entries = array(); $vary = $response->headers->get('vary'); foreach ($this->getMetadata($key) as $entry) { if (!isset($entry[1]['vary'])) { $entry[1]['vary'] = array(''); } if ($vary != $entry[1]['vary'][0] || !$this->requestsMatch($vary, $entry[0], $storedEnv)) { $entries[] = $entry; } } $headers = $this->persistResponse($response); unset($headers['age']); array_unshift($entries, array($storedEnv, $headers)); if (false === $this->save($key, serialize($entries))) { throw new \RuntimeException(sprintf('Unable to store the metadata (%s).', $e->getMessage())); } return $key; }
/** * Restores the Response body. * * @param Symfony\Components\HttpFoundation\Response $response A Response instance * * @return Symfony\Components\HttpFoundation\Response A Response instance */ protected function restoreResponseBody(Response $response) { if ($response->headers->has('X-Body-Eval')) { ob_start(); if ($response->headers->has('X-Body-File')) { include $response->headers->get('X-Body-File'); } else { eval('; ?>' . $response->getContent() . '<?php ;'); } $response->setContent(ob_get_clean()); $response->headers->delete('X-Body-Eval'); } elseif ($response->headers->has('X-Body-File')) { $response->setContent(file_get_contents($response->headers->get('X-Body-File'))); } else { return; } $response->headers->delete('X-Body-File'); if (!$response->headers->has('Transfer-Encoding')) { $response->headers->set('Content-Length', strlen($response->getContent())); } }