Skip to content

Commit

Permalink
Generate files for multiple versions
Browse files Browse the repository at this point in the history
  • Loading branch information
shish committed Dec 5, 2024
1 parent 61a871c commit 386e365
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 18 deletions.
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"generated/ftp.php",
"generated/funchand.php",
"generated/gettext.php",
"generated/gmp.php",
"generated/gnupg.php",
"generated/hash.php",
"generated/ibase.php",
Expand All @@ -60,6 +61,7 @@
"generated/mbstring.php",
"generated/misc.php",
"generated/mysql.php",
"generated/mysqli.php",
"generated/network.php",
"generated/oci8.php",
"generated/opcache.php",
Expand All @@ -72,7 +74,6 @@
"generated/ps.php",
"generated/pspell.php",
"generated/readline.php",
"generated/rnp.php",
"generated/rpminfo.php",
"generated/rrd.php",
"generated/sem.php",
Expand All @@ -98,7 +99,8 @@
"generated/yaml.php",
"generated/yaz.php",
"generated/zip.php",
"generated/zlib.php"
"generated/zlib.php",
"generated/rnp.php"
]
},
"require": {
Expand Down
24 changes: 24 additions & 0 deletions generator/src/FileCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public function generatePhpFile(array $functions, string $path): void

foreach ($phpFunctionsByModule as $module => $phpFunctions) {
$lcModule = \lcfirst($module);
if (!is_dir($path)) {
\mkdir($path);
}
$stream = \fopen($path.$lcModule.'.php', 'w');
if ($stream === false) {
throw new \RuntimeException('Unable to write to '.$path);
Expand All @@ -43,6 +46,27 @@ public function generatePhpFile(array $functions, string $path): void
}
}

/**
* @param string[] $versions
*/
public function generateVersionSplitters(string $module, string $path, array $versions): void
{
$lcModule = \lcfirst($module);
$stream = \fopen($path.$lcModule.'.php', 'w');
if ($stream === false) {
throw new \RuntimeException('Unable to write to '.$path);
}
\fwrite($stream, "<?php\n");
foreach ($versions as $version) {
if (file_exists("$path/$version/$lcModule.php")) {
\fwrite($stream, "\nif(strpos(PHP_VERSION, \"$version.\") === 0) {");
\fwrite($stream, "\n require_once __DIR__ . '/$version/$lcModule.php';");
\fwrite($stream, "\n}");
}
}
\fclose($stream);
}

/**
* @param Method[] $functions
* @return string[]
Expand Down
45 changes: 29 additions & 16 deletions generator/src/GenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,36 +25,49 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->rmGenerated();
// Let's build the DTD necessary to load the XML files.
DocPage::buildEntities();
$scanner = new Scanner(__DIR__ . '/../doc/doc-en/en/reference/');

$paths = $scanner->getFunctionsPaths();
$versions = [
"8.1" => "9097ea48f608dbbbf795235a31af82b85bd94430",
"8.2" => "8f4e8cf3de08208e71eb0117f1c970c27e9120c9",
"8.3" => "7453a50321f0834421cebea8edade14deef5466b",
"8.4" => "d553fa36940639b0889ec4358fa3bbb92f123b69",

This comment has been minimized.

Copy link
@shish

shish Dec 5, 2024

Author Collaborator

The PHP docs are a living document which is constantly updated to reflect the nightly builds, there's no branches or tags to reflect specific versions - so in this case I've chosen some commits based on the day of release of each version/

"8.5" => "master",
];
$modules = [];

$res = $scanner->getMethods($paths);
$functions = $res->methods;
$overloadedFunctions = $res->overloadedFunctions;
foreach ($versions as $version => $commit) {
$process = new Process(['git', "checkout", $commit], __DIR__ . '/../doc/doc-en/en/reference/');
$process->run();
$scanner = new Scanner(__DIR__ . '/../doc/doc-en/en/reference/');

$output->writeln('These functions have been ignored and must be dealt with manually: '.\implode(', ', $overloadedFunctions));
$paths = $scanner->getFunctionsPaths();

$fileCreator = new FileCreator();
$fileCreator->generatePhpFile($functions, __DIR__ . '/../../generated/');
$fileCreator->generateFunctionsList($functions, __DIR__ . '/../../generated/functionsList.php');
$fileCreator->generateRectorFile($functions, __DIR__ . '/../../rector-migrate.php');
$res = $scanner->getMethods($paths);
$functions = $res->methods;
$overloadedFunctions = $res->overloadedFunctions;

$output->writeln('These functions have been ignored and must be dealt with manually: '.\implode(', ', $overloadedFunctions));

$modules = [];
foreach ($functions as $function) {
$moduleName = $function->getModuleName();
$modules[$moduleName] = $moduleName;
$fileCreator = new FileCreator();
$fileCreator->generatePhpFile($functions, __DIR__ . "/../../generated/$version/");
$fileCreator->generateFunctionsList($functions, __DIR__ . "/../../generated/$version/functionsList.php");
$fileCreator->generateRectorFile($functions, __DIR__ . "/../../generated/$version/rector-migrate.php");

foreach ($functions as $function) {
$moduleName = $function->getModuleName();
$modules[$moduleName] = $moduleName;
}
}

foreach ($modules as $moduleName => $foo) {
$fileCreator->generateVersionSplitters($moduleName, __DIR__ . "/../../generated/", array_keys($versions));
$fileCreator->createExceptionFile((string) $moduleName);
}

$this->runCsFix($output);

// Let's require the generated file to check there is no error.
$files = \glob(__DIR__.'/../../generated/*.php');
$files = \glob(__DIR__."/../../generated/8.1/*.php");
if ($files === false) {
throw new \RuntimeException('Failed to require the generated file');
}
Expand Down Expand Up @@ -91,7 +104,7 @@ private function rmGenerated(): void
\unlink($exception);
}

$files = \glob(__DIR__.'/../../generated/*.php');
$files = \glob(__DIR__.'/../../generated/*/*.php');
if ($files === false) {
throw new \RuntimeException('Failed to require the generated files');
}
Expand Down

0 comments on commit 386e365

Please sign in to comment.