Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/DependencyInjection/ContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ private function detectDuplicateIncludedFiles(
array $loaderParameters,
): array
{
$neonAdapter = new NeonAdapter([]);
$neonAdapter = new NeonCachedFileReader([]);
$phpAdapter = new PhpAdapter();
$allConfigFiles = [];
$configArray = [];
Expand Down Expand Up @@ -257,7 +257,7 @@ private function detectDuplicateIncludedFiles(
*/
private static function getConfigFiles(
FileHelper $fileHelper,
NeonAdapter $neonAdapter,
NeonCachedFileReader $neonAdapter,
PhpAdapter $phpAdapter,
string $configFile,
array $loaderParameters,
Expand Down
2 changes: 1 addition & 1 deletion src/DependencyInjection/LoaderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function __construct(

public function createLoader(): Loader
{
$neonAdapter = new NeonAdapter($this->expandRelativePaths);
$neonAdapter = new NeonCachedFileReader($this->expandRelativePaths);

$loader = new NeonLoader($this->fileHelper, $this->generateBaselineFile);
$loader->addAdapter('dist', $neonAdapter);
Expand Down
49 changes: 49 additions & 0 deletions src/DependencyInjection/NeonCachedFileReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,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()));
}
}

}
Loading