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