[CLEANUP] Cleanup code in ext:backend/Configuration
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Configuration / TsConfigParser.php
1 <?php
2 namespace TYPO3\CMS\Backend\Configuration;
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 use TYPO3\CMS\Backend\Utility\BackendUtility;
18
19 /**
20 * A TS-Config parsing class which performs condition evaluation
21 *
22 * @author Kraft Bernhard <kraftb@kraftb.at>
23 */
24 class TsConfigParser extends \TYPO3\CMS\Core\TypoScript\Parser\TypoScriptParser {
25
26 /**
27 * @var array
28 */
29 protected $rootLine = array();
30
31 /**
32 * Parses the passed TS-Config using conditions and caching
33 *
34 * @param string $TStext The TSConfig being parsed
35 * @param string $type The type of TSConfig (either "userTS" or "PAGES")
36 * @param int $id The uid of the page being handled
37 * @param array $rootLine The rootline of the page being handled
38 * @return array Array containing the parsed TSConfig and a flag whether the content was retrieved from cache
39 */
40 public function parseTSconfig($TStext, $type, $id = 0, array $rootLine = array()) {
41 $this->type = $type;
42 $this->id = $id;
43 $this->rootLine = $rootLine;
44 $hash = md5($type . ':' . $TStext);
45 $cachedContent = BackendUtility::getHash($hash);
46 if (is_array($cachedContent)) {
47 $storedData = $cachedContent[0];
48 $storedMD5 = $cachedContent[1];
49 $storedData['match'] = array();
50 $storedData = $this->matching($storedData);
51 $checkMD5 = md5(serialize($storedData));
52 if ($checkMD5 == $storedMD5) {
53 $res = array(
54 'TSconfig' => $storedData['TSconfig'],
55 'cached' => 1
56 );
57 } else {
58 $shash = md5($checkMD5 . $hash);
59 $cachedSpec = BackendUtility::getHash($shash);
60 if (is_array($cachedSpec)) {
61 $storedData = $cachedSpec;
62 $res = array(
63 'TSconfig' => $storedData['TSconfig'],
64 'cached' => 1
65 );
66 } else {
67 $storeData = $this->parseWithConditions($TStext);
68 BackendUtility::storeHash($shash, $storeData, $type . '_TSconfig');
69 $res = array(
70 'TSconfig' => $storeData['TSconfig'],
71 'cached' => 0
72 );
73 }
74 }
75 } else {
76 $storeData = $this->parseWithConditions($TStext);
77 $md5 = md5(serialize($storeData));
78 BackendUtility::storeHash($hash, array($storeData, $md5), $type . '_TSconfig');
79 $res = array(
80 'TSconfig' => $storeData['TSconfig'],
81 'cached' => 0
82 );
83 }
84 return $res;
85 }
86
87 /**
88 * Does the actual parsing using the parent objects "parse" method. Creates the match-Object
89 *
90 * @param string $TSconfig The TSConfig being parsed
91 * @return array Array containing the parsed TSConfig, the encountered sectiosn, the matched sections
92 */
93 protected function parseWithConditions($TSconfig) {
94 /** @var $matchObj \TYPO3\CMS\Backend\Configuration\TypoScript\ConditionMatching\ConditionMatcher */
95 $matchObj = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Configuration\TypoScript\ConditionMatching\ConditionMatcher::class);
96 $matchObj->setRootline($this->rootLine);
97 $matchObj->setPageId($this->id);
98 $this->parse($TSconfig, $matchObj);
99 return array(
100 'TSconfig' => $this->setup,
101 'sections' => $this->sections,
102 'match' => $this->sectionsMatch
103 );
104 }
105
106 /**
107 * Is just going through an array of conditions to determine which are matching (for getting correct cache entry)
108 *
109 * @param array $cc An array containing the sections to match
110 * @return array The input array with matching sections filled into the "match" key
111 */
112 protected function matching(array $cc) {
113 if (is_array($cc['sections'])) {
114 /** @var $matchObj \TYPO3\CMS\Backend\Configuration\TypoScript\ConditionMatching\ConditionMatcher */
115 $matchObj = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Configuration\TypoScript\ConditionMatching\ConditionMatcher::class);
116 $matchObj->setRootline($this->rootLine);
117 $matchObj->setPageId($this->id);
118 foreach ($cc['sections'] as $key => $pre) {
119 if ($matchObj->match($pre)) {
120 $cc['match'][$key] = $pre;
121 }
122 }
123 }
124 return $cc;
125 }
126
127 }