[TASK] Remove ext:dbal from installation steps
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Functional / Framework / Frontend / Collector.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Functional\Framework\Frontend;
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 /**
18 * Model of frontend response
19 */
20 class Collector implements \TYPO3\CMS\Core\SingletonInterface
21 {
22 /**
23 * @var array
24 */
25 protected $tableFields;
26
27 /**
28 * @var array
29 */
30 protected $structure = [];
31
32 /**
33 * @var array
34 */
35 protected $structurePaths = [];
36
37 /**
38 * @var array
39 */
40 protected $records = [];
41
42 /**
43 * @var \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
44 */
45 public $cObj;
46
47 public function addRecordData($content, array $configuration = null)
48 {
49 $recordIdentifier = $this->cObj->currentRecord;
50 list($tableName) = explode(':', $recordIdentifier);
51 $currentWatcherValue = $this->getCurrentWatcherValue();
52 $position = strpos($currentWatcherValue, '/' . $recordIdentifier);
53
54 $recordData = $this->filterFields($tableName, $this->cObj->data);
55 $this->records[$recordIdentifier] = $recordData;
56
57 if ($currentWatcherValue === $recordIdentifier) {
58 $this->structure[$recordIdentifier] = $recordData;
59 $this->structurePaths[$recordIdentifier] = [[]];
60 } elseif (!empty($position)) {
61 $levelIdentifier = substr($currentWatcherValue, 0, $position);
62 $this->addToStructure($levelIdentifier, $recordIdentifier, $recordData);
63 }
64 }
65
66 public function addFileData($content, array $configuration = null)
67 {
68 $currentFile = $this->cObj->getCurrentFile();
69
70 if ($currentFile instanceof \TYPO3\CMS\Core\Resource\File) {
71 $tableName = 'sys_file';
72 } elseif ($currentFile instanceof \TYPO3\CMS\Core\Resource\FileReference) {
73 $tableName = 'sys_file_reference';
74 } else {
75 return;
76 }
77
78 $recordData = $this->filterFields($tableName, $currentFile->getProperties());
79 $recordIdentifier = $tableName . ':' . $currentFile->getUid();
80 $this->records[$recordIdentifier] = $recordData;
81
82 $currentWatcherValue = $this->getCurrentWatcherValue();
83 $levelIdentifier = rtrim($currentWatcherValue, '/');
84 $this->addToStructure($levelIdentifier, $recordIdentifier, $recordData);
85 }
86
87 /**
88 * @param string $tableName
89 * @param array $recordData
90 * @return array
91 */
92 protected function filterFields($tableName, array $recordData)
93 {
94 $recordData = array_intersect_key(
95 $recordData,
96 array_flip($this->getTableFields($tableName))
97 );
98 return $recordData;
99 }
100
101 protected function addToStructure($levelIdentifier, $recordIdentifier, array $recordData)
102 {
103 $steps = explode('/', $levelIdentifier);
104 $structurePaths = [];
105 $structure = &$this->structure;
106
107 foreach ($steps as $step) {
108 list($identifier, $fieldName) = explode('.', $step);
109 $structurePaths[] = $identifier;
110 $structurePaths[] = $fieldName;
111 if (!isset($structure[$identifier])) {
112 return;
113 }
114 $structure = &$structure[$identifier];
115 if (!isset($structure[$fieldName]) || !is_array($structure[$fieldName])) {
116 $structure[$fieldName] = [];
117 }
118 $structure = &$structure[$fieldName];
119 }
120
121 $structure[$recordIdentifier] = $recordData;
122 $this->structurePaths[$recordIdentifier][] = $structurePaths;
123 }
124
125 /**
126 * @param string $content
127 * @param NULL|array $configuration
128 * @return void
129 */
130 public function attachSection($content, array $configuration = null)
131 {
132 $section = [
133 'structure' => $this->structure,
134 'structurePaths' => $this->structurePaths,
135 'records' => $this->records,
136 ];
137
138 $as = (!empty($configuration['as']) ? $configuration['as'] : null);
139 $this->getRenderer()->addSection($section, $as);
140 $this->reset();
141 }
142
143 /**
144 * @param string $tableName
145 * @return array
146 */
147 protected function getTableFields($tableName)
148 {
149 if (!isset($this->tableFields) && !empty($this->getFrontendController()->tmpl->setup['config.']['watcher.']['tableFields.'])) {
150 $this->tableFields = $this->getFrontendController()->tmpl->setup['config.']['watcher.']['tableFields.'];
151 foreach ($this->tableFields as &$fieldList) {
152 $fieldList = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $fieldList, true);
153 }
154 unset($fieldList);
155 }
156
157 return (!empty($this->tableFields[$tableName]) ? $this->tableFields[$tableName] : []);
158 }
159
160 /**
161 * @return string
162 */
163 protected function getCurrentWatcherValue()
164 {
165 $watcherValue = null;
166 if (isset($this->getFrontendController()->register['watcher'])) {
167 $watcherValue = $this->getFrontendController()->register['watcher'];
168 }
169 return $watcherValue;
170 }
171
172 /**
173 * @return Renderer
174 */
175 protected function getRenderer()
176 {
177 return \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
178 \TYPO3\CMS\Core\Tests\Functional\Framework\Frontend\Renderer::class
179 );
180 }
181
182 /**
183 * @return \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
184 */
185 protected function getFrontendController()
186 {
187 return $GLOBALS['TSFE'];
188 }
189
190 /**
191 * Collector needs to be reset after attaching a section, otherwise records will pile up.
192 *
193 * @return void
194 */
195 protected function reset()
196 {
197 $this->structure = [];
198 $this->structurePaths = [];
199 $this->records = [];
200 }
201 }