-
-
Notifications
You must be signed in to change notification settings - Fork 338
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dump dependency container and re-use it - 35% faster test-suite (#3809)
- Loading branch information
Showing
12 changed files
with
271 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Core\Kernel; | ||
|
||
use Rector\Core\Exception\ShouldNotHappenException; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
use Symfony\Component\DependencyInjection\Dumper\PhpDumper; | ||
use Symplify\SmartFileSystem\SmartFileSystem; | ||
|
||
/** | ||
* see https://symfony.com/doc/current/components/dependency_injection/compilation.html#dumping-the-configuration-for-performance | ||
*/ | ||
final class CachedContainerBuilder | ||
{ | ||
public function __construct( | ||
private readonly string $cacheDir, | ||
private readonly string $cacheKey, | ||
) { | ||
if (! str_ends_with($cacheDir, '/')) { | ||
throw new ShouldNotHappenException(sprintf('Cache dir "%s" must end with "/"', $cacheDir)); | ||
} | ||
} | ||
|
||
/** | ||
* @param string[] $configFiles | ||
* @param callable(string[] $configFiles):ContainerBuilder $containerBuilderCallback | ||
*/ | ||
public function build(array $configFiles, string $hash, callable $containerBuilderCallback): ContainerInterface | ||
{ | ||
$smartFileSystem = new SmartFileSystem(); | ||
$className = 'RectorKernel' . $hash; | ||
$file = $this->cacheDir . 'kernel-' . $this->cacheKey . '-' . $hash . '.php'; | ||
|
||
if (file_exists($file)) { | ||
require_once $file; | ||
$className = '\\' . __NAMESPACE__ . '\\' . $className; | ||
$container = new $className(); | ||
if (! $container instanceof ContainerInterface) { | ||
throw new ShouldNotHappenException(); | ||
} | ||
} else { | ||
$container = ($containerBuilderCallback)($configFiles); | ||
|
||
$phpDumper = new PhpDumper($container); | ||
$dumpedContainer = $phpDumper->dump([ | ||
'class' => $className, | ||
'namespace' => __NAMESPACE__, | ||
]); | ||
if (! is_string($dumpedContainer)) { | ||
throw new ShouldNotHappenException(); | ||
} | ||
|
||
$smartFileSystem->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; | ||
} | ||
|
||
$smartFileSystem = new SmartFileSystem(); | ||
$smartFileSystem->remove($cacheFiles); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Core\Kernel; | ||
|
||
use Rector\Core\Config\Loader\ConfigureCallMergingLoaderFactory; | ||
use Rector\Core\DependencyInjection\Collector\ConfigureCallValuesCollector; | ||
use Rector\Core\DependencyInjection\CompilerPass\AutowireArrayParameterCompilerPass; | ||
use Rector\Core\DependencyInjection\CompilerPass\AutowireRectorCompilerPass; | ||
use Rector\Core\DependencyInjection\CompilerPass\MakeRectorsPublicCompilerPass; | ||
use Rector\Core\DependencyInjection\CompilerPass\MergeImportedRectorConfigureCallValuesCompilerPass; | ||
use Rector\Core\DependencyInjection\CompilerPass\RemoveSkippedRectorsCompilerPass; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
final class ContainerBuilderBuilder | ||
{ | ||
private readonly ConfigureCallValuesCollector $configureCallValuesCollector; | ||
|
||
public function __construct() | ||
{ | ||
$this->configureCallValuesCollector = new ConfigureCallValuesCollector(); | ||
} | ||
|
||
/** | ||
* @param string[] $configFiles | ||
*/ | ||
public function build(array $configFiles): ContainerBuilder | ||
{ | ||
$compilerPasses = $this->createCompilerPasses(); | ||
|
||
$configureCallMergingLoaderFactory = new ConfigureCallMergingLoaderFactory($this->configureCallValuesCollector); | ||
$containerBuilderFactory = new ContainerBuilderFactory($configureCallMergingLoaderFactory); | ||
|
||
$containerBuilder = $containerBuilderFactory->create($configFiles, $compilerPasses); | ||
|
||
// @see https://symfony.com/blog/new-in-symfony-4-4-dependency-injection-improvements-part-1 | ||
$containerBuilder->setParameter('container.dumper.inline_factories', true); | ||
// to fix reincluding files again | ||
$containerBuilder->setParameter('container.dumper.inline_class_loader', false); | ||
|
||
$containerBuilder->compile(); | ||
|
||
return $containerBuilder; | ||
} | ||
|
||
/** | ||
* @return CompilerPassInterface[] | ||
*/ | ||
private function createCompilerPasses(): array | ||
{ | ||
return [ | ||
|
||
// must run before AutowireArrayParameterCompilerPass, as the autowired array cannot contain removed services | ||
new RemoveSkippedRectorsCompilerPass(), | ||
|
||
// autowire Rectors by default (mainly for tests) | ||
new AutowireRectorCompilerPass(), | ||
new MakeRectorsPublicCompilerPass(), | ||
|
||
// add all merged arguments of Rector services | ||
new MergeImportedRectorConfigureCallValuesCompilerPass($this->configureCallValuesCollector), | ||
new AutowireArrayParameterCompilerPass(), | ||
]; | ||
} | ||
} |
Oops, something went wrong.