5977ea5e0c39d4720d6f08d2317c9d315ee24030
[Packages/TYPO3.CMS.git] / typo3 / sysext / extensionmanager / Classes / Utility / ExtensionModelUtility.php
1 <?php
2 namespace TYPO3\CMS\Extensionmanager\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
17 /**
18 * Utility for dealing with extension model related helper functions
19 */
20 class ExtensionModelUtility
21 {
22 /**
23 * @var \TYPO3\CMS\Extbase\Object\ObjectManager
24 */
25 protected $objectManager;
26
27 /**
28 * @param \TYPO3\CMS\Extbase\Object\ObjectManager $objectManager
29 */
30 public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManager $objectManager)
31 {
32 $this->objectManager = $objectManager;
33 }
34
35 /**
36 * Map a legacy extension array to an object
37 *
38 * @param array $extensionArray
39 * @return \TYPO3\CMS\Extensionmanager\Domain\Model\Extension
40 */
41 public function mapExtensionArrayToModel(array $extensionArray)
42 {
43 /** @var \TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extension */
44 $extension = $this->objectManager->get(\TYPO3\CMS\Extensionmanager\Domain\Model\Extension::class);
45 $extension->setExtensionKey($extensionArray['key']);
46 if (isset($extensionArray['version'])) {
47 $extension->setVersion($extensionArray['version']);
48 }
49 if (isset($extensionArray['constraints'])) {
50 $extension->setDependencies($this->convertDependenciesToObjects(serialize($extensionArray['constraints'])));
51 }
52 return $extension;
53 }
54
55 /**
56 * Converts string dependencies to an object storage of dependencies
57 *
58 * @param string $dependencies
59 * @return \SplObjectStorage
60 */
61 public function convertDependenciesToObjects($dependencies)
62 {
63 $dependenciesObject = new \SplObjectStorage();
64 $unserializedDependencies = unserialize($dependencies);
65 if (!is_array($unserializedDependencies)) {
66 return $dependenciesObject;
67 }
68 foreach ($unserializedDependencies as $dependencyType => $dependencyValues) {
69 // Dependencies might be given as empty string, e.g. conflicts => ''
70 if (!is_array($dependencyValues)) {
71 continue;
72 }
73 foreach ($dependencyValues as $dependency => $versions) {
74 if ($dependencyType && $dependency) {
75 $versionNumbers = \TYPO3\CMS\Core\Utility\VersionNumberUtility::convertVersionsStringToVersionNumbers($versions);
76 $lowest = $versionNumbers[0];
77 if (count($versionNumbers) === 2) {
78 $highest = $versionNumbers[1];
79 } else {
80 $highest = '';
81 }
82 /** @var $dependencyObject \TYPO3\CMS\Extensionmanager\Domain\Model\Dependency */
83 $dependencyObject = $this->objectManager->get(\TYPO3\CMS\Extensionmanager\Domain\Model\Dependency::class);
84 $dependencyObject->setType($dependencyType);
85 // dynamically migrate 'cms' dependency to 'core' dependency
86 // see also \TYPO3\CMS\Core\Package\Package::getPackageMetaData
87 $dependencyObject->setIdentifier($dependency === 'cms' ? 'core' : $dependency);
88 $dependencyObject->setLowestVersion($lowest);
89 $dependencyObject->setHighestVersion($highest);
90 $dependenciesObject->attach($dependencyObject);
91 unset($dependencyObject);
92 }
93 }
94 }
95 return $dependenciesObject;
96 }
97 }