There is no problem not worth solving, and no technology not worth learning!

Adjustment of the size comparison of semantic version number (v2.0.0 < v2.0.0-h.1)

排序后的结果如下,符合预期

1. Reference:https://www.shuijingwanwq.com/2022/11/02/7121/. Now, due to the existence of some special version numbers, it does not meet the specification of semantic 2.0. The final requirements are as follows: v2.0.0 < v2.0.0-h.1

2. Implement v2.0.0 < v2.0.0-h.1, after executing version_compare, the result is: bool(false), that is, 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 is implemented 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 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 between 2 versions, if 1 version exists -h., 1 version does not exist -h., and the two are stripped – after the substring after the substring, the main version number (eg v2.0.15) is equal, then there is -h. the version is bigger. The code is implemented 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 results after sorting are as follows, in line with expectations. as shown in Figure 1

排序后的结果如下,符合预期
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
)



PHP / Laravel / Yii2 Legacy Project Maintenance & Long-Term Technical Support

If your PHP / Laravel / Yii2 project is already in production but needs bug fixing, API troubleshooting, performance optimization, developer handover support, or long-term maintenance, feel free to contact me for remote technical support.

Ideal For:
✅ PHP legacy systems without active maintenance
✅ Laravel / Yii2 project bug fixes
✅ Admin panel feature iterations
✅ RESTful API troubleshooting
✅ MySQL / Redis / Nginx performance issues
✅ Long-term remote part-time maintenance

We can start with a small task:
✅ Production error troubleshooting
✅ API issue analysis
✅ Slow query and performance bottleneck diagnosis
✅ Initial code structure review
✅ Deployment environment and log inspection

If you would like to discuss your project, please contact me and mention: PHP Maintenance Consultation.

Contact Me:
Telegram: @shuijingwan
WeChat: 13980074657
Email: shuijingwanwq@gmail.com

评论

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.