b581a159fa16dcefb79a437cbbec9c713271cbe9
[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 * Copyright notice
6 *
7 * (c) 2014 Oliver Hader <oliver.hader@typo3.org>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 /**
28 * Model of frontend response
29 */
30 class Collector implements \TYPO3\CMS\Core\SingletonInterface {
31
32 /**
33 * @var array
34 */
35 protected $tableFields;
36
37 /**
38 * @var array
39 */
40 protected $structure = array();
41
42 /**
43 * @var array
44 */
45 protected $structurePaths = array();
46
47 /**
48 * @var array
49 */
50 protected $records = array();
51
52 /**
53 * @var \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
54 */
55 public $cObj;
56
57 public function addRecordData($content, array $configuration = NULL) {
58 $recordIdentifier = $this->cObj->currentRecord;
59 list($tableName) = explode(':', $recordIdentifier);
60 $currentWatcherValue = $this->getCurrentWatcherValue();
61 $position = strpos($currentWatcherValue, '/' . $recordIdentifier);
62
63 $recordData = $this->filterFields($tableName, $this->cObj->data);
64 $this->records[$recordIdentifier] = $recordData;
65
66 if ($currentWatcherValue === $recordIdentifier) {
67 $this->structure[$recordIdentifier] = $recordData;
68 $this->structurePaths[$recordIdentifier] = array(array());
69 } elseif(!empty($position)) {
70 $levelIdentifier = substr($currentWatcherValue, 0, $position);
71 $this->addToStructure($levelIdentifier, $recordIdentifier, $recordData);
72 }
73 }
74
75 public function addFileData($content, array $configuration = NULL) {
76 $currentFile = $this->cObj->getCurrentFile();
77
78 if ($currentFile instanceof \TYPO3\CMS\Core\Resource\File) {
79 $tableName = 'sys_file';
80 } elseif ($currentFile instanceof \TYPO3\CMS\Core\Resource\FileReference) {
81 $tableName = 'sys_file_reference';
82 } else {
83 return;
84 }
85
86 $recordData = $this->filterFields($tableName, $currentFile->getProperties());
87 $recordIdentifier = $tableName . ':' . $currentFile->getUid();
88 $this->records[$recordIdentifier] = $recordData;
89
90 $currentWatcherValue = $this->getCurrentWatcherValue();
91 $levelIdentifier = rtrim($currentWatcherValue, '/');
92 $this->addToStructure($levelIdentifier, $recordIdentifier, $recordData);
93 }
94
95 /**
96 * @param string $tableName
97 * @param array $recordData
98 * @return array
99 */
100 protected function filterFields($tableName, array $recordData) {
101 $recordData = array_intersect_key(
102 $recordData,
103 array_flip($this->getTableFields($tableName))
104 );
105 return $recordData;
106 }
107
108 protected function addToStructure($levelIdentifier, $recordIdentifier, array $recordData) {
109 $steps = explode('/', $levelIdentifier);
110 $structurePaths = array();
111 $structure = &$this->structure;
112
113 foreach ($steps as $step) {
114 list($identifier, $fieldName) = explode('.', $step);
115 $structurePaths[] = $identifier;
116 $structurePaths[] = $fieldName;
117 if (!isset($structure[$identifier])) {
118 return;
119 }
120 $structure = &$structure[$identifier];
121 if (!isset($structure[$fieldName]) || !is_array($structure[$fieldName])) {
122 $structure[$fieldName] = array();
123 }
124 $structure = &$structure[$fieldName];
125 }
126
127 $structure[$recordIdentifier] = $recordData;
128 $this->structurePaths[$recordIdentifier][] = $structurePaths;
129 }
130
131 /**
132 * @param string $content
133 * @param NULL|array $configuration
134 * @return void
135 */
136 public function attachSection($content, array $configuration = NULL) {
137 $section = array(
138 'structure' => $this->structure,
139 'structurePaths' => $this->structurePaths,
140 'records' => $this->records,
141 );
142
143 $as = (!empty($configuration['as']) ? $configuration['as'] : NULL);
144 $this->getRenderer()->addSection($section, $as);
145 }
146
147 /**
148 * @param string $tableName
149 * @return array
150 */
151 protected function getTableFields($tableName) {
152 if (!isset($this->tableFields) && !empty($this->getFrontendController()->tmpl->setup['config.']['watcher.']['tableFields.'])) {
153 $this->tableFields = $this->getFrontendController()->tmpl->setup['config.']['watcher.']['tableFields.'];
154 foreach ($this->tableFields as &$fieldList) {
155 $fieldList = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $fieldList, TRUE);
156 }
157 unset($fieldList);
158 }
159
160 return (!empty($this->tableFields[$tableName]) ? $this->tableFields[$tableName] : array());
161 }
162
163 /**
164 * @return string
165 */
166 protected function getCurrentWatcherValue() {
167 $watcherValue = NULL;
168 if (isset($this->getFrontendController()->register['watcher'])) {
169 $watcherValue = $this->getFrontendController()->register['watcher'];
170 }
171 return $watcherValue;
172 }
173
174 /**
175 * @return Renderer
176 */
177 protected function getRenderer() {
178 return \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
179 'TYPO3\\CMS\\Core\\Tests\\Functional\\Framework\\Frontend\\Renderer'
180 );
181 }
182
183 /**
184 * @return \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
185 */
186 protected function getFrontendController() {
187 return $GLOBALS['TSFE'];
188 }
189
190 }