From 07db98d9ffd8bfeece4fab6765fa1e9c6828a526 Mon Sep 17 00:00:00 2001 From: Jake Hotson Date: Thu, 29 Jan 2026 11:31:18 +0000 Subject: [PATCH] Generate splitter with fallback This will allow for non-generated files. E.g. if the version for PHP 8.4 is not generated (because it is the same as the 8.3 version), it will fall back to the version for PHP 8.3, and so on. Part of #706. --- generator/src/Generator/FileCreator.php | 45 ++++++++++++++++++++----- 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/generator/src/Generator/FileCreator.php b/generator/src/Generator/FileCreator.php index 47935d0a..7a79870b 100644 --- a/generator/src/Generator/FileCreator.php +++ b/generator/src/Generator/FileCreator.php @@ -63,20 +63,47 @@ public function generatePhpFile( public function generateVersionSplitters(string $module, string $path, array $versions, bool $return = false): void { $lcModule = \lcfirst($module); + $availableVersions = \array_filter( + $versions, + static function (string $version) use ($path, $lcModule): bool { + return \file_exists("$path/$version/$lcModule.php"); + } + ); + if ($availableVersions === []) { + throw new \RuntimeException('No versions found for '.$module); + } + $stream = \fopen($path.$lcModule.'.php', 'w'); if ($stream === false) { throw new \RuntimeException('Unable to write to '.$path); } + // Pick up the latest compatible version + \rsort($availableVersions); + $versionsArrayContent = \implode( + ', ', + \array_map( + function (string $version): string { + return '\'' . $version . '\''; + }, + $availableVersions + ) + ); $return = $return ? "return " : ""; - \fwrite($stream, "= 0) {' . "\n" . + ' ' . $return . 'require_once __DIR__ . \'/\' . $phpVersion . \'/' . $lcModule . '.php;\'' . "\n" . + ' break;' . "\n" . + ' }' . "\n" . + ' }' . "\n" . + ' }' . "\n" . + ');' . "\n" + ); + \fclose($stream); }