4815f236e6fadd43e455966532d34e75165b8d63
[Packages/TYPO3.CMS.git] / typo3 / sysext / extensionmanager / Classes / Utility / EmConfUtility.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 use TYPO3\CMS\Core\Core\Environment;
18 use TYPO3\CMS\Core\SingletonInterface;
19
20 /**
21 * Utility for dealing with ext_emconf
22 */
23 class EmConfUtility implements SingletonInterface
24 {
25 /**
26 * Returns the $EM_CONF array from an extensions ext_emconf.php file
27 *
28 * @param array $extension Extension information array
29 * @return array EMconf array values.
30 */
31 public function includeEmConf(array $extension)
32 {
33 $_EXTKEY = $extension['key'];
34 $path = Environment::getPublicPath() . '/' . $extension['siteRelPath'] . 'ext_emconf.php';
35 $EM_CONF = null;
36 if (file_exists($path)) {
37 include $path;
38 if (is_array($EM_CONF[$_EXTKEY])) {
39 return $EM_CONF[$_EXTKEY];
40 }
41 }
42 return false;
43 }
44
45 /**
46 * Generates the content for the ext_emconf.php file
47 * Sets dependencies from TER data if any
48 *
49 * @param array $extensionData
50 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extension Extension object from TER data
51 * @return string
52 * @internal
53 */
54 public function constructEmConf(array $extensionData, \TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extension = null)
55 {
56 if (is_object($extension) && empty($extensionData['EM_CONF']['constraints'])) {
57 $extensionData['EM_CONF']['constraints'] = unserialize($extension->getSerializedDependencies());
58 }
59 $emConf = $this->fixEmConf($extensionData['EM_CONF']);
60 $emConf = var_export($emConf, true);
61 $code = '<?php
62
63 /***************************************************************
64 * Extension Manager/Repository config file for ext "' . $extensionData['extKey'] . '".
65 *
66 * Auto generated ' . date('d-m-Y H:i') . '
67 *
68 * Manual updates:
69 * Only the data in the array - everything else is removed by next
70 * writing. "version" and "dependencies" must not be touched!
71 ***************************************************************/
72
73 $EM_CONF[$_EXTKEY] = ' . $emConf . ';
74
75 ';
76 return $code;
77 }
78
79 /**
80 * Fix the em conf - Converts old / ter em_conf format to new format
81 *
82 * @param array $emConf
83 * @return array
84 */
85 public function fixEmConf(array $emConf)
86 {
87 if (
88 !isset($emConf['constraints']) || !isset($emConf['constraints']['depends'])
89 || !isset($emConf['constraints']['conflicts']) || !isset($emConf['constraints']['suggests'])
90 ) {
91 if (!isset($emConf['constraints']) || !isset($emConf['constraints']['depends'])) {
92 $emConf['constraints']['depends'] = $this->stringToDependency($emConf['dependencies'] ?? '');
93 if (isset($emConf['PHP_version']) && (string)$emConf['PHP_version'] !== '') {
94 $emConf['constraints']['depends']['php'] = $emConf['PHP_version'];
95 }
96 if (isset($emConf['TYPO3_version']) && (string)$emConf['TYPO3_version'] !== '') {
97 $emConf['constraints']['depends']['typo3'] = $emConf['TYPO3_version'];
98 }
99 }
100 if (!isset($emConf['constraints']) || !isset($emConf['constraints']['conflicts'])) {
101 $emConf['constraints']['conflicts'] = $this->stringToDependency($emConf['conflicts'] ?? '');
102 }
103 if (!isset($emConf['constraints']) || !isset($emConf['constraints']['suggests'])) {
104 $emConf['constraints']['suggests'] = [];
105 }
106 }
107
108 // Remove TER v1-style entries
109 unset($emConf['dependencies']);
110 unset($emConf['conflicts']);
111 unset($emConf['suggests']);
112 unset($emConf['private']);
113 unset($emConf['download_password']);
114 unset($emConf['TYPO3_version']);
115 unset($emConf['PHP_version']);
116 unset($emConf['internal']);
117 unset($emConf['module']);
118 unset($emConf['loadOrder']);
119 unset($emConf['lockType']);
120 unset($emConf['shy']);
121 unset($emConf['priority']);
122 unset($emConf['modify_tables']);
123 unset($emConf['CGLcompliance']);
124 unset($emConf['CGLcompliance_note']);
125
126 return $emConf;
127 }
128
129 /**
130 * Checks whether the passed dependency is TER-style (string)or
131 * TER2-style (array)and returns a single string for displaying the
132 * dependencies.
133 *
134 * It leaves out all version numbers and the "php" and "typo3" dependencies,
135 * as they are implicit and of no interest without the version number.
136 *
137 * @param mixed $dependency Either a string or an array listing dependencies.
138 * @return string A simple dependency list for display
139 */
140 public function stringToDependency($dependency)
141 {
142 $constraint = [];
143 if (is_string($dependency) && $dependency !== '') {
144 $dependency = explode(',', $dependency);
145 foreach ($dependency as $v) {
146 $constraint[$v] = '';
147 }
148 }
149 return $constraint;
150 }
151 }