forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNeonCachedFileReader.php
More file actions
49 lines (40 loc) · 1.05 KB
/
Copy pathNeonCachedFileReader.php
File metadata and controls
49 lines (40 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php declare(strict_types = 1);
namespace PHPStan\DependencyInjection;
use Nette\DI\Config\Adapter;
use Nette\Neon\Exception;
use Nette\Neon\Neon;
use Override;
use PHPStan\File\FileReader;
use function sprintf;
final class NeonCachedFileReader implements Adapter
{
private NeonAdapter $adapter;
/** @var array<string, mixed[]> */
private static array $decodedCache = [];
/**
* @param list<string> $expandRelativePaths
*/
public function __construct(private array $expandRelativePaths)
{
$this->adapter = new NeonAdapter($this->expandRelativePaths);
}
/**
* @return mixed[]
*/
#[Override]
public function load(string $file): array
{
try {
if (isset(self::$decodedCache[$file])) {
$neon = self::$decodedCache[$file];
} else {
$contents = FileReader::read($file);
$neon = (array) Neon::decode($contents);
self::$decodedCache[$file] = $neon;
}
return $this->adapter->process($neon, '', $file);
} catch (Exception $e) {
throw new Exception(sprintf('Error while loading %s: %s', $file, $e->getMessage()));
}
}
}