1. Reference: In PHP 7.4, execute the script files in directories one by one from low to high based on semantic versioning directory names. Now, because there are some special version numbers that do not conform to the Semantic Versioning 2.0 specification, the final requirement is as follows: v2.0.0 < v2.0.0-h.1
2. To implement v2.0.0 < v2.0.0-h.1, after executing version_compare, the result is: bool(false), meaning v2.0.0 is not less than v2.0.0-h.1.
var_dump(version_compare('v2.0.0', 'v2.0.0-h.1', '<'));
3. The existing code implementation is as follows
$directories = glob(module_path('ThemeStoreDB') . '/Resources/setting_migrations' . '/*', GLOB_ONLYDIR);
$versions = array_map(fn($directory): string => pathinfo($directory)['basename'], $directories);
$availableVersions = array_filter($versions, function ($v) use ($originalThemeVersionSemantic, $themeVersionSemantic) {
return version_compare($v, $originalThemeVersionSemantic, '>') && version_compare($v, $themeVersionSemantic, '<=');
});
usort($availableVersions, 'version_compare');
return $availableVersions;
4. The final result obtained is as follows
Array
(
[0] => v2.0.15-h.6
[1] => v2.0.15-rc.7
[2] => v2.0.15
[3] => v2.0.16-h.2
[4] => v2.0.16-h.3
[5] => v2.0.16
[6] => v2.0.17-h.0
[7] => v2.0.20-h.1
[8] => v2.0.20
)
5. The expected result is as follows
Array
(
[0] => v2.0.15-rc.7
[1] => v2.0.15
[2] => v2.0.15-h.6
[3] => v2.0.16-h.2
[4] => v2.0.16-h.3
[5] => v2.0.16
[6] => v2.0.17-h.0
[7] => v2.0.20
[8] => v2.0.20-h.1
)
6. When comparing two versions, if one version contains -h. and the other does not, and the main version numbers (e.g., v2.0.15) are equal after stripping the substrings following the -, then the version containing -h. is greater. The code implementation is as follows
$directories = glob(module_path('ThemeStoreDB') . '/Resources/setting_migrations' . '/*', GLOB_ONLYDIR);
$versions = array_map(fn($directory): string => pathinfo($directory)['basename'], $directories);
$specialVersionIdentifier = '-h.'; // 特殊版本标识
$version_compare = function ($v1, $v2) use ($specialVersionIdentifier) {
if (Str::contains($v1, $specialVersionIdentifier) && !Str::contains($v2, $specialVersionIdentifier) && Str::before($v1, '-') == Str::before($v2, '-')) {
return 1;
} elseif (!Str::contains($v1, $specialVersionIdentifier) && Str::contains($v2, $specialVersionIdentifier) && Str::before($v1, '-') == Str::before($v2, '-')) {
return -1;
} else {
return version_compare($v1, $v2);
}
};
usort($versions, $version_compare);
print_r($versions);
exit;
7. The sorted result is as follows, meeting expectations. See Figure 1

Array
(
[0] => v2.0.15-rc.7
[1] => v2.0.15
[2] => v2.0.15-h.6
[3] => v2.0.16
[4] => v2.0.16-h.2
[5] => v2.0.16-h.3
[6] => v2.0.17-h.0
[7] => v2.0.20
[8] => v2.0.20-h.1
)
