‪TYPO3CMS  10.4
ConstantConfigurationParser.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
23 
34 {
40  protected ‪$subCategories = [
41  // Standard categories:
42  'enable' => ['Enable features', 'a'],
43  'dims' => ['Dimensions, widths, heights, pixels', 'b'],
44  'file' => ['Files', 'c'],
45  'typo' => ['Typography', 'd'],
46  'color' => ['Colors', 'e'],
47  'links' => ['Links and targets', 'f'],
48  'language' => ['Language specific constants', 'g'],
49  // subcategories based on the default content elements
50  'cheader' => ['Content: \'Header\'', 'ma'],
51  'cheader_g' => ['Content: \'Header\', Graphical', 'ma'],
52  'ctext' => ['Content: \'Text\'', 'mb'],
53  'cimage' => ['Content: \'Image\'', 'md'],
54  'ctextmedia' => ['Content: \'Textmedia\'', 'ml'],
55  'cbullets' => ['Content: \'Bullet list\'', 'me'],
56  'ctable' => ['Content: \'Table\'', 'mf'],
57  'cuploads' => ['Content: \'Filelinks\'', 'mg'],
58  'cmultimedia' => ['Content: \'Multimedia\'', 'mh'],
59  'cmedia' => ['Content: \'Media\'', 'mr'],
60  'cmailform' => ['Content: \'Form\'', 'mi'],
61  'csearch' => ['Content: \'Search\'', 'mj'],
62  'clogin' => ['Content: \'Login\'', 'mk'],
63  'cmenu' => ['Content: \'Menu/Sitemap\'', 'mm'],
64  'cshortcut' => ['Content: \'Insert records\'', 'mn'],
65  'clist' => ['Content: \'List of records\'', 'mo'],
66  'chtml' => ['Content: \'HTML\'', 'mq'],
67  ];
68 
76  public function ‪prepareConfigurationForView(array $configuration): array
77  {
78  $resultArray = [];
79  if (!empty($configuration)) {
80  $hierarchicConfiguration = [];
81  foreach ($configuration as $configurationOption) {
82  $originalConfiguration = $this->‪buildConfigurationArray($configurationOption);
83  ‪ArrayUtility::mergeRecursiveWithOverrule($originalConfiguration, $hierarchicConfiguration);
84  $hierarchicConfiguration = $originalConfiguration;
85  }
86  // Flip category array as it was merged the other way around
87  $hierarchicConfiguration = array_reverse($hierarchicConfiguration);
88  // Sort configurations of each subcategory
89  foreach ($hierarchicConfiguration as &$catConfigurationArray) {
90  foreach ($catConfigurationArray as &$subcatConfigurationArray) {
91  uasort($subcatConfigurationArray, function ($a, $b) {
92  return strnatcmp($a['subcat'], $b['subcat']);
93  });
94  }
95  unset($subcatConfigurationArray);
96  }
97  unset($catConfigurationArray);
98  $resultArray = $hierarchicConfiguration;
99  }
100  return $resultArray;
101  }
102 
110  protected function ‪buildConfigurationArray(array $configurationOption): array
111  {
112  $hierarchicConfiguration = [];
113  if (GeneralUtility::isFirstPartOfStr($configurationOption['type'], 'user')) {
114  $configurationOption = $this->‪extractInformationForConfigFieldsOfTypeUser($configurationOption);
115  } elseif (GeneralUtility::isFirstPartOfStr($configurationOption['type'], 'options')) {
116  $configurationOption = $this->‪extractInformationForConfigFieldsOfTypeOptions($configurationOption);
117  }
118  $languageService = $this->‪getLanguageService();
119  if (is_string($configurationOption['label'])) {
120  $translatedLabel = $languageService->sL($configurationOption['label']);
121  if ($translatedLabel) {
122  $configurationOption['label'] = $translatedLabel;
123  }
124  }
125  $configurationOption['labels'] = ‪GeneralUtility::trimExplode(':', $configurationOption['label'], false, 2);
126  $configurationOption['subcat_name'] = $configurationOption['subcat_name'] ?: '__default';
127  $hierarchicConfiguration[$configurationOption['cat']][$configurationOption['subcat_name']][$configurationOption['name']] = $configurationOption;
128  return $hierarchicConfiguration;
129  }
130 
138  protected function ‪extractInformationForConfigFieldsOfTypeOptions(array $configurationOption): array
139  {
141  preg_match('/options\[(.*)\]/is', $configurationOption['type'], $typeMatches);
142  foreach (‪GeneralUtility::trimExplode(',', $typeMatches[1]) as $optionItem) {
143  $optionPair = ‪GeneralUtility::trimExplode('=', $optionItem);
144  if (count($optionPair) === 2) {
145  $configurationOption['generic'][$optionPair[0]] = $optionPair[1];
146  } else {
147  $configurationOption['generic'][$optionPair[0]] = $optionPair[0];
148  }
149  }
150  $configurationOption['type'] = 'options';
151  return $configurationOption;
152  }
153 
161  protected function ‪extractInformationForConfigFieldsOfTypeUser(array $configurationOption): array
162  {
164  preg_match('/user\\[(.*)\\]/is', $configurationOption['type'], $matches);
165  $configurationOption['generic'] = $matches[1];
166  $configurationOption['type'] = 'user';
167  return $configurationOption;
168  }
169 
195  public function ‪getConfigurationAsValuedArray(string $rawConfiguration): array
196  {
197  $typoScriptParser = new ‪TypoScriptParser();
198  $typoScriptParser->regComments = true;
199  $typoScriptParser->parse($rawConfiguration);
200  $flatSetup = ‪ArrayUtility::flatten($typoScriptParser->setup, '', true);
201  $theConstants = $this->‪parseComments($flatSetup);
202 
203  // Loop through configuration items, see if it is assigned to a sub category
204  // and add the sub category label to the item property if so.
205  foreach ($theConstants as $configurationOptionName => $configurationOption) {
206  if (
207  array_key_exists('subcat_name', $configurationOption) && isset($this->subCategories[$configurationOption['subcat_name']][0])
208  ) {
209  $theConstants[$configurationOptionName]['subcat_label'] = $this->subCategories[$configurationOption['subcat_name']][0];
210  }
211  }
212  return $theConstants;
213  }
214 
223  public function ‪parseComments($flatSetup, $default = null): array
224  {
225  $default = $default ?? $flatSetup;
226  $categoryLabels = [];
227  $editableComments = [];
228  $counter = 0;
229  foreach ($flatSetup as $const => $value) {
230  $key = $const . '..';
231  if (substr($const, -2) === '..' || !isset($flatSetup[$key])) {
232  continue;
233  }
234  $counter++;
235  $comment = trim($flatSetup[$key]);
236  foreach (explode(LF, $comment) as $k => $v) {
237  $line = trim(preg_replace('/^[#\\/]*/', '', $v) ?? '');
238  if (!$line) {
239  continue;
240  }
241  foreach (explode(';', $line) as $par) {
242  if (strpos($par, '=') !== false) {
243  $keyValPair = explode('=', $par, 2);
244  switch (strtolower(trim($keyValPair[0]))) {
245  case 'type':
246  // Type:
247  $editableComments[$const]['type'] = trim($keyValPair[1]);
248  break;
249  case 'cat':
250  // List of categories.
251  $catSplit = explode('/', strtolower($keyValPair[1]));
252  $catSplit[0] = trim($catSplit[0]);
253  if (isset($categoryLabels[$catSplit[0]])) {
254  $catSplit[0] = $categoryLabels[$catSplit[0]];
255  }
256  $editableComments[$const]['cat'] = $catSplit[0];
257  // This is the subcategory. Must be a key in $this->subCategories[].
258  // catSplit[2] represents the search-order within the subcat.
259  $catSplit[1] = !empty($catSplit[1]) ? trim($catSplit[1]) : '';
260  if ($catSplit[1] && isset($this->subCategories[$catSplit[1]])) {
261  $editableComments[$const]['subcat_name'] = $catSplit[1];
262  $orderIdentifier = isset($catSplit[2]) ? trim($catSplit[2]) : $counter;
263  $editableComments[$const]['subcat'] = $this->subCategories[$catSplit[1]][1]
264  . '/' . $catSplit[1] . '/' . $orderIdentifier . 'z';
265  } elseif (isset($catSplit[2])) {
266  $editableComments[$const]['subcat'] = 'x/' . trim($catSplit[2]) . 'z';
267  } else {
268  $editableComments[$const]['subcat'] = 'x/' . $counter . 'z';
269  }
270  break;
271  case 'label':
272  // Label
273  $editableComments[$const]['label'] = trim($keyValPair[1]);
274  break;
275  case 'customcategory':
276  // Custom category label
277  $customCategory = explode('=', $keyValPair[1], 2);
278  if (trim($customCategory[0])) {
279  $categoryKey = strtolower($customCategory[0]);
280  $categoryLabels[$categoryKey] = $this->‪getLanguageService()->‪sL($customCategory[1]);
281  }
282  break;
283  case 'customsubcategory':
284  // Custom subCategory label
285  $customSubcategory = explode('=', $keyValPair[1], 2);
286  if (trim($customSubcategory[0])) {
287  $subCategoryKey = strtolower($customSubcategory[0]);
288  $this->subCategories[$subCategoryKey][0] = $this->‪getLanguageService()->‪sL($customSubcategory[1]);
289  }
290  break;
291  }
292  }
293  }
294  }
295  if (isset($editableComments[$const])) {
296  $editableComments[$const]['name'] = $const;
297  $editableComments[$const]['value'] = trim($value);
298  $editableComments[$const]['default_value'] = trim((string)($default[$const] ?? ''));
299  }
300  }
301  return $editableComments;
302  }
303 
307  protected function ‪getLanguageService(): ‪LanguageService
308  {
309  return ‪$GLOBALS['LANG'];
310  }
311 
323  public function ‪getSubCategories(): array
324  {
326  }
327 }
‪TYPO3\CMS\Core\TypoScript\Parser\ConstantConfigurationParser\prepareConfigurationForView
‪array prepareConfigurationForView(array $configuration)
Definition: ConstantConfigurationParser.php:75
‪TYPO3\CMS\Core\Utility\ArrayUtility\flatten
‪static array flatten(array $array, $prefix='', bool $keepDots=false)
Definition: ArrayUtility.php:486
‪TYPO3\CMS\Core\TypoScript\Parser\ConstantConfigurationParser\$subCategories
‪array $subCategories
Definition: ConstantConfigurationParser.php:39
‪TYPO3\CMS\Core\TypoScript\Parser\ConstantConfigurationParser\getConfigurationAsValuedArray
‪array getConfigurationAsValuedArray(string $rawConfiguration)
Definition: ConstantConfigurationParser.php:194
‪TYPO3\CMS\Core\TypoScript\Parser\TypoScriptParser
Definition: TypoScriptParser.php:37
‪TYPO3\CMS\Core\TypoScript\Parser
Definition: ConstantConfigurationParser.php:18
‪TYPO3\CMS\Core\Utility\ArrayUtility\mergeRecursiveWithOverrule
‪static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=true, $includeEmptyValues=true, $enableUnsetFeature=true)
Definition: ArrayUtility.php:654
‪TYPO3\CMS\Core\TypoScript\Parser\ConstantConfigurationParser\parseComments
‪array parseComments($flatSetup, $default=null)
Definition: ConstantConfigurationParser.php:222
‪TYPO3\CMS\Core\Localization\LanguageService\sL
‪string sL($input)
Definition: LanguageService.php:194
‪TYPO3\CMS\Core\TypoScript\Parser\ConstantConfigurationParser\extractInformationForConfigFieldsOfTypeOptions
‪array extractInformationForConfigFieldsOfTypeOptions(array $configurationOption)
Definition: ConstantConfigurationParser.php:137
‪TYPO3\CMS\Core\TypoScript\Parser\ConstantConfigurationParser\getSubCategories
‪array getSubCategories()
Definition: ConstantConfigurationParser.php:322
‪TYPO3\CMS\Core\TypoScript\Parser\ConstantConfigurationParser\buildConfigurationArray
‪array buildConfigurationArray(array $configurationOption)
Definition: ConstantConfigurationParser.php:109
‪TYPO3\CMS\Core\TypoScript\Parser\ConstantConfigurationParser\getLanguageService
‪LanguageService getLanguageService()
Definition: ConstantConfigurationParser.php:306
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static string[] trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
Definition: GeneralUtility.php:1059
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:24
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\TypoScript\Parser\ConstantConfigurationParser
Definition: ConstantConfigurationParser.php:34
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:42
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\TypoScript\Parser\ConstantConfigurationParser\extractInformationForConfigFieldsOfTypeUser
‪array extractInformationForConfigFieldsOfTypeUser(array $configurationOption)
Definition: ConstantConfigurationParser.php:160