Skip to content

Commit

Permalink
Invalidate DI container based on included files hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Oct 20, 2022
1 parent f15cd6d commit 615c6a1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
28 changes: 27 additions & 1 deletion src/DependencyInjection/Configurator.php
Expand Up @@ -4,13 +4,18 @@

use Nette\DI\Config\Loader;
use Nette\DI\ContainerLoader;
use PHPStan\File\FileReader;
use function array_keys;
use function sha1;
use const PHP_RELEASE_VERSION;
use const PHP_VERSION_ID;

class Configurator extends \Nette\Bootstrap\Configurator
{

/** @var string[] */
private array $allConfigFiles = [];

public function __construct(private LoaderFactory $loaderFactory)
{
parent::__construct();
Expand All @@ -21,6 +26,14 @@ protected function createLoader(): Loader
return $this->loaderFactory->createLoader();
}

/**
* @param string[] $allConfigFiles
*/
public function setAllConfigFiles(array $allConfigFiles): void
{
$this->allConfigFiles = $allConfigFiles;
}

/**
* @return mixed[]
*/
Expand All @@ -43,8 +56,21 @@ public function loadContainer(): string

return $loader->load(
[$this, 'generateContainer'],
[$this->staticParameters, array_keys($this->dynamicParameters), $this->configs, PHP_VERSION_ID - PHP_RELEASE_VERSION, NeonAdapter::CACHE_KEY],
[$this->staticParameters, array_keys($this->dynamicParameters), $this->configs, PHP_VERSION_ID - PHP_RELEASE_VERSION, NeonAdapter::CACHE_KEY, $this->getAllConfigFilesHashes()],
);
}

/**
* @return string[]
*/
private function getAllConfigFilesHashes(): array
{
$hashes = [];
foreach ($this->allConfigFiles as $file) {
$hashes[$file] = sha1(FileReader::read($file));
}

return $hashes;
}

}
9 changes: 6 additions & 3 deletions src/DependencyInjection/ContainerFactory.php
Expand Up @@ -84,7 +84,7 @@ public function create(
?string $singleReflectionInsteadOfFile = null,
): Container
{
$this->detectDuplicateIncludedFiles(
$allConfigFiles = $this->detectDuplicateIncludedFiles(
$additionalConfigFiles,
[
'rootDir' => $this->rootDirectory,
Expand Down Expand Up @@ -127,6 +127,8 @@ public function create(
$configurator->addConfig($additionalConfigFile);
}

$configurator->setAllConfigFiles($allConfigFiles);

$container = $configurator->createContainer();

/** @var SourceLocator $sourceLocator */
Expand Down Expand Up @@ -211,12 +213,13 @@ public function getConfigDirectory(): string
/**
* @param string[] $configFiles
* @param array<string, string> $loaderParameters
* @return string[]
* @throws DuplicateIncludedFilesException
*/
private function detectDuplicateIncludedFiles(
array $configFiles,
array $loaderParameters,
): void
): array
{
$neonAdapter = new NeonAdapter();
$phpAdapter = new PhpAdapter();
Expand All @@ -229,7 +232,7 @@ private function detectDuplicateIncludedFiles(

$deduplicated = array_unique($normalized);
if (count($normalized) <= count($deduplicated)) {
return;
return $normalized;
}

$duplicateFiles = array_unique(array_diff_key($normalized, $deduplicated));
Expand Down

0 comments on commit 615c6a1

Please sign in to comment.