LTSVersionService.php 5.71 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
<?php
namespace T3o\TerFe2\Service;

/*
 * This file is part of the TYPO3 CMS project.
 *
 * 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.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 * The TYPO3 project - inspiring people to share!
 */

use TYPO3\CMS\Core\Utility\VersionNumberUtility;

/**
 * Class LTSVersionService
 */
class LTSVersionService
{
24 25 26 27
    const FACTOR_MAIN_VERSION = 1000000;
    const FACTOR_MINOR_VERSION = 1000;
    const FIRST_LTS_VERSION_WITH_MAIN_VERSION_NUMBER = 7;

28
    /**
29
     * @var array
30 31 32
     */
    public $coreData = '';

33 34 35 36 37
    public $ltsVersionsWithMinorVersions = [
        '4.5',
        '6.2'
    ];

38 39 40 41 42
    public function __construct()
    {
        // Will be automatically update with UpdateCurrentVersionListTask see T3o\Ter\Task\UpdateCurrentVersionListTask
        $sourceFile = PATH_site . $GLOBALS['TYPO3_CONF_VARS']['BE']['fileadminDir'] . 'currentcoredata.json';
        $json = file_get_contents($sourceFile);
43
        $this->coreData = json_decode($json, true);
44 45 46 47 48
    }

    /**
     * Get the current stable versions
     *
49
     * @param bool $fullVersionNumber
50 51
     * @return array
     */
52
    public function getCurrentStableVersions($fullVersionNumber = false): array
53 54
    {
        return [
55 56
            $this->getLatestOldLTS($fullVersionNumber),
            $this->getLatestLTS($fullVersionNumber)
57 58 59 60 61 62
        ];
    }

    /**
     * Returns all maintained versions.
     *
63
     * @param bool $fullVersionNumber
64 65
     * @return array
     */
66
    public function getAllMaintainedVersions($fullVersionNumber = false): array
67
    {
68 69 70
        $versions = $this->getCurrentStableVersions($fullVersionNumber);
        array_push($versions, $this->getLatestDevelopmentVersion($fullVersionNumber));

71 72 73
        return $versions;
    }

74 75 76 77 78 79 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
    /**
     * Get all LTS versions ever
     *
     * @return array
     */
    public function getAllLTSVersions(): array
    {
        $ltsVersions = [];
        foreach ($this->coreData as $releaseVersion => $releaseData) {
            if (in_array($releaseVersion, $this->ltsVersionsWithMinorVersions) || $releaseVersion >= self::FIRST_LTS_VERSION_WITH_MAIN_VERSION_NUMBER) {
                $latestVersionParts = explode('.', $releaseData['latest']);
                if ((int)$latestVersionParts[1] === 0) {
                    continue;
                }
                $minimumVersion = $latestVersionParts[0] . '.' . $latestVersionParts[1] . '.0';
                $ltsVersions[] = VersionNumberUtility::convertVersionNumberToInteger($minimumVersion);
            }
        }
        sort($ltsVersions);

        return $ltsVersions;
    }

    /**
     * Get all LTS versions but current maintained
     *
     * @return array
     */
    public function getOldLTSVersions(): array
    {
        $oldLTSVersions = $this->getAllLTSVersions();
        foreach ($oldLTSVersions as $key => $version) {
            $mainVersion = floor($version / self::FACTOR_MAIN_VERSION);
            if (in_array($mainVersion, $this->getCurrentStableVersions())) {
                unset($oldLTSVersions[$key]);
            }
        }

        return $oldLTSVersions;
    }

115 116 117 118 119 120 121 122 123
    /**
     * Get latest version within a specific release
     *
     * @param int $release
     * @return string
     */
    public function getLatestVersionOfRelease($release)
    {
        $versions = [];
124
        foreach ($this->coreData[$release]['releases'] as $release) {
125 126 127 128 129 130 131 132 133
            $versions[] = VersionNumberUtility::convertVersionNumberToInteger($release['version']);
        }
        sort($versions);
        return VersionNumberUtility::convertIntegerToVersionNumber(array_pop($versions));
    }

    /**
     * Get latest development version
     *
134 135
     * @param bool $fullVersionNumber
     * @return int
136
     */
137
    public function getLatestDevelopmentVersion($fullVersionNumber = false): int
138
    {
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
        if ($fullVersionNumber) {
            return VersionNumberUtility::convertVersionNumberToInteger($this->coreData['latest_stable']);
        }

        return floor(VersionNumberUtility::convertVersionNumberToInteger($this->coreData['latest_stable']) / self::FACTOR_MAIN_VERSION);
    }

    public function getVersionLabelOfRelease(int $release): string
    {
        if ($release >= (self::FIRST_LTS_VERSION_WITH_MAIN_VERSION_NUMBER * self::FACTOR_MAIN_VERSION)) {
            $versionLabel = (string)floor($release / self::FACTOR_MAIN_VERSION);
        } else {
            $fullVersionLabelParts = explode('.', VersionNumberUtility::convertIntegerToVersionNumber($release));
            $versionLabel = $fullVersionLabelParts[0] . '.' . $fullVersionLabelParts[1];
        }

        return $versionLabel;
156 157 158 159 160
    }

    /**
     * Get latest LTS Version
     *
161
     * @param bool $fullVersionNumber
162 163
     * @return int
     */
164
    public function getLatestLTS($fullVersionNumber = false): int
165
    {
166 167 168 169 170
        if ($fullVersionNumber) {
            return VersionNumberUtility::convertVersionNumberToInteger($this->coreData['latest_lts']);
        }

        return floor(VersionNumberUtility::convertVersionNumberToInteger($this->coreData['latest_lts']) / self::FACTOR_MAIN_VERSION);
171 172 173 174
    }

    /**
     * Get latest "old" LTS Version
175
     *
176
     * @param bool $fullVersionNumber
177 178
     * @return int
     */
179
    public function getLatestOldLTS($fullVersionNumber = false): int
180
    {
181 182 183 184 185
        if ($fullVersionNumber) {
            return VersionNumberUtility::convertVersionNumberToInteger($this->coreData['latest_old_lts']);
        }

        return floor(VersionNumberUtility::convertVersionNumberToInteger($this->coreData['latest_old_lts']) / self::FACTOR_MAIN_VERSION);
186
    }
187
}