From b4aa8b8d952cd7da03d7690da010815f8c44922a Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sun, 21 Dec 2025 09:46:28 +0100 Subject: [PATCH] Cache neon file reads --- src/DependencyInjection/ContainerFactory.php | 4 +- src/DependencyInjection/LoaderFactory.php | 2 +- .../NeonCachedFileReader.php | 49 +++++++++++++++++++ 3 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 src/DependencyInjection/NeonCachedFileReader.php diff --git a/src/DependencyInjection/ContainerFactory.php b/src/DependencyInjection/ContainerFactory.php index 379b1fb741..7f056c2c40 100644 --- a/src/DependencyInjection/ContainerFactory.php +++ b/src/DependencyInjection/ContainerFactory.php @@ -227,7 +227,7 @@ private function detectDuplicateIncludedFiles( array $loaderParameters, ): array { - $neonAdapter = new NeonAdapter([]); + $neonAdapter = new NeonCachedFileReader([]); $phpAdapter = new PhpAdapter(); $allConfigFiles = []; $configArray = []; @@ -257,7 +257,7 @@ private function detectDuplicateIncludedFiles( */ private static function getConfigFiles( FileHelper $fileHelper, - NeonAdapter $neonAdapter, + NeonCachedFileReader $neonAdapter, PhpAdapter $phpAdapter, string $configFile, array $loaderParameters, diff --git a/src/DependencyInjection/LoaderFactory.php b/src/DependencyInjection/LoaderFactory.php index 088e678cdb..de6ac821e2 100644 --- a/src/DependencyInjection/LoaderFactory.php +++ b/src/DependencyInjection/LoaderFactory.php @@ -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); diff --git a/src/DependencyInjection/NeonCachedFileReader.php b/src/DependencyInjection/NeonCachedFileReader.php new file mode 100644 index 0000000000..a9ca8e1ed6 --- /dev/null +++ b/src/DependencyInjection/NeonCachedFileReader.php @@ -0,0 +1,49 @@ + */ + private static array $decodedCache = []; + + /** + * @param list $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())); + } + } + +}