VersionUtility.php 6.09 KB
Newer Older
1
<?php
2
3
namespace T3o\TerFe2\Utility;

4
5
/*
 * This file is part of the TYPO3 CMS project.
6
 *
7
8
9
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
10
 *
11
12
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
15
 * The TYPO3 project - inspiring people to share!
 */
16

17
18
19
use T3o\TerFe2\Service\LTSVersionService;
use TYPO3\CMS\Core\Utility\GeneralUtility;

20
21
22
/**
 * Utilities to manage versions
 */
23
class VersionUtility
24
{
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
    /**
     * Returns the three part version number (string) from an integer, eg 4012003 -> '4.12.3'
     *
     * @param int $versionInteger Integer representation of version number
     * @return string Version number as format x.x.x
     * @throws \InvalidArgumentException if $versionInteger is not an integer
     */
    public static function convertIntegerToVersionNumber(int $versionInteger): string
    {
        $versionString = str_pad($versionInteger, 9, '0', STR_PAD_LEFT);
        $parts = [
            substr($versionString, 0, 3),
            substr($versionString, 3, 3),
            substr($versionString, 6, 3)
        ];

        return (int)$parts[0] . '.' . (int)$parts[1] . '.' . (int)$parts[2];
    }

Thomas Löffler's avatar
Thomas Löffler committed
44
    /**
45
     * @param \T3o\TerFe2\Domain\Model\Extension $extension
Thomas Löffler's avatar
Thomas Löffler committed
46
47
48
     * @param int $mainVersion
     * @return bool
     */
49
    public static function doesExtensionSupportTypo3Version(\T3o\TerFe2\Domain\Model\Extension $extension, int $mainVersion): bool
Thomas Löffler's avatar
Thomas Löffler committed
50
    {
51
        foreach ($extension->getVersions() as $version) {
Thomas Löffler's avatar
Thomas Löffler committed
52
            if ($version->getTypo3Dependency() === null || $version->getReviewState() === \T3o\TerFe2\Domain\Model\Version::VERSION_IS_INSECURE) {
53
54
55
                continue;
            }

56
            if ($version->doesSupportTypo3Version($mainVersion)) {
57
58
59
                return true;
            }
        }
Thomas Löffler's avatar
Thomas Löffler committed
60

61
        return false;
Thomas Löffler's avatar
Thomas Löffler committed
62
    }
63
64
65
66
67
68
69
70
71
72
73
74
75

    /**
     * Generates a list with compatible TYPO3 versions from given dependencies
     *
     * @param array $dependencies
     * @return string
     */
    public static function getCompatibleTypo3Versions(array $dependencies): string
    {
        $compatibleTypo3Versions = [];

        foreach ($dependencies as $relation) {
            if ((string)($relation['kind'] ?? '') !== 'depends'
76
                || !in_array((string)($relation['extensionKey'] ?? ''), ['typo3', 'cms'], true)
77
78
79
            ) {
                continue;
            }
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124

            $versionRange = (string)($relation['versionRange'] ?? '');

            // Assume all LTS versions are supported
            if ($versionRange === '*') {
                $compatibleTypo3Versions = self::getAllLtsVersionAsMajorNumber();
                continue;
            }

            // Extract all supported versions form minimal and maximal
            if (strpos($versionRange, '-') !== false) {
                [$min, $max] = explode('-', $versionRange);
                if ($min === null || $min === '0' || $min === '0.0.0') {
                    // Skip if minimal version is not valid
                    continue;
                }
                $min = self::getMajorVersion($min);
                if ($min <= 0 || $max === null) {
                    // Skip if major version could not be extracted or maximal version is not valid
                    continue;
                }
                if ($max === '0' || $max === '0.0.0') {
                    // Assume that all LTS versions between the minimal and the latest are compatible.
                    $compatibleTypo3Versions = self::getVersionRange($min);
                    // Also break the foreach as all information was gathered.
                    break;
                }
                $max = self::getMajorVersion($max);
                if ($max <= 0) {
                    // Only add minimal since maximal could not be extracted
                   $compatibleTypo3Versions[] = $min;
                   continue;
                }
                // Add all major versions between min and max
                $compatibleTypo3Versions = self::getVersionRange($min, $max);
                // Also break the foreach as all information was gathered.
                break;
            }

            // Assume only the minimal version is supported
            if (strpos($versionRange, '.') !== false) {
                $major = self::getMajorVersion($versionRange);
                if ($major > 0) {
                    $compatibleTypo3Versions[] = $major;
                }
125
126
127
128
129
            }
        }

        return implode(',', array_unique($compatibleTypo3Versions));
    }
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182

    /**
     * Generates an array of LTS versions with only their major number
     *
     * @return array
     */
    protected static function getAllLtsVersionAsMajorNumber(): array
    {
        $ltsVersions = GeneralUtility::makeInstance(LTSVersionService::class)->getAllLTSVersions();
        asort($ltsVersions, SORT_NUMERIC);
        foreach ($ltsVersions as &$ltsVersion) {
            $ltsVersion = self::getMajorVersion(self::convertIntegerToVersionNumber($ltsVersion));
        }
        return $ltsVersions;
    }

    /**
     * Generate range for min and max by removing invalid version numbers
     *
     * @param int $min
     * @param null|int $max
     * @return array
     */
    protected static function getVersionRange(int $min, int $max = null): array
    {
        $allLtsVersions = self::getAllLtsVersionAsMajorNumber();
        $max = (int)($max ?? end($allLtsVersions) ?: 0);

        if ($max <= 0) {
            return [];
        }

        $range = range($min, $max);
        foreach ($range as $key => $version) {
            if (!in_array($version, $allLtsVersions, true)) {
                unset($range[$key]);
            }
        }

        return $range;
    }

    /**
     * Extract the major version from version string
     *
     * @param string $versionString
     * @return int
     */
    protected static function getMajorVersion(string $versionString): int
    {
        $parts = explode('.', $versionString) ?: [];
        return (int)array_shift($parts);
    }
183
}