[TASK] Re-work/simplify copyright header in PHP files - Part 8
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Service / TypoScriptService.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Service;
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 * Utilities to manage and convert TypoScript
18 */
19 class TypoScriptService implements \TYPO3\CMS\Core\SingletonInterface {
20
21 /**
22 * Removes all trailing dots recursively from TS settings array
23 *
24 * Extbase converts the "classical" TypoScript (with trailing dot) to a format without trailing dot,
25 * to be more future-proof and not to have any conflicts with Fluid object accessor syntax.
26 *
27 * @param array $typoScriptArray The TypoScript array (e.g. array('foo' => 'TEXT', 'foo.' => array('bar' => 'baz')))
28 * @return array e.g. array('foo' => array('_typoScriptNodeValue' => 'TEXT', 'bar' => 'baz'))
29 * @api
30 */
31 public function convertTypoScriptArrayToPlainArray(array $typoScriptArray) {
32 foreach ($typoScriptArray as $key => $value) {
33 if (substr($key, -1) === '.') {
34 $keyWithoutDot = substr($key, 0, -1);
35 $typoScriptNodeValue = isset($typoScriptArray[$keyWithoutDot]) ? $typoScriptArray[$keyWithoutDot] : NULL;
36 if (is_array($value)) {
37 $typoScriptArray[$keyWithoutDot] = $this->convertTypoScriptArrayToPlainArray($value);
38 if (!is_null($typoScriptNodeValue)) {
39 $typoScriptArray[$keyWithoutDot]['_typoScriptNodeValue'] = $typoScriptNodeValue;
40 }
41 unset($typoScriptArray[$key]);
42 } else {
43 $typoScriptArray[$keyWithoutDot] = NULL;
44 }
45 }
46 }
47 return $typoScriptArray;
48 }
49
50 /**
51 * Returns an array with Typoscript the old way (with dot).
52 *
53 * Extbase converts the "classical" TypoScript (with trailing dot) to a format without trailing dot,
54 * to be more future-proof and not to have any conflicts with Fluid object accessor syntax.
55 * However, if you want to call legacy TypoScript objects, you somehow need the "old" syntax (because this is what TYPO3 is used to).
56 * With this method, you can convert the extbase TypoScript to classical TYPO3 TypoScript which is understood by the rest of TYPO3.
57 *
58 * @param array $plainArray An TypoScript Array with Extbase Syntax (without dot but with _typoScriptNodeValue)
59 * @return array array with TypoScript as usual (with dot)
60 * @api
61 */
62 public function convertPlainArrayToTypoScriptArray(array $plainArray) {
63 $typoScriptArray = array();
64 foreach ($plainArray as $key => $value) {
65 if (is_array($value)) {
66 if (isset($value['_typoScriptNodeValue'])) {
67 $typoScriptArray[$key] = $value['_typoScriptNodeValue'];
68 unset($value['_typoScriptNodeValue']);
69 }
70 $typoScriptArray[$key . '.'] = $this->convertPlainArrayToTypoScriptArray($value);
71 } else {
72 $typoScriptArray[$key] = is_null($value) ? '' : $value;
73 }
74 }
75 return $typoScriptArray;
76 }
77 }