7bc665fd644dc139ffcee95879d9223b6556ecb1
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Utility / TypoScript.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Christian Müller <christian@kitsunet.de>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25 /**
26 * Utilities to manage and convert Typoscript Code
27 *
28 * @package Extbase
29 * @subpackage Utility
30 * @version $ID:$
31 */
32 class Tx_Extbase_Utility_TypoScript {
33
34 /**
35 * Removes all trailing dots recursively from TS settings array
36 * TODO Explain why we remove the dots.
37 *
38 * @param array $setup The settings array
39 * @return void
40 * @api
41 */
42 public static function convertTypoScriptArrayToPlainArray(array $settings) {
43 $processedSettings = array();
44 // TODO Check if the t3lib_div::removeDotsFromTS() fits for this purpose (using rtrim() for removing trailing dots)
45 foreach ($settings as $key => $value) {
46 if (substr($key, -1) === '.') {
47 $keyWithoutDot = substr($key, 0, -1);
48 if (is_array($value)) {
49 $processedSettings[$keyWithoutDot] = self::convertTypoScriptArrayToPlainArray($value);
50 } else {
51 $processedSettings[$keyWithoutDot] = NULL;
52 }
53 if (array_key_exists($keyWithoutDot, $settings)) {
54 $processedSettings[$keyWithoutDot]['_typoScriptNodeValue'] = $settings[$keyWithoutDot];
55 unset($settings[$keyWithoutDot]);
56 }
57 } else {
58 $keyWithDot = $key . '.';
59 if (array_key_exists($keyWithDot, $settings)) {
60 $processedSettings[$key] = self::convertTypoScriptArrayToPlainArray($settings[$keyWithDot]);
61 $processedSettings[$key]['_typoScriptNodeValue'] = $value;
62 unset($settings[$keyWithDot]);
63 } else {
64 $processedSettings[$key] = $value;
65 }
66 }
67 }
68 return $processedSettings;
69 }
70
71 /**
72 * Returns an array with Typoscript the old way (with dot).
73 *
74 * Extbase converts the "classical" TypoScript (with trailing dot) to a format without trailing dot,
75 * to be more future-proof and not to have any conflicts with Fluid object accessor syntax.
76 * However, if you want to call legacy TypoScript objects, you somehow need the "old" syntax (because this is what TYPO3 is used to).
77 * With this method, you can convert the extbase TypoScript to classical TYPO3 TypoScript which is understood by the rest of TYPO3.
78 *
79 * @param array $plainArray An Typoscript Array with Extbase Syntax (without dot but with _typoscriptNodeValue)
80 * @return array array with Typoscript as usual (with dot)
81 * @api
82 */
83 public static function convertPlainArrayToTypoScriptArray($plainArray) {
84 $typoScriptArray = array();
85 if (is_array($plainArray)) {
86 foreach ($plainArray as $key => $value) {
87 if (is_array($value)) {
88 if (isset($value['_typoscriptNodeValue'])) {
89 $typoScriptArray[$key] = $value['_typoscriptNodeValue'];
90 unset($value['_typoscriptNodeValue']);
91 }
92 $typoScriptArray[$key.'.'] = Tx_Extbase_Utility_TypoScript::convertPlainArrayToTypoScriptArray($value);
93 } else {
94 $typoScriptArray[$key] = $value;
95 }
96 }
97 }
98 return $typoScriptArray;
99 }
100 }
101 ?>