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); }