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