VersionUtility.php 2.68 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
/**
 * Utilities to manage versions
 */
20
class VersionUtility
21
{
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
    /**
     * 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
41
    /**
42
     * @param \T3o\TerFe2\Domain\Model\Extension $extension
Thomas Löffler's avatar
Thomas Löffler committed
43
44
45
     * @param int $mainVersion
     * @return bool
     */
46
    public static function doesExtensionSupportTypo3Version(\T3o\TerFe2\Domain\Model\Extension $extension, int $mainVersion): bool
Thomas Löffler's avatar
Thomas Löffler committed
47
    {
48
        foreach ($extension->getVersions() as $version) {
Thomas Löffler's avatar
Thomas Löffler committed
49
            if ($version->getTypo3Dependency() === null || $version->getReviewState() === \T3o\TerFe2\Domain\Model\Version::VERSION_IS_INSECURE) {
50
51
52
                continue;
            }

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

58
        return false;
Thomas Löffler's avatar
Thomas Löffler committed
59
    }
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84

    /**
     * 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'
                || (string)($relation['extensionKey'] ?? '') !== 'typo3'
            ) {
                continue;
            }
            foreach (explode('-', (string)($relation['versionRange'] ?? '')) as $version) {
                $parts = explode('.', $version);
                $compatibleTypo3Versions[] = array_shift($parts);
            }
        }

        return implode(',', array_unique($compatibleTypo3Versions));
    }
85
}