mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-08 04:10:51 +00:00
Move symfony/cache to simpler nette/caching (#5889)
* composer: remove symfony/cache * [Caching] Add nette/cache, lighter * make sure cache directory exists
This commit is contained in:
parent
37eb07af6e
commit
6af2805a1c
|
@ -34,15 +34,14 @@
|
|||
"doctrine/annotations": "^1.12",
|
||||
"doctrine/inflector": "^2.0",
|
||||
"jean85/pretty-package-versions": "^1.5.1|^2.0.1",
|
||||
"nette/caching": "^3.1",
|
||||
"nette/utils": "^3.2",
|
||||
"nikic/php-parser": "^4.10.4",
|
||||
"nette/robot-loader": "^3.4",
|
||||
"phpstan/phpdoc-parser": "^0.4.12",
|
||||
"phpstan/phpstan": "^0.12.81",
|
||||
"phpstan/phpstan-phpunit": "^0.12.18",
|
||||
"psr/simple-cache": "^1.0",
|
||||
"sebastian/diff": "^4.0.4",
|
||||
"symfony/cache": "^4.4.8|^5.1",
|
||||
"symfony/console": "^4.4.8|^5.1",
|
||||
"symfony/dependency-injection": "^5.1",
|
||||
"symfony/finder": "^4.4.8|^5.1",
|
||||
|
|
|
@ -6,6 +6,7 @@ use Composer\Semver\VersionParser;
|
|||
use Doctrine\Common\Annotations\Reader;
|
||||
use Doctrine\Inflector\Inflector;
|
||||
use Doctrine\Inflector\Rules\English\InflectorFactory;
|
||||
use Nette\Caching\Cache;
|
||||
use PhpParser\BuilderFactory;
|
||||
use PhpParser\Lexer;
|
||||
use PhpParser\NodeFinder;
|
||||
|
@ -20,10 +21,8 @@ use PHPStan\File\FileHelper;
|
|||
use PHPStan\PhpDoc\TypeNodeResolver;
|
||||
use PHPStan\PhpDocParser\Parser\PhpDocParser;
|
||||
use PHPStan\Reflection\ReflectionProvider;
|
||||
use Psr\Cache\CacheItemPoolInterface;
|
||||
use Psr\SimpleCache\CacheInterface;
|
||||
use Rector\BetterPhpDocParser\PhpDocParser\BetterPhpDocParser;
|
||||
use Rector\Caching\Cache\Adapter\FilesystemAdapterFactory;
|
||||
use Rector\Caching\Cache\NetteCacheFactory;
|
||||
use Rector\Core\Console\ConsoleApplication;
|
||||
use Rector\Core\PhpParser\Parser\NikicPhpParserFactory;
|
||||
use Rector\Core\PhpParser\Parser\PhpParserLexerFactory;
|
||||
|
@ -31,10 +30,6 @@ use Rector\DoctrineAnnotationGenerated\ConstantPreservingAnnotationReader;
|
|||
use Rector\NodeTypeResolver\DependencyInjection\PHPStanServicesFactory;
|
||||
use Rector\NodeTypeResolver\Reflection\BetterReflection\SourceLocator\IntermediateSourceLocator;
|
||||
use Rector\NodeTypeResolver\Reflection\BetterReflection\SourceLocatorProvider\DynamicSourceLocatorProvider;
|
||||
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
|
||||
use Symfony\Component\Cache\Adapter\TagAwareAdapter;
|
||||
use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
|
||||
use Symfony\Component\Cache\Psr16Cache;
|
||||
use Symfony\Component\Console\Application as SymfonyApplication;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||
|
@ -127,14 +122,9 @@ return static function (ContainerConfigurator $containerConfigurator): void {
|
|||
->factory([service(PHPStanServicesFactory::class), 'createDependencyResolver']);
|
||||
$services->set(FileHelper::class)
|
||||
->factory([service(PHPStanServicesFactory::class), 'createFileHelper']);
|
||||
$services->set(Psr16Cache::class);
|
||||
$services->alias(CacheInterface::class, Psr16Cache::class);
|
||||
$services->set(FilesystemAdapter::class)
|
||||
->factory([service(FilesystemAdapterFactory::class), 'create']);
|
||||
$services->set(TagAwareAdapter::class)
|
||||
->arg('$itemsPool', service(FilesystemAdapter::class));
|
||||
$services->alias(CacheItemPoolInterface::class, FilesystemAdapter::class);
|
||||
$services->alias(TagAwareAdapterInterface::class, TagAwareAdapter::class);
|
||||
|
||||
$services->set(Cache::class)
|
||||
->factory([service(NetteCacheFactory::class), 'create']);
|
||||
|
||||
// type resolving
|
||||
$services->set(IntermediateSourceLocator::class);
|
||||
|
|
|
@ -7,7 +7,6 @@ namespace Rector\Caching\Application;
|
|||
use Rector\Caching\Detector\ChangedFilesDetector;
|
||||
use Rector\Caching\UnchangedFilesFilter;
|
||||
use Rector\Core\Configuration\Configuration;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
final class CachedFileInfoFilterAndReporter
|
||||
|
@ -22,11 +21,6 @@ final class CachedFileInfoFilterAndReporter
|
|||
*/
|
||||
private $changedFilesDetector;
|
||||
|
||||
/**
|
||||
* @var SymfonyStyle
|
||||
*/
|
||||
private $symfonyStyle;
|
||||
|
||||
/**
|
||||
* @var UnchangedFilesFilter
|
||||
*/
|
||||
|
@ -35,12 +29,10 @@ final class CachedFileInfoFilterAndReporter
|
|||
public function __construct(
|
||||
Configuration $configuration,
|
||||
ChangedFilesDetector $changedFilesDetector,
|
||||
SymfonyStyle $symfonyStyle,
|
||||
UnchangedFilesFilter $unchangedFilesFilter
|
||||
) {
|
||||
$this->configuration = $configuration;
|
||||
$this->changedFilesDetector = $changedFilesDetector;
|
||||
$this->symfonyStyle = $symfonyStyle;
|
||||
$this->unchangedFilesFilter = $unchangedFilesFilter;
|
||||
}
|
||||
|
||||
|
@ -59,11 +51,6 @@ final class CachedFileInfoFilterAndReporter
|
|||
$this->changedFilesDetector->clear();
|
||||
}
|
||||
|
||||
if ($this->configuration->isCacheDebug()) {
|
||||
$message = sprintf('[cache] %d files before cache filter', count($phpFileInfos));
|
||||
$this->symfonyStyle->note($message);
|
||||
}
|
||||
|
||||
return $this->unchangedFilesFilter->filterAndJoinWithDependentFileInfos($phpFileInfos);
|
||||
}
|
||||
}
|
||||
|
|
48
packages/Caching/Cache/NetteCacheFactory.php
Normal file
48
packages/Caching/Cache/NetteCacheFactory.php
Normal file
|
@ -0,0 +1,48 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Caching\Cache;
|
||||
|
||||
use Nette\Caching\Cache;
|
||||
use Nette\Caching\Storages\FileStorage;
|
||||
use Nette\Utils\Strings;
|
||||
use Rector\Core\Configuration\Option;
|
||||
use Symplify\PackageBuilder\Parameter\ParameterProvider;
|
||||
use Symplify\SmartFileSystem\SmartFileSystem;
|
||||
|
||||
final class NetteCacheFactory
|
||||
{
|
||||
/**
|
||||
* @var ParameterProvider
|
||||
*/
|
||||
private $parameterProvider;
|
||||
|
||||
/**
|
||||
* @var SmartFileSystem
|
||||
*/
|
||||
private $smartFileSystem;
|
||||
|
||||
public function __construct(ParameterProvider $parameterProvider, SmartFileSystem $smartFileSystem)
|
||||
{
|
||||
$this->parameterProvider = $parameterProvider;
|
||||
$this->smartFileSystem = $smartFileSystem;
|
||||
}
|
||||
|
||||
public function create(): Cache
|
||||
{
|
||||
$cacheDirectory = $this->parameterProvider->provideStringParameter(Option::CACHE_DIR);
|
||||
|
||||
// ensure cache directory exists
|
||||
if (! $this->smartFileSystem->exists($cacheDirectory)) {
|
||||
$this->smartFileSystem->mkdir($cacheDirectory);
|
||||
}
|
||||
|
||||
$fileStorage = new FileStorage($cacheDirectory);
|
||||
|
||||
// namespace is unique per project
|
||||
$namespace = Strings::webalize(getcwd());
|
||||
|
||||
return new Cache($fileStorage, $namespace);
|
||||
}
|
||||
}
|
|
@ -11,7 +11,6 @@ use Symfony\Component\Config\Loader\LoaderResolver;
|
|||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Loader\GlobFileLoader;
|
||||
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
|
||||
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
/**
|
||||
|
@ -22,7 +21,7 @@ final class FileHashComputer
|
|||
{
|
||||
public function compute(SmartFileInfo $fileInfo): string
|
||||
{
|
||||
$this->ensureIsYamlOrPhp($fileInfo);
|
||||
$this->ensureIsPhp($fileInfo);
|
||||
|
||||
$containerBuilder = new ContainerBuilder();
|
||||
$fileLoader = $this->createFileLoader($fileInfo, $containerBuilder);
|
||||
|
@ -30,18 +29,19 @@ final class FileHashComputer
|
|||
$fileLoader->load($fileInfo->getRealPath());
|
||||
|
||||
$parameterBag = $containerBuilder->getParameterBag();
|
||||
|
||||
return $this->arrayToHash($containerBuilder->getDefinitions()) . $this->arrayToHash($parameterBag->all());
|
||||
}
|
||||
|
||||
private function ensureIsYamlOrPhp(SmartFileInfo $fileInfo): void
|
||||
private function ensureIsPhp(SmartFileInfo $fileInfo): void
|
||||
{
|
||||
if ($fileInfo->hasSuffixes(['yml', 'yaml', 'php'])) {
|
||||
if ($fileInfo->hasSuffixes(['php'])) {
|
||||
return;
|
||||
}
|
||||
|
||||
throw new ShouldNotHappenException(sprintf(
|
||||
// getRealPath() cannot be used, as it breaks in phar
|
||||
'Provide only YAML/PHP file, ready for Symfony Dependency Injection. "%s" given', $fileInfo->getRelativeFilePath()
|
||||
'Provide only PHP file, ready for Symfony Dependency Injection. "%s" given', $fileInfo->getRelativeFilePath()
|
||||
));
|
||||
}
|
||||
|
||||
|
@ -52,7 +52,6 @@ final class FileHashComputer
|
|||
$fileLoaders = [
|
||||
new GlobFileLoader($containerBuilder, $fileLocator),
|
||||
new PhpFileLoader($containerBuilder, $fileLocator),
|
||||
new YamlFileLoader($containerBuilder, $fileLocator),
|
||||
];
|
||||
|
||||
$loaderResolver = new LoaderResolver($fileLoaders);
|
||||
|
|
|
@ -4,9 +4,9 @@ declare(strict_types=1);
|
|||
|
||||
namespace Rector\Caching\Detector;
|
||||
|
||||
use Nette\Caching\Cache;
|
||||
use Nette\Utils\Strings;
|
||||
use Rector\Caching\Config\FileHashComputer;
|
||||
use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
/**
|
||||
|
@ -20,20 +20,20 @@ final class ChangedFilesDetector
|
|||
*/
|
||||
private const CONFIGURATION_HASH_KEY = 'configuration_hash';
|
||||
|
||||
/**
|
||||
* @var TagAwareAdapterInterface
|
||||
*/
|
||||
private $tagAwareAdapter;
|
||||
|
||||
/**
|
||||
* @var FileHashComputer
|
||||
*/
|
||||
private $fileHashComputer;
|
||||
|
||||
public function __construct(FileHashComputer $fileHashComputer, TagAwareAdapterInterface $tagAwareAdapter)
|
||||
/**
|
||||
* @var Cache
|
||||
*/
|
||||
private $cache;
|
||||
|
||||
public function __construct(FileHashComputer $fileHashComputer, Cache $cache)
|
||||
{
|
||||
$this->tagAwareAdapter = $tagAwareAdapter;
|
||||
$this->fileHashComputer = $fileHashComputer;
|
||||
$this->cache = $cache;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -44,9 +44,8 @@ final class ChangedFilesDetector
|
|||
$fileInfoCacheKey = $this->getFileInfoCacheKey($smartFileInfo);
|
||||
$hash = $this->hashFile($smartFileInfo);
|
||||
|
||||
$this->saveItemWithValue($fileInfoCacheKey, $hash);
|
||||
|
||||
$this->saveItemWithValue($fileInfoCacheKey . '_files', $dependentFiles);
|
||||
$this->cache->save($fileInfoCacheKey, $hash);
|
||||
$this->cache->save($fileInfoCacheKey . '_files', $dependentFiles);
|
||||
}
|
||||
|
||||
public function hasFileChanged(SmartFileInfo $smartFileInfo): bool
|
||||
|
@ -54,23 +53,22 @@ final class ChangedFilesDetector
|
|||
$currentFileHash = $this->hashFile($smartFileInfo);
|
||||
|
||||
$fileInfoCacheKey = $this->getFileInfoCacheKey($smartFileInfo);
|
||||
$cacheItem = $this->tagAwareAdapter->getItem($fileInfoCacheKey);
|
||||
|
||||
$oldFileHash = $cacheItem->get();
|
||||
|
||||
return $currentFileHash !== $oldFileHash;
|
||||
$cachedValue = $this->cache->load($fileInfoCacheKey);
|
||||
return $currentFileHash !== $cachedValue;
|
||||
}
|
||||
|
||||
public function invalidateFile(SmartFileInfo $smartFileInfo): void
|
||||
{
|
||||
$fileInfoCacheKey = $this->getFileInfoCacheKey($smartFileInfo);
|
||||
|
||||
$this->tagAwareAdapter->deleteItem($fileInfoCacheKey);
|
||||
$this->cache->remove($fileInfoCacheKey);
|
||||
}
|
||||
|
||||
public function clear(): void
|
||||
{
|
||||
$this->tagAwareAdapter->clear();
|
||||
$this->cache->clean([
|
||||
Cache::ALL => true,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -80,14 +78,14 @@ final class ChangedFilesDetector
|
|||
{
|
||||
$fileInfoCacheKey = $this->getFileInfoCacheKey($fileInfo);
|
||||
|
||||
$cacheItem = $this->tagAwareAdapter->getItem($fileInfoCacheKey . '_files');
|
||||
if ($cacheItem->get() === null) {
|
||||
$cacheValue = $this->cache->load($fileInfoCacheKey . '_files');
|
||||
if ($cacheValue === null) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$dependentFileInfos = [];
|
||||
|
||||
$dependentFiles = $cacheItem->get();
|
||||
$dependentFiles = $cacheValue;
|
||||
foreach ($dependentFiles as $dependentFile) {
|
||||
if (! file_exists($dependentFile)) {
|
||||
continue;
|
||||
|
@ -119,34 +117,22 @@ final class ChangedFilesDetector
|
|||
return (string) sha1_file($smartFileInfo->getRealPath());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
*/
|
||||
private function saveItemWithValue(string $key, $value): void
|
||||
{
|
||||
$cacheItem = $this->tagAwareAdapter->getItem($key);
|
||||
$cacheItem->set($value);
|
||||
|
||||
$this->tagAwareAdapter->save($cacheItem);
|
||||
}
|
||||
|
||||
private function storeConfigurationDataHash(SmartFileInfo $fileInfo, string $configurationHash): void
|
||||
{
|
||||
$key = self::CONFIGURATION_HASH_KEY . '_' . Strings::webalize($fileInfo->getRealPath());
|
||||
|
||||
$this->invalidateCacheIfConfigurationChanged($key, $configurationHash);
|
||||
|
||||
$this->saveItemWithValue($key, $configurationHash);
|
||||
$this->cache->save($key, $configurationHash);
|
||||
}
|
||||
|
||||
private function invalidateCacheIfConfigurationChanged(string $key, string $configurationHash): void
|
||||
{
|
||||
$cacheItem = $this->tagAwareAdapter->getItem($key);
|
||||
|
||||
$oldConfigurationHash = $cacheItem->get();
|
||||
if ($configurationHash !== $oldConfigurationHash) {
|
||||
// should be unique per getcwd()
|
||||
$this->clear();
|
||||
$oldCachedValue = $this->cache->load($key);
|
||||
if ($oldCachedValue === $configurationHash) {
|
||||
return;
|
||||
}
|
||||
|
||||
// should be unique per getcwd()
|
||||
$this->clear();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user