b66faa507cc7ffbc012490f7ffedf3718ec06f1a
[Packages/TYPO3.CMS.git] / typo3 / sysext / extensionmanager / Classes / Utility / EmConfUtility.php
1 <?php
2 namespace TYPO3\CMS\Extensionmanager\Utility;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2012-2013 Susanne Moog <susanne.moog@typo3.org>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the text file GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29 /**
30 * Utility for dealing with ext_emconf
31 *
32 * @author Susanne Moog <susanne.moog@typo3.org>
33 */
34 class EmConfUtility implements \TYPO3\CMS\Core\SingletonInterface {
35
36 /**
37 * Returns the $EM_CONF array from an extensions ext_emconf.php file
38 *
39 * @param array $extension Extension information array
40 * @return array EMconf array values.
41 */
42 public function includeEmConf(array $extension) {
43 $_EXTKEY = $extension['key'];
44 $path = PATH_site . $extension['siteRelPath'] . 'ext_emconf.php';
45 $EM_CONF = NULL;
46 if (file_exists($path)) {
47 include $path;
48 if (is_array($EM_CONF[$_EXTKEY])) {
49 return $EM_CONF[$_EXTKEY];
50 }
51 }
52 return FALSE;
53 }
54
55 /**
56 * Generates the content for the ext_emconf.php file
57 * Sets dependencies from TER data if any
58 *
59 * @internal
60 * @param array $extensionData
61 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extension Extension object from TER data
62 * @return string
63 */
64 public function constructEmConf(array $extensionData, \TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extension = NULL) {
65 if (is_object($extension)) {
66 $extensionData['EM_CONF']['constraints'] = unserialize($extension->getSerializedDependencies());
67 }
68 $emConf = $this->fixEmConf($extensionData['EM_CONF']);
69 $emConf = var_export($emConf, TRUE);
70 $code = '<?php
71
72 /***************************************************************
73 * Extension Manager/Repository config file for ext "' . $extensionData['extKey'] . '".
74 *
75 * Auto generated ' . date('d-m-Y H:i') . '
76 *
77 * Manual updates:
78 * Only the data in the array - everything else is removed by next
79 * writing. "version" and "dependencies" must not be touched!
80 ***************************************************************/
81
82 $EM_CONF[$_EXTKEY] = ' . $emConf . ';
83
84 ?>';
85 return str_replace(' ', TAB, $code);
86 }
87
88 /**
89 * Fix the em conf - Converts old / ter em_conf format to new format
90 *
91 * @param array $emConf
92 * @return array
93 */
94 public function fixEmConf(array $emConf) {
95 if (
96 !isset($emConf['constraints']) || !isset($emConf['constraints']['depends'])
97 || !isset($emConf['constraints']['conflicts']) || !isset($emConf['constraints']['suggests'])
98 ) {
99 if (!isset($emConf['constraints']) || !isset($emConf['constraints']['depends'])) {
100 $emConf['constraints']['depends'] = $this->stringToDependency($emConf['dependencies']);
101 if (strlen($emConf['PHP_version'])) {
102 $emConf['constraints']['depends']['php'] = $emConf['PHP_version'];
103 }
104 if (strlen($emConf['TYPO3_version'])) {
105 $emConf['constraints']['depends']['typo3'] = $emConf['TYPO3_version'];
106 }
107 }
108 if (!isset($emConf['constraints']) || !isset($emConf['constraints']['conflicts'])) {
109 $emConf['constraints']['conflicts'] = $this->stringToDependency($emConf['conflicts']);
110 }
111 if (!isset($emConf['constraints']) || !isset($emConf['constraints']['suggests'])) {
112 $emConf['constraints']['suggests'] = array();
113 }
114 } elseif (isset($emConf['constraints']) && isset($emConf['dependencies'])) {
115 $emConf['suggests'] = isset($emConf['suggests']) ? $emConf['suggests'] : array();
116 $emConf['dependencies'] = $this->dependencyToString($emConf['constraints']);
117 $emConf['conflicts'] = $this->dependencyToString($emConf['constraints'], 'conflicts');
118 }
119 unset($emConf['private']);
120 unset($emConf['download_password']);
121 unset($emConf['TYPO3_version']);
122 unset($emConf['PHP_version']);
123 return $emConf;
124 }
125
126 /**
127 * Checks whether the passed dependency is TER2-style (array) and returns a
128 * single string for displaying the 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 * @param string $type The dependency type to list if $dep is an array
135 * @return string A simple dependency list for display
136 */
137 static public function dependencyToString($dependency, $type = 'depends') {
138 if (!is_array($dependency) || !is_array($dependency[$type])) {
139 return '';
140 }
141
142 unset($dependency[$type]['php']);
143 unset($dependency[$type]['typo3']);
144
145 return implode(',', array_keys($dependency[$type]));
146 }
147
148 /**
149 * Checks whether the passed dependency is TER-style (string) or
150 * TER2-style (array) and returns a single string for displaying the
151 * dependencies.
152 *
153 * It leaves out all version numbers and the "php" and "typo3" dependencies,
154 * as they are implicit and of no interest without the version number.
155 *
156 * @param mixed $dependency Either a string or an array listing dependencies.
157 * @return string A simple dependency list for display
158 */
159 public function stringToDependency($dependency) {
160 $constraint = array();
161 if (is_string($dependency) && strlen($dependency)) {
162 $dependency = explode(',', $dependency);
163 foreach ($dependency as $v) {
164 $constraint[$v] = '';
165 }
166 }
167 return $constraint;
168 }
169
170 }