TYPO3  7.6
VersionNumberUtility.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Core\Utility;
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
21 {
28  public static function convertVersionNumberToInteger($versionNumber)
29  {
30  $versionParts = explode('.', $versionNumber);
31  return (int)(((int)$versionParts[0] . str_pad((int)$versionParts[1], 3, '0', STR_PAD_LEFT)) . str_pad((int)$versionParts[2], 3, '0', STR_PAD_LEFT));
32  }
33 
41  public static function convertIntegerToVersionNumber($versionInteger)
42  {
43  if (!is_int($versionInteger)) {
44  throw new \InvalidArgumentException(\TYPO3\CMS\Core\Utility\VersionNumberUtility::class . '::convertIntegerToVersionNumber() supports an integer argument only!', 1334072223);
45  }
46  $versionString = str_pad($versionInteger, 9, '0', STR_PAD_LEFT);
47  $parts = array(
48  substr($versionString, 0, 3),
49  substr($versionString, 3, 3),
50  substr($versionString, 6, 3)
51  );
52  return (int)$parts[0] . '.' . (int)$parts[1] . '.' . (int)$parts[2];
53  }
54 
65  public static function splitVersionRange($version)
66  {
67  $versionRange = array();
68  if (strstr($version, '-')) {
69  $versionRange = explode('-', $version, 2);
70  } else {
71  $versionRange[0] = $version;
72  $versionRange[1] = '';
73  }
74  if (!$versionRange[0]) {
75  $versionRange[0] = '0.0.0';
76  }
77  if (!$versionRange[1]) {
78  $versionRange[1] = '0.0.0';
79  }
80  return $versionRange;
81  }
82 
89  public static function getNumericTypo3Version()
90  {
91  $t3version = static::getCurrentTypo3Version();
92  $t3version = preg_replace('/-?(dev|alpha|beta|RC).*$/', '', $t3version);
93  $parts = GeneralUtility::intExplode('.', $t3version . '..');
94  $t3version = MathUtility::forceIntegerInRange($parts[0], 0, 999) . '.' .
95  MathUtility::forceIntegerInRange($parts[1], 0, 999) . '.' .
96  MathUtility::forceIntegerInRange($parts[2], 0, 999);
97  return $t3version;
98  }
99 
106  public static function getCurrentTypo3Version()
107  {
108  return TYPO3_version;
109  }
110 
119  public static function convertVersionsStringToVersionNumbers($versionsString)
120  {
121  $versions = GeneralUtility::trimExplode('-', $versionsString);
122  $versionsCount = count($versions);
123  for ($i = 0; $i < $versionsCount; $i++) {
124  $cleanedVersion = GeneralUtility::trimExplode('.', $versions[$i]);
125  $cleanedVersionCount = count($cleanedVersion);
126  for ($j = 0; $j < $cleanedVersionCount; $j++) {
127  $cleanedVersion[$j] = MathUtility::forceIntegerInRange($cleanedVersion[$j], 0, 999);
128  }
129  $cleanedVersionString = implode('.', $cleanedVersion);
130  if (static::convertVersionNumberToInteger($cleanedVersionString) === 0) {
131  $cleanedVersionString = '';
132  }
133  $versions[$i] = $cleanedVersionString;
134  }
135  return $versions;
136  }
137 
145  public static function convertVersionStringToArray($version)
146  {
147  $parts = GeneralUtility::intExplode('.', $version . '..');
148  $parts[0] = MathUtility::forceIntegerInRange($parts[0], 0, 999);
149  $parts[1] = MathUtility::forceIntegerInRange($parts[1], 0, 999);
150  $parts[2] = MathUtility::forceIntegerInRange($parts[2], 0, 999);
151  $result = array();
152  $result['version'] = $parts[0] . '.' . $parts[1] . '.' . $parts[2];
153  $result['version_int'] = (int)($parts[0] * 1000000 + $parts[1] * 1000 + $parts[2]);
154  $result['version_main'] = $parts[0];
155  $result['version_sub'] = $parts[1];
156  $result['version_dev'] = $parts[2];
157  return $result;
158  }
159 
168  public static function raiseVersionNumber($raise, $version)
169  {
170  if (!in_array($raise, array('main', 'sub', 'dev'))) {
171  throw new \TYPO3\CMS\Core\Exception('RaiseVersionNumber expects one of "main", "sub" or "dev".', 1342639555);
172  }
173  $parts = GeneralUtility::intExplode('.', $version . '..');
174  $parts[0] = MathUtility::forceIntegerInRange($parts[0], 0, 999);
175  $parts[1] = MathUtility::forceIntegerInRange($parts[1], 0, 999);
176  $parts[2] = MathUtility::forceIntegerInRange($parts[2], 0, 999);
177  switch ((string)$raise) {
178  case 'main':
179  $parts[0]++;
180  $parts[1] = 0;
181  $parts[2] = 0;
182  break;
183  case 'sub':
184  $parts[1]++;
185  $parts[2] = 0;
186  break;
187  case 'dev':
188  $parts[2]++;
189  break;
190  }
191  return $parts[0] . '.' . $parts[1] . '.' . $parts[2];
192  }
193 }