没有不值得去解决的问题,也没有不值得去学习的技术!

Adjusting Semantic Version Comparison (v2.0.0 < v2.0.0-h.1)

The sorted result is as follows, meeting expectations

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.

PHP


var_dump(version_compare('v2.0.0', 'v2.0.0-h.1', '<'));


3. The existing code implementation is as follows

PHP

$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

Plaintext

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

Plaintext

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

PHP

$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

The sorted result is as follows, meeting expectations
Figure 1
Plaintext

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
)


关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系