2021-11-01 14:32:25 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\Core\Kernel;
|
2021-11-01 14:32:25 +00:00
|
|
|
|
2023-05-16 04:29:24 +00:00
|
|
|
use Rector\Core\Application\VersionResolver;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\Core\Exception\ShouldNotHappenException;
|
2023-05-15 15:58:52 +00:00
|
|
|
use Rector\Core\Util\FileHasher;
|
|
|
|
use Rector\Testing\PHPUnit\StaticPHPUnitEnvironment;
|
2023-06-01 08:56:46 +00:00
|
|
|
use RectorPrefix202306\Symfony\Component\DependencyInjection\ContainerBuilder;
|
|
|
|
use RectorPrefix202306\Symfony\Component\DependencyInjection\ContainerInterface;
|
|
|
|
use RectorPrefix202306\Webmozart\Assert\Assert;
|
2022-08-31 11:18:37 +00:00
|
|
|
final class RectorKernel
|
2021-11-01 14:32:25 +00:00
|
|
|
{
|
|
|
|
/**
|
2023-05-15 15:58:52 +00:00
|
|
|
* @var string
|
2021-11-01 14:32:25 +00:00
|
|
|
*/
|
2023-06-05 09:55:14 +00:00
|
|
|
private const CACHE_KEY = 'v55';
|
2021-11-01 14:32:25 +00:00
|
|
|
/**
|
|
|
|
* @var \Symfony\Component\DependencyInjection\ContainerInterface|null
|
|
|
|
*/
|
|
|
|
private $container = null;
|
2023-05-15 15:58:52 +00:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private $dumpFileCache = \false;
|
|
|
|
/**
|
|
|
|
* @var string|null
|
|
|
|
*/
|
|
|
|
private static $defaultFilesHash;
|
2021-11-01 14:32:25 +00:00
|
|
|
public function __construct()
|
|
|
|
{
|
2023-05-15 15:58:52 +00:00
|
|
|
// while running tests we use different DI containers a lot,
|
2023-05-16 04:29:24 +00:00
|
|
|
// therefore make sure we don't compile them over and over again on rector-src.
|
|
|
|
if (!StaticPHPUnitEnvironment::isPHPUnitRun()) {
|
|
|
|
return;
|
2023-05-15 15:58:52 +00:00
|
|
|
}
|
2023-05-16 10:30:50 +00:00
|
|
|
if ($this->isPrefixedBuild()) {
|
2023-05-16 04:29:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
$this->dumpFileCache = \true;
|
2021-11-01 14:32:25 +00:00
|
|
|
}
|
2022-12-23 17:10:25 +00:00
|
|
|
/**
|
2023-05-15 15:58:52 +00:00
|
|
|
* @param string[] $configFiles
|
2022-12-23 17:10:25 +00:00
|
|
|
* @api used in tests
|
|
|
|
*/
|
2023-05-15 15:58:52 +00:00
|
|
|
public function createBuilder(array $configFiles = []) : ContainerBuilder
|
2022-08-31 11:18:37 +00:00
|
|
|
{
|
2023-05-15 15:58:52 +00:00
|
|
|
return $this->buildContainer($configFiles);
|
2022-08-31 11:18:37 +00:00
|
|
|
}
|
2021-11-01 14:32:25 +00:00
|
|
|
/**
|
|
|
|
* @param string[] $configFiles
|
2023-05-15 15:58:52 +00:00
|
|
|
* @api used in tests
|
2021-11-01 14:32:25 +00:00
|
|
|
*/
|
2023-05-15 15:58:52 +00:00
|
|
|
public function createFromConfigs(array $configFiles) : ContainerInterface
|
2021-11-01 14:32:25 +00:00
|
|
|
{
|
2023-05-15 15:58:52 +00:00
|
|
|
if ($configFiles === []) {
|
|
|
|
return $this->buildContainer([]);
|
|
|
|
}
|
2023-05-15 17:50:04 +00:00
|
|
|
$container = $this->dumpFileCache ? $this->buildCachedContainer($configFiles) : $this->buildContainer($configFiles);
|
2023-05-15 15:58:52 +00:00
|
|
|
return $this->container = $container;
|
2021-11-01 14:32:25 +00:00
|
|
|
}
|
2022-12-23 17:10:25 +00:00
|
|
|
/**
|
|
|
|
* @api used in tests
|
|
|
|
*/
|
2022-08-31 11:18:37 +00:00
|
|
|
public function getContainer() : ContainerInterface
|
2021-11-01 14:32:25 +00:00
|
|
|
{
|
2023-03-23 23:21:34 +00:00
|
|
|
if (!$this->container instanceof ContainerInterface) {
|
2022-06-07 08:22:29 +00:00
|
|
|
throw new ShouldNotHappenException();
|
2021-11-01 14:32:25 +00:00
|
|
|
}
|
|
|
|
return $this->container;
|
|
|
|
}
|
2023-05-15 15:58:52 +00:00
|
|
|
public static function clearCache() : void
|
2021-11-01 14:32:25 +00:00
|
|
|
{
|
2023-05-15 15:58:52 +00:00
|
|
|
$cachedContainerBuilder = new \Rector\Core\Kernel\CachedContainerBuilder(self::getCacheDir(), self::CACHE_KEY);
|
|
|
|
$cachedContainerBuilder->clearCache();
|
2021-11-01 14:32:25 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
private function createDefaultConfigFiles() : array
|
|
|
|
{
|
2022-09-01 20:56:47 +00:00
|
|
|
return [__DIR__ . '/../../config/config.php'];
|
2021-11-01 14:32:25 +00:00
|
|
|
}
|
2023-05-15 15:58:52 +00:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
private function buildContainer(array $configFiles) : ContainerBuilder
|
|
|
|
{
|
|
|
|
$defaultConfigFiles = $this->createDefaultConfigFiles();
|
|
|
|
$configFiles = \array_merge($defaultConfigFiles, $configFiles);
|
|
|
|
$containerBuilderBuilder = new \Rector\Core\Kernel\ContainerBuilderBuilder();
|
|
|
|
return $this->container = $containerBuilderBuilder->build($configFiles);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param string[] $configFiles
|
|
|
|
*/
|
|
|
|
private function buildCachedContainer(array $configFiles) : ContainerInterface
|
|
|
|
{
|
|
|
|
$hash = $this->createConfigsHash($configFiles);
|
|
|
|
$cachedContainerBuilder = new \Rector\Core\Kernel\CachedContainerBuilder(self::getCacheDir(), self::CACHE_KEY);
|
|
|
|
return $cachedContainerBuilder->build($configFiles, $hash, function (array $configFiles) : ContainerBuilder {
|
|
|
|
return $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/';
|
|
|
|
}
|
2023-05-16 10:30:50 +00:00
|
|
|
private function isPrefixedBuild() : bool
|
|
|
|
{
|
|
|
|
return VersionResolver::PACKAGE_VERSION !== '@package_version@';
|
|
|
|
}
|
2021-11-01 14:32:25 +00:00
|
|
|
}
|