2021-06-11 11:03:31 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\Caching\ValueObject\Storage;
|
2021-06-11 11:03:31 +00:00
|
|
|
|
2021-10-18 17:44:46 +00:00
|
|
|
use FilesystemIterator;
|
2023-06-01 08:56:46 +00:00
|
|
|
use RectorPrefix202306\Nette\Utils\FileSystem;
|
|
|
|
use RectorPrefix202306\Nette\Utils\Random;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\Caching\Contract\ValueObject\Storage\CacheStorageInterface;
|
|
|
|
use Rector\Caching\ValueObject\CacheFilePaths;
|
|
|
|
use Rector\Caching\ValueObject\CacheItem;
|
|
|
|
use Rector\Core\Exception\Cache\CachingException;
|
2021-06-11 11:03:31 +00:00
|
|
|
/**
|
2021-07-26 13:28:02 +00:00
|
|
|
* Inspired by https://github.com/phpstan/phpstan-src/blob/1e7ceae933f07e5a250b61ed94799e6c2ea8daa2/src/Cache/FileCacheStorage.php
|
2021-10-18 17:44:46 +00:00
|
|
|
* @see \Rector\Tests\Caching\ValueObject\Storage\FileCacheStorageTest
|
2021-06-11 11:03:31 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class FileCacheStorage implements CacheStorageInterface
|
2021-06-11 11:03:31 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $directory;
|
|
|
|
/**
|
2022-08-29 22:42:57 +00:00
|
|
|
* @var \Symfony\Component\Filesystem\Filesystem
|
2021-06-11 11:03:31 +00:00
|
|
|
*/
|
2022-08-29 22:42:57 +00:00
|
|
|
private $filesystem;
|
2023-06-01 08:56:46 +00:00
|
|
|
public function __construct(string $directory, \RectorPrefix202306\Symfony\Component\Filesystem\Filesystem $filesystem)
|
2021-06-11 11:03:31 +00:00
|
|
|
{
|
|
|
|
$this->directory = $directory;
|
2022-08-29 22:42:57 +00:00
|
|
|
$this->filesystem = $filesystem;
|
2021-06-11 11:03:31 +00:00
|
|
|
}
|
2021-12-10 10:22:23 +00:00
|
|
|
public function load(string $key, string $variableKey)
|
2021-06-11 11:03:31 +00:00
|
|
|
{
|
2021-07-26 13:28:02 +00:00
|
|
|
return (function (string $key, string $variableKey) {
|
|
|
|
$cacheFilePaths = $this->getCacheFilePaths($key);
|
|
|
|
$filePath = $cacheFilePaths->getFilePath();
|
|
|
|
if (!\is_file($filePath)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$cacheItem = (require $filePath);
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$cacheItem instanceof CacheItem) {
|
2021-07-26 13:28:02 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (!$cacheItem->isVariableKeyValid($variableKey)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $cacheItem->getData();
|
|
|
|
})($key, $variableKey);
|
2021-06-11 11:03:31 +00:00
|
|
|
}
|
2022-07-27 08:28:59 +00:00
|
|
|
/**
|
|
|
|
* @param mixed $data
|
|
|
|
*/
|
2021-12-10 10:22:23 +00:00
|
|
|
public function save(string $key, string $variableKey, $data) : void
|
2021-06-11 11:03:31 +00:00
|
|
|
{
|
|
|
|
$cacheFilePaths = $this->getCacheFilePaths($key);
|
2022-08-29 22:42:57 +00:00
|
|
|
$this->filesystem->mkdir($cacheFilePaths->getFirstDirectory());
|
|
|
|
$this->filesystem->mkdir($cacheFilePaths->getSecondDirectory());
|
2021-07-26 13:28:02 +00:00
|
|
|
$path = $cacheFilePaths->getFilePath();
|
2022-06-07 08:22:29 +00:00
|
|
|
$tmpPath = \sprintf('%s/%s.tmp', $this->directory, Random::generate());
|
2021-06-11 11:03:31 +00:00
|
|
|
$errorBefore = \error_get_last();
|
2022-06-07 08:22:29 +00:00
|
|
|
$exported = @\var_export(new CacheItem($variableKey, $data), \true);
|
2021-06-11 11:03:31 +00:00
|
|
|
$errorAfter = \error_get_last();
|
|
|
|
if ($errorAfter !== null && $errorBefore !== $errorAfter) {
|
2022-06-07 08:22:29 +00:00
|
|
|
throw new CachingException(\sprintf('Error occurred while saving item %s (%s) to cache: %s', $key, $variableKey, $errorAfter['message']));
|
2021-07-26 13:28:02 +00:00
|
|
|
}
|
|
|
|
// for performance reasons we don't use SmartFileSystem
|
2022-06-07 08:22:29 +00:00
|
|
|
FileSystem::write($tmpPath, \sprintf("<?php declare(strict_types = 1);\n\nreturn %s;", $exported));
|
2021-07-26 13:28:02 +00:00
|
|
|
$renameSuccess = @\rename($tmpPath, $path);
|
|
|
|
if ($renameSuccess) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
@\unlink($tmpPath);
|
|
|
|
if (\DIRECTORY_SEPARATOR === '/' || !\file_exists($path)) {
|
2022-06-07 08:22:29 +00:00
|
|
|
throw new CachingException(\sprintf('Could not write data to cache file %s.', $path));
|
2021-06-11 11:03:31 +00:00
|
|
|
}
|
|
|
|
}
|
2021-12-10 10:22:23 +00:00
|
|
|
public function clean(string $key) : void
|
2021-06-11 11:03:31 +00:00
|
|
|
{
|
2021-07-28 07:05:44 +00:00
|
|
|
$cacheFilePaths = $this->getCacheFilePaths($key);
|
2021-10-18 17:44:46 +00:00
|
|
|
$this->processRemoveCacheFilePath($cacheFilePaths);
|
|
|
|
$this->processRemoveEmptyDirectory($cacheFilePaths->getSecondDirectory());
|
|
|
|
$this->processRemoveEmptyDirectory($cacheFilePaths->getFirstDirectory());
|
|
|
|
}
|
|
|
|
public function clear() : void
|
|
|
|
{
|
2022-08-29 22:42:57 +00:00
|
|
|
$this->filesystem->remove($this->directory);
|
2021-10-18 17:44:46 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function processRemoveCacheFilePath(CacheFilePaths $cacheFilePaths) : void
|
2021-10-18 17:44:46 +00:00
|
|
|
{
|
|
|
|
$filePath = $cacheFilePaths->getFilePath();
|
2022-08-29 22:42:57 +00:00
|
|
|
if (!$this->filesystem->exists($filePath)) {
|
2021-10-18 16:50:10 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-08-29 22:42:57 +00:00
|
|
|
$this->filesystem->remove($filePath);
|
2021-10-18 17:44:46 +00:00
|
|
|
}
|
|
|
|
private function processRemoveEmptyDirectory(string $directory) : void
|
|
|
|
{
|
2022-08-29 22:42:57 +00:00
|
|
|
if (!$this->filesystem->exists($directory)) {
|
2021-10-18 16:50:10 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-10-18 17:44:46 +00:00
|
|
|
if ($this->isNotEmptyDirectory($directory)) {
|
2021-10-18 16:50:10 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-08-29 22:42:57 +00:00
|
|
|
$this->filesystem->remove($directory);
|
2021-06-11 11:03:31 +00:00
|
|
|
}
|
2021-10-18 17:44:46 +00:00
|
|
|
private function isNotEmptyDirectory(string $directory) : bool
|
2021-06-11 11:03:31 +00:00
|
|
|
{
|
2021-10-18 17:44:46 +00:00
|
|
|
// FilesystemIterator will initially point to the first file in the folder - if there are no files in the folder, valid() will return false
|
2022-06-07 08:22:29 +00:00
|
|
|
$filesystemIterator = new FilesystemIterator($directory);
|
2021-10-18 17:44:46 +00:00
|
|
|
return $filesystemIterator->valid();
|
2021-06-11 11:03:31 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function getCacheFilePaths(string $key) : CacheFilePaths
|
2021-06-11 11:03:31 +00:00
|
|
|
{
|
|
|
|
$keyHash = \sha1($key);
|
2021-07-26 13:28:02 +00:00
|
|
|
$firstDirectory = \sprintf('%s/%s', $this->directory, \substr($keyHash, 0, 2));
|
|
|
|
$secondDirectory = \sprintf('%s/%s', $firstDirectory, \substr($keyHash, 2, 2));
|
2021-06-11 11:03:31 +00:00
|
|
|
$filePath = \sprintf('%s/%s.php', $secondDirectory, $keyHash);
|
2022-06-07 08:22:29 +00:00
|
|
|
return new CacheFilePaths($firstDirectory, $secondDirectory, $filePath);
|
2021-06-11 11:03:31 +00:00
|
|
|
}
|
|
|
|
}
|