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