From 91cf7cc721c70ec8914c1bc4855a810bb85b640c Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Tue, 13 Jun 2023 19:30:34 +0700 Subject: [PATCH] Remove CachedContainerBuilder (#4211) * Remove RectorKernel cache * Remove RectorKernel cache * remove unused class * fix phpstan * clean up * Fix phpstan * clean up phpstan config --- .github/workflows/tests.yaml | 7 -- .../Command/WorkerCommandLineFactoryTest.php | 2 +- .../FileSystem/FnMatchPathNormalizerTest.php | 2 +- phpstan.neon | 11 --- src/Console/Command/ProcessCommand.php | 2 - .../Cache/StaleContainerCacheException.php | 11 --- src/Kernel/CacheInvalidatingContainer.php | 70 -------------- src/Kernel/CachedContainerBuilder.php | 78 --------------- src/Kernel/RectorKernel.php | 96 +------------------ .../ConfigurableArrayMissingTest.php | 2 +- .../EmptyConfigurableRectorCollectorTest.php | 2 +- .../EmptyConfigureTest.php | 2 +- 12 files changed, 9 insertions(+), 276 deletions(-) delete mode 100644 src/Exception/Cache/StaleContainerCacheException.php delete mode 100644 src/Kernel/CacheInvalidatingContainer.php delete mode 100644 src/Kernel/CachedContainerBuilder.php diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index a0b926083e2..dc01bf8e36b 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -46,11 +46,4 @@ jobs: - uses: "ramsey/composer-install@v2" - - name: Cache rector kernels temporary files - uses: actions/cache@v3 - with: - path: /tmp/rector/ - key: ${{ matrix.php }}-${{ matrix.path }}-rector-kernel-${{ github.run_id }} - restore-keys: ${{ matrix.php }}-${{ matrix.path }}-rector-kernel- - - run: vendor/bin/phpunit ${{ matrix.path }} --colors diff --git a/packages-tests/Parallel/Command/WorkerCommandLineFactoryTest.php b/packages-tests/Parallel/Command/WorkerCommandLineFactoryTest.php index 6e2449fc7d8..a28a1ebea7b 100644 --- a/packages-tests/Parallel/Command/WorkerCommandLineFactoryTest.php +++ b/packages-tests/Parallel/Command/WorkerCommandLineFactoryTest.php @@ -35,7 +35,7 @@ final class WorkerCommandLineFactoryTest extends TestCase protected function setUp(): void { $rectorKernel = new RectorKernel(); - $containerBuilder = $rectorKernel->createBuilder(); + $containerBuilder = $rectorKernel->create(); $this->workerCommandLineFactory = $containerBuilder->get(WorkerCommandLineFactory::class); $this->processCommand = $containerBuilder->get(ProcessCommand::class); diff --git a/packages-tests/Skipper/FileSystem/FnMatchPathNormalizerTest.php b/packages-tests/Skipper/FileSystem/FnMatchPathNormalizerTest.php index 7c6820bd10e..bdb040f619a 100644 --- a/packages-tests/Skipper/FileSystem/FnMatchPathNormalizerTest.php +++ b/packages-tests/Skipper/FileSystem/FnMatchPathNormalizerTest.php @@ -17,7 +17,7 @@ final class FnMatchPathNormalizerTest extends TestCase protected function setUp(): void { $rectorKernel = new RectorKernel(); - $containerBuilder = $rectorKernel->createBuilder(); + $containerBuilder = $rectorKernel->create(); $this->fnMatchPathNormalizer = $containerBuilder->get(FnMatchPathNormalizer::class); } diff --git a/phpstan.neon b/phpstan.neon index 4aa1ef5d754..afc4d33a608 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -634,17 +634,6 @@ parameters: # resolve continually - '#Property Rector\\Core\\Contract\\PhpParser\\Node\\StmtsAwareInterface\:\:\$stmts \(array\|null\) does not accept array#' - - # statics are required in the kernel for performance reasons - - - message: '#Do not use static property#' - path: src/Kernel/RectorKernel.php - - # '@package_version@' compare with '@package_version@' to verify on rector-src - - - message: '#Strict comparison using \!\=\= between .* and .* will always evaluate to false#' - path: src/Kernel/RectorKernel.php - # stmts aware/expression generics - '#PhpParser\\Node\\Stmt\\Expression is not generic#' - '#Access to an undefined property PhpParser\\Node\\Stmt&Rector\\Core\\Contract\\PhpParser\\Node\\StmtsAwareInterface\:\:\$stmts#' diff --git a/src/Console/Command/ProcessCommand.php b/src/Console/Command/ProcessCommand.php index 198bafcb6b7..68c3a599f1f 100644 --- a/src/Console/Command/ProcessCommand.php +++ b/src/Console/Command/ProcessCommand.php @@ -14,7 +14,6 @@ use Rector\Core\Console\Output\OutputFormatterCollector; use Rector\Core\Contract\Console\OutputStyleInterface; use Rector\Core\Exception\ShouldNotHappenException; -use Rector\Core\Kernel\RectorKernel; use Rector\Core\StaticReflection\DynamicSourceLocatorDecorator; use Rector\Core\Util\MemoryLimiter; use Rector\Core\Validation\EmptyConfigurableRectorChecker; @@ -109,7 +108,6 @@ protected function initialize(InputInterface $input, OutputInterface $output): v $optionClearCache = (bool) $input->getOption(Option::CLEAR_CACHE); if ($optionDebug || $optionClearCache) { $this->changedFilesDetector->clear(); - RectorKernel::clearCache(); } } diff --git a/src/Exception/Cache/StaleContainerCacheException.php b/src/Exception/Cache/StaleContainerCacheException.php deleted file mode 100644 index c9b18da3e14..00000000000 --- a/src/Exception/Cache/StaleContainerCacheException.php +++ /dev/null @@ -1,11 +0,0 @@ -container->set($id, $service); - } - - public function get(string $id, int $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE): ?object - { - try { - return $this->container->get($id, $invalidBehavior); - } catch (Throwable $throwable) { - // clear compiled container cache, to trigger re-discovery - RectorKernel::clearCache(); - - throw new StaleContainerCacheException( - 'Container cache is outdated and was cleared. please re-run the command.', - 0, - $throwable - ); - } - } - - public function has(string $id): bool - { - return $this->container->has($id); - } - - public function initialized(string $id): bool - { - return $this->container->initialized($id); - } - - /** - * @return array|bool|float|int|string|UnitEnum|null - */ - public function getParameter(string $name) - { - return $this->container->getParameter($name); - } - - public function hasParameter(string $name): bool - { - return $this->container->hasParameter($name); - } - - /** - * @param UnitEnum|float|array|bool|int|string|null $value - */ - public function setParameter(string $name, UnitEnum|float|array|bool|int|string|null $value): void - { - $this->container->setParameter($name, $value); - } -} diff --git a/src/Kernel/CachedContainerBuilder.php b/src/Kernel/CachedContainerBuilder.php deleted file mode 100644 index b9a97b729d5..00000000000 --- a/src/Kernel/CachedContainerBuilder.php +++ /dev/null @@ -1,78 +0,0 @@ -cacheDir . 'kernel-' . $this->cacheKey . '-' . $hash . '.php'; - - if (file_exists($file)) { - require_once $file; - $className = '\\' . __NAMESPACE__ . '\\' . $className; - $cachedContainer = new $className(); - if (! $cachedContainer instanceof ContainerInterface) { - throw new ShouldNotHappenException(); - } - - $container = new CacheInvalidatingContainer($cachedContainer); - } else { - $container = ($containerBuilderCallback)($configFiles); - - $phpDumper = new PhpDumper($container); - $dumpedContainer = $phpDumper->dump([ - 'class' => $className, - 'namespace' => __NAMESPACE__, - ]); - if (! is_string($dumpedContainer)) { - throw new ShouldNotHappenException(); - } - - $filesystem->dumpFile($file, $dumpedContainer); - } - - return $container; - } - - public function clearCache(): void - { - if (! is_writable($this->cacheDir)) { - return; - } - - $cacheFiles = glob($this->cacheDir . 'kernel-*.php'); - if ($cacheFiles === false) { - return; - } - - $filesystem = new Filesystem(); - $filesystem->remove($cacheFiles); - } -} diff --git a/src/Kernel/RectorKernel.php b/src/Kernel/RectorKernel.php index c4facd3d99d..27b989f40ce 100644 --- a/src/Kernel/RectorKernel.php +++ b/src/Kernel/RectorKernel.php @@ -4,65 +4,28 @@ namespace Rector\Core\Kernel; -use Rector\Core\Application\VersionResolver; use Rector\Core\Exception\ShouldNotHappenException; -use Rector\Core\Util\FileHasher; -use Rector\Testing\PHPUnit\StaticPHPUnitEnvironment; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\ContainerInterface; -use Webmozart\Assert\Assert; final class RectorKernel { - /** - * @var string - */ - private const CACHE_KEY = 'v119'; - private ContainerInterface|null $container = null; - private bool $dumpFileCache = false; - - private static ?string $defaultFilesHash = null; - - public function __construct() - { - // while running tests we use different DI containers a lot, - // therefore make sure we don't compile them over and over again on rector-src. - if (! StaticPHPUnitEnvironment::isPHPUnitRun()) { - return; - } - - if ($this->isPrefixedBuild()) { - return; - } - - $this->dumpFileCache = true; - } - /** - * @param string[] $configFiles * @api used in tests */ - public function createBuilder(array $configFiles = []): ContainerBuilder + public function create(): ContainerInterface { - return $this->buildContainer($configFiles); + return $this->createFromConfigs([]); } /** * @param string[] $configFiles - * @api used in tests */ - public function createFromConfigs(array $configFiles): ContainerInterface + public function createFromConfigs(array $configFiles): ContainerBuilder { - if ($configFiles === []) { - return $this->buildContainer([]); - } - - $container = $this->dumpFileCache ? $this->buildCachedContainer($configFiles) : $this->buildContainer( - $configFiles - ); - + $container = $this->buildContainer($configFiles); return $this->container = $container; } @@ -78,12 +41,6 @@ public function getContainer(): ContainerInterface return $this->container; } - public static function clearCache(): void - { - $cachedContainerBuilder = new CachedContainerBuilder(self::getCacheDir(), self::CACHE_KEY); - $cachedContainerBuilder->clearCache(); - } - /** * @return string[] */ @@ -92,23 +49,6 @@ private function createDefaultConfigFiles(): array return [__DIR__ . '/../../config/config.php']; } - /** - * @param string[] $configFiles - */ - private function createConfigsHash(array $configFiles): string - { - $fileHasher = new FileHasher(); - - if (self::$defaultFilesHash === null) { - self::$defaultFilesHash = $fileHasher->hashFiles($this->createDefaultConfigFiles()); - } - - Assert::allString($configFiles); - $configHash = $fileHasher->hashFiles($configFiles); - - return self::$defaultFilesHash . $configHash; - } - /** * @param string[] $configFiles */ @@ -121,32 +61,4 @@ private function buildContainer(array $configFiles): ContainerBuilder return $this->container = $containerBuilderBuilder->build($configFiles); } - - /** - * @param string[] $configFiles - */ - private function buildCachedContainer(array $configFiles): ContainerInterface - { - $hash = $this->createConfigsHash($configFiles); - - $cachedContainerBuilder = new CachedContainerBuilder(self::getCacheDir(), self::CACHE_KEY); - - return $cachedContainerBuilder->build( - $configFiles, - $hash, - fn (array $configFiles): ContainerBuilder => $this->buildContainer($configFiles) - ); - } - - private static function getCacheDir(): string - { - // we use the system temp dir only in our test-suite as we cannot reliably use it anywhere - // see https://github.com/rectorphp/rector/issues/7700 - return sys_get_temp_dir() . '/rector/'; - } - - private function isPrefixedBuild(): bool - { - return VersionResolver::PACKAGE_VERSION !== '@package_version@'; - } } diff --git a/tests/Validation/Collector/EmptyConfigurableRectorCollector/ConfigurableArrayMissingTest.php b/tests/Validation/Collector/EmptyConfigurableRectorCollector/ConfigurableArrayMissingTest.php index 8563abc0f39..4c7f17681ce 100644 --- a/tests/Validation/Collector/EmptyConfigurableRectorCollector/ConfigurableArrayMissingTest.php +++ b/tests/Validation/Collector/EmptyConfigurableRectorCollector/ConfigurableArrayMissingTest.php @@ -18,7 +18,7 @@ final class ConfigurableArrayMissingTest extends AbstractTestCase protected function setUp(): void { $rectorKernel = new RectorKernel(); - $containerBuilder = $rectorKernel->createBuilder([__DIR__ . '/config/configurable_array_missing.php']); + $containerBuilder = $rectorKernel->createFromConfigs([__DIR__ . '/config/configurable_array_missing.php']); $this->emptyConfigurableRectorCollector = $containerBuilder->get(EmptyConfigurableRectorCollector::class); } diff --git a/tests/Validation/Collector/EmptyConfigurableRectorCollector/EmptyConfigurableRectorCollectorTest.php b/tests/Validation/Collector/EmptyConfigurableRectorCollector/EmptyConfigurableRectorCollectorTest.php index ea567ecc75e..0a4d675dc80 100644 --- a/tests/Validation/Collector/EmptyConfigurableRectorCollector/EmptyConfigurableRectorCollectorTest.php +++ b/tests/Validation/Collector/EmptyConfigurableRectorCollector/EmptyConfigurableRectorCollectorTest.php @@ -15,7 +15,7 @@ final class EmptyConfigurableRectorCollectorTest extends AbstractTestCase protected function setUp(): void { $rectorKernel = new RectorKernel(); - $containerBuilder = $rectorKernel->createBuilder([__DIR__ . '/config/configurable_array_has_values.php']); + $containerBuilder = $rectorKernel->createFromConfigs([__DIR__ . '/config/configurable_array_has_values.php']); $this->emptyConfigurableRectorCollector = $containerBuilder->get(EmptyConfigurableRectorCollector::class); } diff --git a/tests/Validation/Collector/EmptyConfigurableRectorCollector/EmptyConfigureTest.php b/tests/Validation/Collector/EmptyConfigurableRectorCollector/EmptyConfigureTest.php index 5b630656bea..4863767dc2e 100644 --- a/tests/Validation/Collector/EmptyConfigurableRectorCollector/EmptyConfigureTest.php +++ b/tests/Validation/Collector/EmptyConfigurableRectorCollector/EmptyConfigureTest.php @@ -15,7 +15,7 @@ final class EmptyConfigureTest extends AbstractTestCase protected function setUp(): void { $rectorKernel = new RectorKernel(); - $containerBuilder = $rectorKernel->createBuilder([__DIR__ . '/config/empty_configure.php']); + $containerBuilder = $rectorKernel->createFromConfigs([__DIR__ . '/config/empty_configure.php']); $this->emptyConfigurableRectorCollector = $containerBuilder->get(EmptyConfigurableRectorCollector::class); }