EmConfUtility.php 5.13 KB
Newer Older
1
<?php
2

3
/*
4
 * This file is part of the TYPO3 CMS project.
5
 *
6
7
8
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
9
 *
10
11
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
12
 *
13
14
 * The TYPO3 project - inspiring people to share!
 */
15

16
17
namespace TYPO3\CMS\Extensionmanager\Utility;

18
19
use TYPO3\CMS\Core\SingletonInterface;

20
21
/**
 * Utility for dealing with ext_emconf
22
 * @internal This class is a specific ExtensionManager implementation and is not part of the Public TYPO3 API.
23
 */
24
class EmConfUtility implements SingletonInterface
25
26
27
28
{
    /**
     * Returns the $EM_CONF array from an extensions ext_emconf.php file
     *
29
     * @param string $extensionKey the extension name
30
     * @param string $absolutePath path to the ext_emconf.php
31
     * @return array|bool EMconf array values or false if no ext_emconf.php found.
32
     */
33
    public function includeEmConf(string $extensionKey, string $absolutePath)
34
    {
35
        $_EXTKEY = $extensionKey;
36
        $path = rtrim($absolutePath, '/') . '/ext_emconf.php';
37
        $EM_CONF = null;
38
        if (!empty($absolutePath) && file_exists($path)) {
39
40
41
42
43
44
45
            include $path;
            if (is_array($EM_CONF[$_EXTKEY])) {
                return $EM_CONF[$_EXTKEY];
            }
        }
        return false;
    }
46

47
48
49
    /**
     * Generates the content for the ext_emconf.php file
     *
50
51
     * @param string $extensionKey
     * @param array $emConf
52
53
     * @return string
     */
54
    public function constructEmConf(string $extensionKey, array $emConf)
55
    {
56
        $emConf = $this->fixEmConf($emConf);
57
        $emConf = var_export($emConf, true);
58
        return '<?php
59
60

/***************************************************************
61
 * Extension Manager/Repository config file for ext "' . $extensionKey . '".
62
63
64
65
66
67
68
 *
 * Auto generated ' . date('d-m-Y H:i') . '
 *
 * Manual updates:
 * Only the data in the array - everything else is removed by next
 * writing. "version" and "dependencies" must not be touched!
 ***************************************************************/
69

70
$EM_CONF[$_EXTKEY] = ' . $emConf . ';
71

72
';
73
    }
74

75
76
77
78
79
80
    /**
     * Fix the em conf - Converts old / ter em_conf format to new format
     *
     * @param array $emConf
     * @return array
     */
81
    protected function fixEmConf(array $emConf)
82
83
84
85
86
87
    {
        if (
            !isset($emConf['constraints']) || !isset($emConf['constraints']['depends'])
            || !isset($emConf['constraints']['conflicts']) || !isset($emConf['constraints']['suggests'])
        ) {
            if (!isset($emConf['constraints']) || !isset($emConf['constraints']['depends'])) {
88
89
                $emConf['constraints']['depends'] = $this->stringToDependency($emConf['dependencies'] ?? '');
                if (isset($emConf['PHP_version']) && (string)$emConf['PHP_version'] !== '') {
90
91
                    $emConf['constraints']['depends']['php'] = $emConf['PHP_version'];
                }
92
                if (isset($emConf['TYPO3_version']) && (string)$emConf['TYPO3_version'] !== '') {
93
94
95
96
                    $emConf['constraints']['depends']['typo3'] = $emConf['TYPO3_version'];
                }
            }
            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'] = [];
101
102
            }
        }
103

104
105
106
107
108
109
110
111
112
113
114
115
        // Remove TER v1-style entries
        unset($emConf['dependencies']);
        unset($emConf['conflicts']);
        unset($emConf['suggests']);
        unset($emConf['private']);
        unset($emConf['download_password']);
        unset($emConf['TYPO3_version']);
        unset($emConf['PHP_version']);
        unset($emConf['internal']);
        unset($emConf['module']);
        unset($emConf['loadOrder']);
        unset($emConf['lockType']);
116
        unset($emConf['createDirs']);
117
118
119
120
121
        unset($emConf['shy']);
        unset($emConf['priority']);
        unset($emConf['modify_tables']);
        unset($emConf['CGLcompliance']);
        unset($emConf['CGLcompliance_note']);
122

123
124
        return $emConf;
    }
125

126
127
128
129
130
131
132
133
134
    /**
     * Checks whether the passed dependency is TER-style (string)or
     * TER2-style (array)and returns a single string for displaying the
     * dependencies.
     *
     * It leaves out all version numbers and the "php" and "typo3" dependencies,
     * as they are implicit and of no interest without the version number.
     *
     * @param mixed $dependency Either a string or an array listing dependencies.
135
     * @return array A simple dependency list for display
136
     */
137
    protected function stringToDependency($dependency)
138
    {
139
        $constraint = [];
140
141
142
143
144
145
146
147
        if (is_string($dependency) && $dependency !== '') {
            $dependency = explode(',', $dependency);
            foreach ($dependency as $v) {
                $constraint[$v] = '';
            }
        }
        return $constraint;
    }
148
}