2020-04-01 01:55:44 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\Caching\Config;
|
2020-04-01 01:55:44 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\Core\Exception\ShouldNotHappenException;
|
2023-05-01 01:36:32 +00:00
|
|
|
use RectorPrefix202305\Symfony\Component\Config\FileLocator;
|
|
|
|
use RectorPrefix202305\Symfony\Component\Config\Loader\LoaderInterface;
|
|
|
|
use RectorPrefix202305\Symfony\Component\Config\Loader\LoaderResolver;
|
|
|
|
use RectorPrefix202305\Symfony\Component\DependencyInjection\ContainerBuilder;
|
|
|
|
use RectorPrefix202305\Symfony\Component\DependencyInjection\Loader\GlobFileLoader;
|
|
|
|
use RectorPrefix202305\Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
|
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
|
|
|
|
*/
|
|
|
|
final class FileHashComputer
|
|
|
|
{
|
2021-11-01 14:32:25 +00:00
|
|
|
public function compute(string $filePath) : string
|
2020-04-01 01:55:44 +00:00
|
|
|
{
|
2021-11-01 14:32:25 +00:00
|
|
|
$this->ensureIsPhp($filePath);
|
2022-06-07 08:22:29 +00:00
|
|
|
$containerBuilder = new ContainerBuilder();
|
2021-11-01 14:32:25 +00:00
|
|
|
$fileLoader = $this->createFileLoader($filePath, $containerBuilder);
|
|
|
|
$fileLoader->load($filePath);
|
2020-11-16 17:50:38 +00:00
|
|
|
$parameterBag = $containerBuilder->getParameterBag();
|
|
|
|
return $this->arrayToHash($containerBuilder->getDefinitions()) . $this->arrayToHash($parameterBag->all());
|
2020-04-01 01:55:44 +00:00
|
|
|
}
|
2021-11-01 14:32:25 +00:00
|
|
|
private function ensureIsPhp(string $filePath) : void
|
2020-04-01 01:55:44 +00:00
|
|
|
{
|
2021-11-01 14:32:25 +00:00
|
|
|
$fileExtension = \pathinfo($filePath, \PATHINFO_EXTENSION);
|
|
|
|
if ($fileExtension === 'php') {
|
2020-04-01 01:55:44 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
throw new ShouldNotHappenException(\sprintf(
|
2020-07-21 22:14:12 +00:00
|
|
|
// getRealPath() cannot be used, as it breaks in phar
|
2021-05-06 18:51:25 +00:00
|
|
|
'Provide only PHP file, ready for Symfony Dependency Injection. "%s" given',
|
2021-11-01 14:32:25 +00:00
|
|
|
$filePath
|
2020-04-01 01:55:44 +00:00
|
|
|
));
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function createFileLoader(string $filePath, ContainerBuilder $containerBuilder) : LoaderInterface
|
2020-07-16 18:59:54 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
$fileLocator = new FileLocator([$filePath]);
|
|
|
|
$fileLoaders = [new GlobFileLoader($containerBuilder, $fileLocator), new PhpFileLoader($containerBuilder, $fileLocator)];
|
|
|
|
$loaderResolver = new LoaderResolver($fileLoaders);
|
2021-11-01 14:32:25 +00:00
|
|
|
$loader = $loaderResolver->resolve($filePath);
|
2021-11-28 18:05:13 +00:00
|
|
|
if ($loader === \false) {
|
2022-06-07 08:22:29 +00:00
|
|
|
throw new ShouldNotHappenException();
|
2020-07-16 18:59:54 +00:00
|
|
|
}
|
|
|
|
return $loader;
|
|
|
|
}
|
2020-08-05 20:45:36 +00:00
|
|
|
/**
|
|
|
|
* @param mixed[] $array
|
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
private function arrayToHash(array $array) : string
|
2020-08-05 20:45:36 +00:00
|
|
|
{
|
2021-05-09 20:15:43 +00:00
|
|
|
$serializedArray = \serialize($array);
|
|
|
|
return \md5($serializedArray);
|
2020-08-05 20:45:36 +00:00
|
|
|
}
|
2020-04-01 01:55:44 +00:00
|
|
|
}
|