Skip to content

Commit ab53a99

Browse files
committed
cache files in OptimizedDirectorySourceLocatorFactory
1 parent 1a8a657 commit ab53a99

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

src/Reflection/BetterReflection/SourceLocator/OptimizedDirectorySourceLocatorFactory.php

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010
use PHPStan\Reflection\ConstantNameHelper;
1111
use function array_key_exists;
1212
use function count;
13+
use function implode;
1314
use function in_array;
1415
use function ltrim;
1516
use function php_strip_whitespace;
1617
use function preg_match_all;
1718
use function preg_replace;
19+
use function sha1;
1820
use function sha1_file;
1921
use function sprintf;
2022
use function strtolower;
@@ -52,6 +54,11 @@ public function createByDirectory(string $directory): OptimizedDirectorySourceLo
5254
}
5355

5456
$cacheKey = sprintf('odsl-%s', $directory);
57+
return $this->createCachedDirectorySourceLocator($fileHashes, $cacheKey);
58+
}
59+
60+
public function createCachedDirectorySourceLocator(array $fileHashes, string $cacheKey): OptimizedDirectorySourceLocator
61+
{
5562
$variableCacheKey = 'v1';
5663

5764
/** @var array<string, array{string, string[], string[], string[]}>|null $cached */
@@ -96,20 +103,17 @@ public function createByDirectory(string $directory): OptimizedDirectorySourceLo
96103
*/
97104
public function createByFiles(array $files): OptimizedDirectorySourceLocator
98105
{
99-
$symbols = [];
106+
$fileHashes = [];
100107
foreach ($files as $file) {
101-
[$newClasses, $newFunctions, $newConstants] = $this->findSymbols($file);
102-
$symbols[$file] = ['', $newClasses, $newFunctions, $newConstants];
108+
$hash = sha1_file($file);
109+
if ($hash === false) {
110+
continue;
111+
}
112+
$fileHashes[$file] = $hash;
103113
}
104114

105-
[$classToFile, $functionToFiles, $constantToFile] = $this->changeStructure($symbols);
106-
107-
return new OptimizedDirectorySourceLocator(
108-
$this->fileNodesFetcher,
109-
$classToFile,
110-
$functionToFiles,
111-
$constantToFile,
112-
);
115+
$cacheKey = sprintf('odsl-files-%s', sha1(implode(',', $files)));
116+
return $this->createCachedDirectorySourceLocator($fileHashes, $cacheKey);
113117
}
114118

115119
/**

0 commit comments

Comments
 (0)