rector/packages/caching/src/Config/FileHashComputer.php

74 lines
2.4 KiB
PHP
Raw Normal View History

2020-04-01 01:55:44 +00:00
<?php
declare(strict_types=1);
namespace Rector\Caching\Config;
use Rector\Core\Exception\ShouldNotHappenException;
use Symfony\Component\Config\FileLocator;
2020-07-16 18:59:54 +00:00
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\Config\Loader\LoaderResolver;
2020-04-01 01:55:44 +00:00
use Symfony\Component\DependencyInjection\ContainerBuilder;
2020-07-16 18:59:54 +00:00
use Symfony\Component\DependencyInjection\Loader\GlobFileLoader;
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
2020-04-01 01:55:44 +00:00
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
2020-07-21 11:42:17 +00:00
use Symplify\SmartFileSystem\SmartFileInfo;
2020-04-01 01:55:44 +00:00
/**
* Inspired by https://github.com/symplify/easy-coding-standard/blob/e598ab54686e416788f28fcfe007fd08e0f371d9/packages/changed-files-detector/src/FileHashComputer.php
* @see \Rector\Caching\Tests\Config\FileHashComputerTest
2020-04-01 01:55:44 +00:00
*/
final class FileHashComputer
{
2020-07-21 11:42:17 +00:00
public function compute(SmartFileInfo $fileInfo): string
2020-04-01 01:55:44 +00:00
{
2020-07-21 11:42:17 +00:00
$this->ensureIsYamlOrPhp($fileInfo);
2020-04-01 01:55:44 +00:00
$containerBuilder = new ContainerBuilder();
2020-07-21 11:42:17 +00:00
$fileLoader = $this->createFileLoader($fileInfo, $containerBuilder);
2020-04-01 01:55:44 +00:00
2020-07-21 11:42:17 +00:00
$fileLoader->load($fileInfo->getRealPath());
2020-04-01 01:55:44 +00:00
return $this->arrayToHash($containerBuilder->getDefinitions()) .
$this->arrayToHash($containerBuilder->getParameterBag()->all());
}
2020-07-21 11:42:17 +00:00
private function ensureIsYamlOrPhp(SmartFileInfo $fileInfo): void
2020-04-01 01:55:44 +00:00
{
2020-07-21 11:42:17 +00:00
if ($fileInfo->hasSuffixes(['yml', 'yaml', 'php'])) {
2020-04-01 01:55:44 +00:00
return;
}
throw new ShouldNotHappenException(sprintf(
2020-07-21 22:14:12 +00:00
// getRealPath() cannot be used, as it breaks in phar
'Provide only YAML/PHP file, ready for Symfony Dependency Injection. "%s" given', $fileInfo->getRelativeFilePath()
2020-04-01 01:55:44 +00:00
));
}
2020-07-21 11:42:17 +00:00
private function createFileLoader(SmartFileInfo $fileInfo, ContainerBuilder $containerBuilder): LoaderInterface
2020-07-16 18:59:54 +00:00
{
2020-07-21 11:42:17 +00:00
$fileLocator = new FileLocator([$fileInfo->getPath()]);
2020-07-16 18:59:54 +00:00
$loaderResolver = new LoaderResolver([
new GlobFileLoader($containerBuilder, $fileLocator),
new PhpFileLoader($containerBuilder, $fileLocator),
new YamlFileLoader($containerBuilder, $fileLocator),
]);
2020-07-21 11:42:17 +00:00
$loader = $loaderResolver->resolve($fileInfo->getRealPath());
2020-07-16 18:59:54 +00:00
if (! $loader) {
throw new ShouldNotHappenException();
}
return $loader;
}
/**
* @param mixed[] $array
*/
private function arrayToHash(array $array): string
{
return md5(serialize($array));
}
2020-04-01 01:55:44 +00:00
}