Skip to content

Commit

Permalink
move from nette/caching to own cache based on PHPStan (#202)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jun 11, 2021
1 parent 9dea8d8 commit ac81e98
Show file tree
Hide file tree
Showing 14 changed files with 281 additions and 95 deletions.
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"doctrine/inflector": "^2.0",
"ergebnis/json-printer": "^3.1",
"idiosyncratic/editorconfig": "^0.1.3",
"nette/caching": "^3.1",
"nette/utils": "^3.2",
"nikic/php-parser": "4.10.5",
"phpstan/phpdoc-parser": "^0.5.4",
Expand Down
1 change: 1 addition & 0 deletions config/services-packages.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
__DIR__ . '/../packages/BetterPhpDocParser/PhpDocInfo/PhpDocInfo.php',
__DIR__ . '/../packages/Testing/PHPUnit',
__DIR__ . '/../packages/BetterPhpDocParser/PhpDoc',
__DIR__ . '/../packages/Caching/Cache.php',
__DIR__ . '/../packages/NodeTypeResolver/NodeVisitor/FileNodeVisitor.php',

// used in PHPStan
Expand Down
7 changes: 3 additions & 4 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Ergebnis\Json\Printer\Printer;
use Ergebnis\Json\Printer\PrinterInterface;
use Idiosyncratic\EditorConfig\EditorConfig;
use Nette\Caching\Cache;
use PhpParser\BuilderFactory;
use PhpParser\Lexer;
use PhpParser\NodeFinder;
Expand All @@ -26,7 +25,7 @@
use PHPStan\Reflection\ReflectionProvider;
use Rector\BetterPhpDocParser\PhpDocParser\BetterPhpDocParser;
use Rector\BetterPhpDocParser\PhpDocParser\BetterTypeParser;
use Rector\Caching\Cache\NetteCacheFactory;
use Rector\Caching\CacheFactory;
use Rector\Core\Console\ConsoleApplication;
use Rector\Core\PhpParser\Parser\NikicPhpParserFactory;
use Rector\Core\PhpParser\Parser\PhpParserLexerFactory;
Expand Down Expand Up @@ -128,8 +127,8 @@
$services->set(FileHelper::class)
->factory([service(PHPStanServicesFactory::class), 'createFileHelper']);

$services->set(Cache::class)
->factory([service(NetteCacheFactory::class), 'create']);
$services->set(\Rector\Caching\Cache::class)
->factory([service(CacheFactory::class), 'create']);

// type resolving
$services->set(IntermediateSourceLocator::class);
Expand Down
41 changes: 41 additions & 0 deletions packages/Caching/Cache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Rector\Caching;

use Rector\Caching\ValueObject\Storage\FileCacheStorage;

final class Cache
{
public function __construct(
private FileCacheStorage $fileCacheStorage
) {
}

/**
* @return mixed|null
*/
public function load(string $key, string $variableKey)
{
return $this->fileCacheStorage->load($key, $variableKey);
}

/**
* @param mixed $data
*/
public function save(string $key, string $variableKey, $data): void
{
$this->fileCacheStorage->save($key, $variableKey, $data);
}

public function clear(): void
{
$this->fileCacheStorage->clear();
}

public function clean(string $cacheKey): void
{
$this->fileCacheStorage->clean($cacheKey);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

declare(strict_types=1);

namespace Rector\Caching\Cache;
namespace Rector\Caching;

use Nette\Caching\Cache;
use Nette\Caching\Storages\FileStorage;
use Rector\Caching\ValueObject\Storage\FileCacheStorage;
use Rector\Core\Configuration\Option;
use Symplify\PackageBuilder\Parameter\ParameterProvider;
use Symplify\SmartFileSystem\SmartFileSystem;

final class NetteCacheFactory
final class CacheFactory
{
public function __construct(
private ParameterProvider $parameterProvider,
Expand All @@ -27,9 +26,7 @@ public function create(): Cache
$this->smartFileSystem->mkdir($cacheDirectory);
}

$fileStorage = new FileStorage($cacheDirectory);

// namespace is unique per project
return new Cache($fileStorage, getcwd());
$fileCacheStorage = new FileCacheStorage($cacheDirectory, $this->smartFileSystem);
return new Cache($fileCacheStorage);
}
}
1 change: 0 additions & 1 deletion packages/Caching/Config/FileHashComputer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

/**
* Inspired by https://github.com/symplify/easy-coding-standard/blob/e598ab54686e416788f28fcfe007fd08e0f371d9/packages/changed-files-detector/src/FileHashComputer.php
* @see \Rector\Caching\Tests\Config\FileHashComputerTest
*/
final class FileHashComputer
{
Expand Down
31 changes: 13 additions & 18 deletions packages/Caching/Detector/ChangedFilesDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,19 @@

namespace Rector\Caching\Detector;

use Nette\Caching\Cache;
use Nette\Utils\Strings;
use Rector\Caching\Cache;
use Rector\Caching\Config\FileHashComputer;
use Rector\Caching\Enum\CacheKey;
use Symplify\SmartFileSystem\SmartFileInfo;

/**
* Inspired by https://github.com/symplify/symplify/pull/90/files#diff-72041b2e1029a08930e13d79d298ef11
* @see \Rector\Caching\Tests\Detector\ChangedFilesDetectorTest
*
* @see \Rector\Tests\Caching\Detector\ChangedFilesDetectorTest
*/
final class ChangedFilesDetector
{
/**
* @var string
*/
private const CONFIGURATION_HASH_KEY = 'configuration_hash';

public function __construct(
private FileHashComputer $fileHashComputer,
private Cache $cache
Expand All @@ -34,8 +31,8 @@ public function addFileWithDependencies(SmartFileInfo $smartFileInfo, array $dep
$fileInfoCacheKey = $this->getFileInfoCacheKey($smartFileInfo);
$hash = $this->hashFile($smartFileInfo);

$this->cache->save($fileInfoCacheKey, $hash);
$this->cache->save($fileInfoCacheKey . '_files', $dependentFiles);
$this->cache->save($fileInfoCacheKey, CacheKey::FILE_HASH_KEY, $hash);
$this->cache->save($fileInfoCacheKey . '_files', CacheKey::DEPENDENT_FILES_KEY, $dependentFiles);
}

public function hasFileChanged(SmartFileInfo $smartFileInfo): bool
Expand All @@ -44,21 +41,19 @@ public function hasFileChanged(SmartFileInfo $smartFileInfo): bool

$fileInfoCacheKey = $this->getFileInfoCacheKey($smartFileInfo);

$cachedValue = $this->cache->load($fileInfoCacheKey);
$cachedValue = $this->cache->load($fileInfoCacheKey, CacheKey::FILE_HASH_KEY);
return $currentFileHash !== $cachedValue;
}

public function invalidateFile(SmartFileInfo $smartFileInfo): void
{
$fileInfoCacheKey = $this->getFileInfoCacheKey($smartFileInfo);
$this->cache->remove($fileInfoCacheKey);
$this->cache->clean($fileInfoCacheKey);
}

public function clear(): void
{
$this->cache->clean([
Cache::ALL => true,
]);
$this->cache->clear();
}

/**
Expand All @@ -68,7 +63,7 @@ public function getDependentFileInfos(SmartFileInfo $fileInfo): array
{
$fileInfoCacheKey = $this->getFileInfoCacheKey($fileInfo);

$cacheValue = $this->cache->load($fileInfoCacheKey . '_files');
$cacheValue = $this->cache->load($fileInfoCacheKey . '_files', CacheKey::DEPENDENT_FILES_KEY);
if ($cacheValue === null) {
return [];
}
Expand Down Expand Up @@ -109,15 +104,15 @@ private function hashFile(SmartFileInfo $smartFileInfo): string

private function storeConfigurationDataHash(SmartFileInfo $fileInfo, string $configurationHash): void
{
$key = self::CONFIGURATION_HASH_KEY . '_' . Strings::webalize($fileInfo->getRealPath());
$key = CacheKey::CONFIGURATION_HASH_KEY . '_' . Strings::webalize($fileInfo->getRealPath());
$this->invalidateCacheIfConfigurationChanged($key, $configurationHash);

$this->cache->save($key, $configurationHash);
$this->cache->save($key, CacheKey::CONFIGURATION_HASH_KEY, $configurationHash);
}

private function invalidateCacheIfConfigurationChanged(string $key, string $configurationHash): void
{
$oldCachedValue = $this->cache->load($key);
$oldCachedValue = $this->cache->load($key, CacheKey::CONFIGURATION_HASH_KEY);
if ($oldCachedValue === $configurationHash) {
return;
}
Expand Down
26 changes: 26 additions & 0 deletions packages/Caching/Enum/CacheKey.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Rector\Caching\Enum;

/**
* @enum
*/
final class CacheKey
{
/**
* @var string
*/
public const CONFIGURATION_HASH_KEY = 'configuration_hash';

/**
* @var string
*/
public const FILE_HASH_KEY = 'file_hash';

/**
* @var string
*/
public const DEPENDENT_FILES_KEY = 'dependency_files_key';
}
30 changes: 30 additions & 0 deletions packages/Caching/ValueObject/CacheFilePaths.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Rector\Caching\ValueObject;

final class CacheFilePaths
{
public function __construct(
private string $firstDirectory,
private string $secondDirectory,
private string $filePath
) {
}

public function getFirstDirectory(): string
{
return $this->firstDirectory;
}

public function getSecondDirectory(): string
{
return $this->secondDirectory;
}

public function getFilePath(): string
{
return $this->filePath;
}
}
42 changes: 42 additions & 0 deletions packages/Caching/ValueObject/CacheItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace Rector\Caching\ValueObject;

/**
* Inspired by
* https://github.com/phpstan/phpstan-src/commit/eeae2da7999b2e8b7b04542c6175d46f80c6d0b9#diff-6dc14f6222bf150e6840ca44a7126653052a1cedc6a149b4e5c1e1a2c80eacdc
*/
final class CacheItem
{
/**
* @param mixed $data
*/
public function __construct(
private string $variableKey,
private $data
) {
}

/**
* @param mixed[] $properties
*/
public static function __set_state(array $properties): self
{
return new self($properties['variableKey'], $properties['data']);
}

public function isVariableKeyValid(string $variableKey): bool
{
return $this->variableKey === $variableKey;
}

/**
* @return mixed
*/
public function getData()
{
return $this->data;
}
}

0 comments on commit ac81e98

Please sign in to comment.