Skip to content

Commit

Permalink
Remove CachedContainerBuilder (#4211)
Browse files Browse the repository at this point in the history
* Remove RectorKernel cache

* Remove RectorKernel cache

* remove unused class

* fix phpstan

* clean up

* Fix phpstan

* clean up phpstan config
  • Loading branch information
samsonasik committed Jun 13, 2023
1 parent d295fe5 commit 91cf7cc
Show file tree
Hide file tree
Showing 12 changed files with 9 additions and 276 deletions.
7 changes: 0 additions & 7 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
11 changes: 0 additions & 11 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -634,17 +634,6 @@ parameters:
# resolve continually
- '#Property Rector\\Core\\Contract\\PhpParser\\Node\\StmtsAwareInterface\:\:\$stmts \(array<PhpParser\\Node\\Stmt>\|null\) does not accept array<PhpParser\\Node\\Stmt\|null>#'


# 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#'
Expand Down
2 changes: 0 additions & 2 deletions src/Console/Command/ProcessCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}

Expand Down
11 changes: 0 additions & 11 deletions src/Exception/Cache/StaleContainerCacheException.php

This file was deleted.

70 changes: 0 additions & 70 deletions src/Kernel/CacheInvalidatingContainer.php

This file was deleted.

78 changes: 0 additions & 78 deletions src/Kernel/CachedContainerBuilder.php

This file was deleted.

96 changes: 4 additions & 92 deletions src/Kernel/RectorKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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[]
*/
Expand All @@ -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
*/
Expand All @@ -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@';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit 91cf7cc

Please sign in to comment.