8cfb241c1c229e48bfd0d207abb202042920586a
[Packages/TYPO3.CMS.git] / typo3 / sysext / extensionmanager / Classes / ViewHelpers / Typo3DependencyViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Extensionmanager\ViewHelpers;
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
17 use TYPO3\CMS\Core\Utility\MathUtility;
18 use TYPO3\CMS\Core\Utility\VersionNumberUtility;
19 use TYPO3\CMS\Extensionmanager\Domain\Model\Dependency;
20 use TYPO3\CMS\Extensionmanager\Domain\Model\Extension;
21 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
22 use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
23 use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
24
25 /**
26 * Shows the version numbers of the TYPO3 dependency, if any
27 *
28 * @internal
29 */
30 class Typo3DependencyViewHelper extends AbstractViewHelper implements CompilableInterface {
31
32 /**
33 * Finds and returns the suitable TYPO3 versions of an extension
34 *
35 * @param Extension $extension
36 * @return string
37 */
38 public function render(Extension $extension) {
39 return self::renderStatic(
40 array(
41 'extension' => $extension,
42 ),
43 $this->buildRenderChildrenClosure(),
44 $this->renderingContext
45 );
46 }
47
48 /**
49 * @param array $arguments
50 * @param callable $renderChildrenClosure
51 * @param RenderingContextInterface $renderingContext
52 *
53 * @return string
54 */
55 static public function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) {
56 $extension = $arguments['extension'];
57 /** @var Dependency $dependency */
58 foreach ($extension->getDependencies() as $dependency) {
59 if ($dependency->getIdentifier() === 'typo3') {
60 $lowestVersion = $dependency->getLowestVersion();
61 $highestVersion = $dependency->getHighestVersion();
62 $cssClass = self::isVersionSuitable($lowestVersion, $highestVersion) ? 'success' : 'default';
63 return
64 '<span class="label label-' . $cssClass . '">'
65 . htmlspecialchars($lowestVersion) . ' - ' . htmlspecialchars($highestVersion)
66 . '</span>';
67 }
68 }
69 return '';
70 }
71
72 /**
73 * Check if current TYPO3 version is suitable for the extension
74 *
75 * @param string $lowestVersion
76 * @param string $highestVersion
77 * @return bool
78 */
79 static protected function isVersionSuitable($lowestVersion, $highestVersion) {
80 $numericTypo3Version = VersionNumberUtility::convertVersionNumberToInteger(VersionNumberUtility::getNumericTypo3Version());
81 $numericLowestVersion = VersionNumberUtility::convertVersionNumberToInteger($lowestVersion);
82 $numericHighestVersion = VersionNumberUtility::convertVersionNumberToInteger($highestVersion);
83 return MathUtility::isIntegerInRange($numericTypo3Version, $numericLowestVersion, $numericHighestVersion);
84 }
85 }