<?php
namespace Symfony\Component\HttpKernel\HttpCache;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class Store implements StoreInterface {
protected $root;
private $keyCache;
private $locks;
public function __construct($root) {
$this->root = $root;
if (!is_dir($this->root)) {
mkdir($this->root, 0777, true);
}
$this->keyCache = new \SplObjectStorage();
$this->locks = array();
}
public function cleanup() {
foreach ($this->locks as $lock) {
@unlink($lock);
}
$error = error_get_last();
if (1 === $error['type'] && false === headers_sent()) {
header('HTTP/1.0 503 Service Unavailable');
header('Retry-After: 10');
echo '503 Service Unavailable';
}
}
public function lock(Request $request) {
$path = $this
->getPath($this
->getCacheKey($request) . '.lck');
if (!is_dir(dirname($path)) && false === @mkdir(dirname($path), 0777, true)) {
return false;
}
$lock = @fopen($path, 'x');
if (false !== $lock) {
fclose($lock);
$this->locks[] = $path;
return true;
}
return !file_exists($path) ?: $path;
}
public function unlock(Request $request) {
$file = $this
->getPath($this
->getCacheKey($request) . '.lck');
return is_file($file) ? @unlink($file) : false;
}
public function isLocked(Request $request) {
return is_file($this
->getPath($this
->getCacheKey($request) . '.lck'));
}
public function lookup(Request $request) {
$key = $this
->getCacheKey($request);
if (!($entries = $this
->getMetadata($key))) {
return null;
}
$match = null;
foreach ($entries as $entry) {
if ($this
->requestsMatch(isset($entry[1]['vary'][0]) ? $entry[1]['vary'][0] : '', $request->headers
->all(), $entry[0])) {
$match = $entry;
break;
}
}
if (null === $match) {
return null;
}
list($req, $headers) = $match;
if (is_file($body = $this
->getPath($headers['x-content-digest'][0]))) {
return $this
->restoreResponse($headers, $body);
}
return null;
}
public function write(Request $request, Response $response) {
$key = $this
->getCacheKey($request);
$storedEnv = $this
->persistRequest($request);
if (!$response->headers
->has('X-Content-Digest')) {
$digest = $this
->generateContentDigest($response);
if (false === $this
->save($digest, $response
->getContent())) {
throw new \RuntimeException('Unable to store the entity.');
}
$response->headers
->set('X-Content-Digest', $digest);
if (!$response->headers
->has('Transfer-Encoding')) {
$response->headers
->set('Content-Length', strlen($response
->getContent()));
}
}
$entries = array();
$vary = $response->headers
->get('vary');
foreach ($this
->getMetadata($key) as $entry) {
if (!isset($entry[1]['vary'][0])) {
$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('Unable to store the metadata.');
}
return $key;
}
protected function generateContentDigest(Response $response) {
return 'en' . sha1($response
->getContent());
}
public function invalidate(Request $request) {
$modified = false;
$key = $this
->getCacheKey($request);
$entries = array();
foreach ($this
->getMetadata($key) as $entry) {
$response = $this
->restoreResponse($entry[1]);
if ($response
->isFresh()) {
$response
->expire();
$modified = true;
$entries[] = array(
$entry[0],
$this
->persistResponse($response),
);
}
else {
$entries[] = $entry;
}
}
if ($modified) {
if (false === $this
->save($key, serialize($entries))) {
throw new \RuntimeException('Unable to store the metadata.');
}
}
}
private function requestsMatch($vary, $env1, $env2) {
if (empty($vary)) {
return true;
}
foreach (preg_split('/[\\s,]+/', $vary) as $header) {
$key = strtr(strtolower($header), '_', '-');
$v1 = isset($env1[$key]) ? $env1[$key] : null;
$v2 = isset($env2[$key]) ? $env2[$key] : null;
if ($v1 !== $v2) {
return false;
}
}
return true;
}
private function getMetadata($key) {
if (false === ($entries = $this
->load($key))) {
return array();
}
return unserialize($entries);
}
public function purge($url) {
if (is_file($path = $this
->getPath($this
->getCacheKey(Request::create($url))))) {
unlink($path);
return true;
}
return false;
}
private function load($key) {
$path = $this
->getPath($key);
return is_file($path) ? file_get_contents($path) : false;
}
private function save($key, $data) {
$path = $this
->getPath($key);
if (!is_dir(dirname($path)) && false === @mkdir(dirname($path), 0777, true)) {
return false;
}
$tmpFile = tempnam(dirname($path), basename($path));
if (false === ($fp = @fopen($tmpFile, 'wb'))) {
return false;
}
@fwrite($fp, $data);
@fclose($fp);
if ($data != file_get_contents($tmpFile)) {
return false;
}
if (false === @rename($tmpFile, $path)) {
return false;
}
@chmod($path, 0666 & ~umask());
}
public function getPath($key) {
return $this->root . DIRECTORY_SEPARATOR . substr($key, 0, 2) . DIRECTORY_SEPARATOR . substr($key, 2, 2) . DIRECTORY_SEPARATOR . substr($key, 4, 2) . DIRECTORY_SEPARATOR . substr($key, 6);
}
private function getCacheKey(Request $request) {
if (isset($this->keyCache[$request])) {
return $this->keyCache[$request];
}
return $this->keyCache[$request] = 'md' . sha1($request
->getUri());
}
private function persistRequest(Request $request) {
return $request->headers
->all();
}
private function persistResponse(Response $response) {
$headers = $response->headers
->all();
$headers['X-Status'] = array(
$response
->getStatusCode(),
);
return $headers;
}
private function restoreResponse($headers, $body = null) {
$status = $headers['X-Status'][0];
unset($headers['X-Status']);
if (null !== $body) {
$headers['X-Body-File'] = array(
$body,
);
}
return new Response($body, $status, $headers);
}
}