Skip to content

Commit 832371b

Browse files
staabmondrejmirtes
authored andcommitted
Cache neon file reads
1 parent 9764ce8 commit 832371b

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

src/DependencyInjection/ContainerFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ private function detectDuplicateIncludedFiles(
227227
array $loaderParameters,
228228
): array
229229
{
230-
$neonAdapter = new NeonAdapter([]);
230+
$neonAdapter = new NeonCachedFileReader([]);
231231
$phpAdapter = new PhpAdapter();
232232
$allConfigFiles = [];
233233
$configArray = [];
@@ -257,7 +257,7 @@ private function detectDuplicateIncludedFiles(
257257
*/
258258
private static function getConfigFiles(
259259
FileHelper $fileHelper,
260-
NeonAdapter $neonAdapter,
260+
NeonCachedFileReader $neonAdapter,
261261
PhpAdapter $phpAdapter,
262262
string $configFile,
263263
array $loaderParameters,

src/DependencyInjection/LoaderFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function __construct(
2424

2525
public function createLoader(): Loader
2626
{
27-
$neonAdapter = new NeonAdapter($this->expandRelativePaths);
27+
$neonAdapter = new NeonCachedFileReader($this->expandRelativePaths);
2828

2929
$loader = new NeonLoader($this->fileHelper, $this->generateBaselineFile);
3030
$loader->addAdapter('dist', $neonAdapter);
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\DependencyInjection;
4+
5+
use Nette\DI\Config\Adapter;
6+
use Nette\Neon\Exception;
7+
use Nette\Neon\Neon;
8+
use Override;
9+
use PHPStan\File\FileReader;
10+
use function sprintf;
11+
12+
final class NeonCachedFileReader implements Adapter
13+
{
14+
15+
private NeonAdapter $adapter;
16+
17+
/** @var array<string, mixed[]> */
18+
private static array $decodedCache = [];
19+
20+
/**
21+
* @param list<string> $expandRelativePaths
22+
*/
23+
public function __construct(private array $expandRelativePaths)
24+
{
25+
$this->adapter = new NeonAdapter($this->expandRelativePaths);
26+
}
27+
28+
/**
29+
* @return mixed[]
30+
*/
31+
#[Override]
32+
public function load(string $file): array
33+
{
34+
try {
35+
if (isset(self::$decodedCache[$file])) {
36+
$neon = self::$decodedCache[$file];
37+
} else {
38+
$contents = FileReader::read($file);
39+
$neon = (array) Neon::decode($contents);
40+
self::$decodedCache[$file] = $neon;
41+
}
42+
43+
return $this->adapter->process($neon, '', $file);
44+
} catch (Exception $e) {
45+
throw new Exception(sprintf('Error while loading %s: %s', $file, $e->getMessage()));
46+
}
47+
}
48+
49+
}

0 commit comments

Comments
 (0)