[+BUGFIX] Extbase (Utility): Improved performance of TypoScript::convertTypoScriptArr...
[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 * @api
32 */
33 class Tx_Extbase_Utility_TypoScript {
34
35 /**
36 * Removes all trailing dots recursively from TS settings array
37 *
38 * Extbase converts the "classical" TypoScript (with trailing dot) to a format without trailing dot,
39 * to be more future-proof and not to have any conflicts with Fluid object accessor syntax.
40 *
41 * @param array $settings The settings array
42 * @return void
43 * @api
44 */
45 public static function convertTypoScriptArrayToPlainArray(array $settings) {
46 foreach ($settings as $key => &$value) {
47 if(substr($key, -1) === '.') {
48 $keyWithoutDot = substr($key, 0, -1);
49 $hasNodeWithoutDot = array_key_exists($keyWithoutDot, $settings);
50 $typoScriptNodeValue = $hasNodeWithoutDot ? $settings[$keyWithoutDot] : NULL;
51 if(is_array($value)) {
52 $settings[$keyWithoutDot] = self::convertTypoScriptArrayToPlainArray($value);
53 if(!is_null($typoScriptNodeValue)) {
54 $settings[$keyWithoutDot]['_typoScriptNodeValue'] = $typoScriptNodeValue;
55 }
56 unset($settings[$key]);
57 } else {
58 $settings[$keyWithoutDot] = NULL;
59 }
60 }
61 }
62 return $settings;
63 }
64
65 /**
66 * Returns an array with Typoscript the old way (with dot).
67 *
68 * Extbase converts the "classical" TypoScript (with trailing dot) to a format without trailing dot,
69 * to be more future-proof and not to have any conflicts with Fluid object accessor syntax.
70 * However, if you want to call legacy TypoScript objects, you somehow need the "old" syntax (because this is what TYPO3 is used to).
71 * With this method, you can convert the extbase TypoScript to classical TYPO3 TypoScript which is understood by the rest of TYPO3.
72 *
73 * @param array $plainArray An Typoscript Array with Extbase Syntax (without dot but with _typoScriptNodeValue)
74 * @return array array with Typoscript as usual (with dot)
75 * @api
76 */
77 public static function convertPlainArrayToTypoScriptArray($plainArray) {
78 $typoScriptArray = array();
79 if (is_array($plainArray)) {
80 foreach ($plainArray as $key => $value) {
81 if (is_array($value)) {
82 if (isset($value['_typoScriptNodeValue'])) {
83 $typoScriptArray[$key] = $value['_typoScriptNodeValue'];
84 unset($value['_typoScriptNodeValue']);
85 }
86 $typoScriptArray[$key.'.'] = Tx_Extbase_Utility_TypoScript::convertPlainArrayToTypoScriptArray($value);
87 } else {
88 $typoScriptArray[$key] = $value;
89 }
90 }
91 }
92 return $typoScriptArray;
93 }
94 }
95 ?>