2016-02-08 10:35:06 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Symfony\Component\Cache\Adapter;
|
|
|
|
|
|
|
|
use Symfony\Component\Cache\Exception\InvalidArgumentException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Nicolas Grekas <p@tchwork.com>
|
|
|
|
*/
|
|
|
|
class FilesystemAdapter extends AbstractAdapter
|
|
|
|
{
|
|
|
|
private $directory;
|
|
|
|
|
2016-04-01 11:55:00 +01:00
|
|
|
public function __construct($namespace = '', $defaultLifetime = 0, $directory = null)
|
2016-02-08 10:35:06 +00:00
|
|
|
{
|
|
|
|
parent::__construct('', $defaultLifetime);
|
|
|
|
|
2016-03-05 16:35:55 +00:00
|
|
|
if (!isset($directory[0])) {
|
|
|
|
$directory = sys_get_temp_dir().'/symfony-cache';
|
|
|
|
}
|
|
|
|
if (isset($namespace[0])) {
|
2016-04-04 08:11:03 +01:00
|
|
|
if (preg_match('#[^-+_.A-Za-z0-9]#', $namespace, $match)) {
|
|
|
|
throw new InvalidArgumentException(sprintf('FilesystemAdapter namespace contains "%s" but only characters in [-+_.A-Za-z0-9] are allowed.', $match[0]));
|
|
|
|
}
|
2016-03-05 16:35:55 +00:00
|
|
|
$directory .= '/'.$namespace;
|
|
|
|
}
|
2016-02-08 10:35:06 +00:00
|
|
|
if (!file_exists($dir = $directory.'/.')) {
|
|
|
|
@mkdir($directory, 0777, true);
|
|
|
|
}
|
|
|
|
if (false === $dir = realpath($dir)) {
|
|
|
|
throw new InvalidArgumentException(sprintf('Cache directory does not exist (%s)', $directory));
|
|
|
|
}
|
|
|
|
if (!is_writable($dir .= DIRECTORY_SEPARATOR)) {
|
|
|
|
throw new InvalidArgumentException(sprintf('Cache directory is not writable (%s)', $directory));
|
|
|
|
}
|
|
|
|
// On Windows the whole path is limited to 258 chars
|
2016-03-10 08:59:31 +00:00
|
|
|
if ('\\' === DIRECTORY_SEPARATOR && strlen($dir) > 234) {
|
2016-02-08 10:35:06 +00:00
|
|
|
throw new InvalidArgumentException(sprintf('Cache directory too long (%s)', $directory));
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->directory = $dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
protected function doFetch(array $ids)
|
|
|
|
{
|
|
|
|
$values = array();
|
|
|
|
$now = time();
|
|
|
|
|
|
|
|
foreach ($ids as $id) {
|
|
|
|
$file = $this->getFile($id);
|
2016-08-08 14:21:59 +01:00
|
|
|
if (!file_exists($file) || !$h = @fopen($file, 'rb')) {
|
2016-02-08 10:35:06 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($now >= (int) $expiresAt = fgets($h)) {
|
|
|
|
fclose($h);
|
|
|
|
if (isset($expiresAt[0])) {
|
|
|
|
@unlink($file);
|
|
|
|
}
|
|
|
|
} else {
|
2016-03-10 08:59:31 +00:00
|
|
|
$i = rawurldecode(rtrim(fgets($h)));
|
2016-02-08 10:35:06 +00:00
|
|
|
$value = stream_get_contents($h);
|
|
|
|
fclose($h);
|
2016-03-10 08:59:31 +00:00
|
|
|
if ($i === $id) {
|
2016-08-08 14:21:59 +01:00
|
|
|
$values[$id] = parent::unserialize($value);
|
2016-03-10 08:59:31 +00:00
|
|
|
}
|
2016-02-08 10:35:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $values;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
protected function doHave($id)
|
|
|
|
{
|
|
|
|
$file = $this->getFile($id);
|
|
|
|
|
|
|
|
return file_exists($file) && (@filemtime($file) > time() || $this->doFetch(array($id)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
protected function doClear($namespace)
|
|
|
|
{
|
|
|
|
$ok = true;
|
|
|
|
|
|
|
|
foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->directory, \FilesystemIterator::SKIP_DOTS)) as $file) {
|
|
|
|
$ok = ($file->isDir() || @unlink($file) || !file_exists($file)) && $ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
protected function doDelete(array $ids)
|
|
|
|
{
|
|
|
|
$ok = true;
|
|
|
|
|
|
|
|
foreach ($ids as $id) {
|
|
|
|
$file = $this->getFile($id);
|
|
|
|
$ok = (!file_exists($file) || @unlink($file) || !file_exists($file)) && $ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
protected function doSave(array $values, $lifetime)
|
|
|
|
{
|
|
|
|
$ok = true;
|
2016-07-26 14:05:38 +01:00
|
|
|
$expiresAt = time() + ($lifetime ?: 31557600); // 31557600s = 1 year
|
2016-05-07 23:04:41 +01:00
|
|
|
$tmp = $this->directory.uniqid('', true);
|
2016-02-08 10:35:06 +00:00
|
|
|
|
|
|
|
foreach ($values as $id => $value) {
|
2016-04-26 16:44:27 +01:00
|
|
|
$file = $this->getFile($id, true);
|
|
|
|
|
2016-03-10 08:59:31 +00:00
|
|
|
$value = $expiresAt."\n".rawurlencode($id)."\n".serialize($value);
|
2016-05-07 23:04:41 +01:00
|
|
|
if (false !== @file_put_contents($tmp, $value)) {
|
|
|
|
@touch($tmp, $expiresAt);
|
|
|
|
$ok = @rename($tmp, $file) && $ok;
|
2016-02-08 10:35:06 +00:00
|
|
|
} else {
|
|
|
|
$ok = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $ok;
|
|
|
|
}
|
|
|
|
|
2016-04-26 16:44:27 +01:00
|
|
|
private function getFile($id, $mkdir = false)
|
2016-02-08 10:35:06 +00:00
|
|
|
{
|
2016-03-10 08:59:31 +00:00
|
|
|
$hash = str_replace('/', '-', base64_encode(md5($id, true)));
|
2016-04-26 16:44:27 +01:00
|
|
|
$dir = $this->directory.$hash[0].DIRECTORY_SEPARATOR.$hash[1].DIRECTORY_SEPARATOR;
|
|
|
|
|
|
|
|
if ($mkdir && !file_exists($dir)) {
|
|
|
|
@mkdir($dir, 0777, true);
|
|
|
|
}
|
2016-02-08 10:35:06 +00:00
|
|
|
|
2016-04-26 16:44:27 +01:00
|
|
|
return $dir.substr($hash, 2, -2);
|
2016-02-08 10:35:06 +00:00
|
|
|
}
|
|
|
|
}
|