[!!!][TASK] Remove support for non namespaced classes in Extbase
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Persistence / Generic / Mapper / DataMapFactory.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Persistence\Generic\Mapper;
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\Database\Query\QueryHelper;
18
19 /**
20 * A factory for a data map to map a single table configured in $TCA on a domain object.
21 */
22 class DataMapFactory implements \TYPO3\CMS\Core\SingletonInterface
23 {
24 /**
25 * @var \TYPO3\CMS\Extbase\Reflection\ReflectionService
26 */
27 protected $reflectionService;
28
29 /**
30 * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
31 */
32 protected $configurationManager;
33
34 /**
35 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
36 */
37 protected $objectManager;
38
39 /**
40 * @var \TYPO3\CMS\Core\Cache\CacheManager
41 */
42 protected $cacheManager;
43
44 /**
45 * @var \TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
46 */
47 protected $dataMapCache;
48
49 /**
50 * @param \TYPO3\CMS\Extbase\Reflection\ReflectionService $reflectionService
51 */
52 public function injectReflectionService(\TYPO3\CMS\Extbase\Reflection\ReflectionService $reflectionService)
53 {
54 $this->reflectionService = $reflectionService;
55 }
56
57 /**
58 * @param \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager
59 */
60 public function injectConfigurationManager(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager)
61 {
62 $this->configurationManager = $configurationManager;
63 }
64
65 /**
66 * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager
67 */
68 public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
69 {
70 $this->objectManager = $objectManager;
71 }
72
73 /**
74 * @param \TYPO3\CMS\Core\Cache\CacheManager $cacheManager
75 */
76 public function injectCacheManager(\TYPO3\CMS\Core\Cache\CacheManager $cacheManager)
77 {
78 $this->cacheManager = $cacheManager;
79 }
80
81 /**
82 * Lifecycle method
83 */
84 public function initializeObject()
85 {
86 $this->dataMapCache = $this->cacheManager->getCache('extbase_datamapfactory_datamap');
87 }
88
89 /**
90 * Builds a data map by adding column maps for all the configured columns in the $TCA.
91 * It also resolves the type of values the column is holding and the typo of relation the column
92 * represents.
93 *
94 * @param string $className The class name you want to fetch the Data Map for
95 * @return \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMap The data map
96 */
97 public function buildDataMap($className)
98 {
99 $dataMap = $this->dataMapCache->get(str_replace('\\', '%', $className));
100 if ($dataMap === false) {
101 $dataMap = $this->buildDataMapInternal($className);
102 $this->dataMapCache->set(str_replace('\\', '%', $className), $dataMap);
103 }
104 return $dataMap;
105 }
106
107 /**
108 * Builds a data map by adding column maps for all the configured columns in the $TCA.
109 * It also resolves the type of values the column is holding and the typo of relation the column
110 * represents.
111 *
112 * @param string $className The class name you want to fetch the Data Map for
113 * @throws \TYPO3\CMS\Extbase\Persistence\Generic\Exception\InvalidClassException
114 * @return \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMap The data map
115 */
116 protected function buildDataMapInternal($className)
117 {
118 if (!class_exists($className)) {
119 throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception\InvalidClassException(
120 'Could not find class definition for name "' . $className . '". This could be caused by a mis-spelling of the class name in the class definition.',
121 1476045117
122 );
123 }
124 $recordType = null;
125 $subclasses = [];
126 $tableName = $this->resolveTableName($className);
127 $columnMapping = [];
128 $frameworkConfiguration = $this->configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
129 $classSettings = $frameworkConfiguration['persistence']['classes'][$className];
130 if ($classSettings !== null) {
131 if (isset($classSettings['subclasses']) && is_array($classSettings['subclasses'])) {
132 $subclasses = $this->resolveSubclassesRecursive($frameworkConfiguration['persistence']['classes'], $classSettings['subclasses']);
133 }
134 if (isset($classSettings['mapping']['recordType']) && $classSettings['mapping']['recordType'] !== '') {
135 $recordType = $classSettings['mapping']['recordType'];
136 }
137 if (isset($classSettings['mapping']['tableName']) && $classSettings['mapping']['tableName'] !== '') {
138 $tableName = $classSettings['mapping']['tableName'];
139 }
140 $classHierarchy = array_merge([$className], class_parents($className));
141 foreach ($classHierarchy as $currentClassName) {
142 if (in_array($currentClassName, [\TYPO3\CMS\Extbase\DomainObject\AbstractEntity::class, \TYPO3\CMS\Extbase\DomainObject\AbstractValueObject::class])) {
143 break;
144 }
145 $currentClassSettings = $frameworkConfiguration['persistence']['classes'][$currentClassName];
146 if ($currentClassSettings !== null) {
147 if (isset($currentClassSettings['mapping']['columns']) && is_array($currentClassSettings['mapping']['columns'])) {
148 \TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule($columnMapping, $currentClassSettings['mapping']['columns'], true, false);
149 }
150 }
151 }
152 }
153 /** @var $dataMap \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMap */
154 $dataMap = $this->objectManager->get(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMap::class, $className, $tableName, $recordType, $subclasses);
155 $dataMap = $this->addMetaDataColumnNames($dataMap, $tableName);
156 // $classPropertyNames = $this->reflectionService->getClassPropertyNames($className);
157 $tcaColumnsDefinition = $this->getColumnsDefinition($tableName);
158 \TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule($tcaColumnsDefinition, $columnMapping);
159 // @todo Is this is too powerful?
160
161 foreach ($tcaColumnsDefinition as $columnName => $columnDefinition) {
162 if (isset($columnDefinition['mapOnProperty'])) {
163 $propertyName = $columnDefinition['mapOnProperty'];
164 } else {
165 $propertyName = \TYPO3\CMS\Core\Utility\GeneralUtility::underscoredToLowerCamelCase($columnName);
166 }
167 // if (in_array($propertyName, $classPropertyNames)) {
168 // @todo Enable check for property existence
169 $columnMap = $this->createColumnMap($columnName, $propertyName);
170 $propertyMetaData = $this->reflectionService->getClassSchema($className)->getProperty($propertyName);
171 $columnMap = $this->setType($columnMap, $columnDefinition['config']);
172 $columnMap = $this->setRelations($columnMap, $columnDefinition['config'], $propertyMetaData);
173 $columnMap = $this->setFieldEvaluations($columnMap, $columnDefinition['config']);
174 $dataMap->addColumnMap($columnMap);
175 }
176 return $dataMap;
177 }
178
179 /**
180 * Resolve the table name for the given class name
181 *
182 * @param string $className
183 * @return string The table name
184 */
185 protected function resolveTableName($className)
186 {
187 $className = ltrim($className, '\\');
188 $classNameParts = explode('\\', $className);
189 // Skip vendor and product name for core classes
190 if (strpos($className, 'TYPO3\\CMS\\') === 0) {
191 $classPartsToSkip = 2;
192 } else {
193 $classPartsToSkip = 1;
194 }
195 $tableName = 'tx_' . strtolower(implode('_', array_slice($classNameParts, $classPartsToSkip)));
196
197 return $tableName;
198 }
199
200 /**
201 * Resolves all subclasses for the given set of (sub-)classes.
202 * The whole classes configuration is used to determine all subclasses recursively.
203 *
204 * @param array $classesConfiguration The framework configuration part [persistence][classes].
205 * @param array $subclasses An array of subclasses defined via TypoScript
206 * @return array An numeric array that contains all available subclasses-strings as values.
207 */
208 protected function resolveSubclassesRecursive(array $classesConfiguration, array $subclasses)
209 {
210 $allSubclasses = [];
211 foreach ($subclasses as $subclass) {
212 $allSubclasses[] = $subclass;
213 if (isset($classesConfiguration[$subclass]['subclasses']) && is_array($classesConfiguration[$subclass]['subclasses'])) {
214 $childSubclasses = $this->resolveSubclassesRecursive($classesConfiguration, $classesConfiguration[$subclass]['subclasses']);
215 $allSubclasses = array_merge($allSubclasses, $childSubclasses);
216 }
217 }
218 return $allSubclasses;
219 }
220
221 /**
222 * Returns the TCA ctrl section of the specified table; or NULL if not set
223 *
224 * @param string $tableName An optional table name to fetch the columns definition from
225 * @return array The TCA columns definition
226 */
227 protected function getControlSection($tableName)
228 {
229 return is_array($GLOBALS['TCA'][$tableName]['ctrl']) ? $GLOBALS['TCA'][$tableName]['ctrl'] : null;
230 }
231
232 /**
233 * Returns the TCA columns array of the specified table
234 *
235 * @param string $tableName An optional table name to fetch the columns definition from
236 * @return array The TCA columns definition
237 */
238 protected function getColumnsDefinition($tableName)
239 {
240 return is_array($GLOBALS['TCA'][$tableName]['columns']) ? $GLOBALS['TCA'][$tableName]['columns'] : [];
241 }
242
243 /**
244 * @param DataMap $dataMap
245 * @param string $tableName
246 * @return DataMap
247 */
248 protected function addMetaDataColumnNames(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMap $dataMap, $tableName)
249 {
250 $controlSection = $GLOBALS['TCA'][$tableName]['ctrl'];
251 $dataMap->setPageIdColumnName('pid');
252 if (isset($controlSection['tstamp'])) {
253 $dataMap->setModificationDateColumnName($controlSection['tstamp']);
254 }
255 if (isset($controlSection['crdate'])) {
256 $dataMap->setCreationDateColumnName($controlSection['crdate']);
257 }
258 if (isset($controlSection['cruser_id'])) {
259 $dataMap->setCreatorColumnName($controlSection['cruser_id']);
260 }
261 if (isset($controlSection['delete'])) {
262 $dataMap->setDeletedFlagColumnName($controlSection['delete']);
263 }
264 if (isset($controlSection['languageField'])) {
265 $dataMap->setLanguageIdColumnName($controlSection['languageField']);
266 }
267 if (isset($controlSection['transOrigPointerField'])) {
268 $dataMap->setTranslationOriginColumnName($controlSection['transOrigPointerField']);
269 }
270 if (isset($controlSection['transOrigDiffSourceField'])) {
271 $dataMap->setTranslationOriginDiffSourceName($controlSection['transOrigDiffSourceField']);
272 }
273 if (isset($controlSection['type'])) {
274 $dataMap->setRecordTypeColumnName($controlSection['type']);
275 }
276 if (isset($controlSection['rootLevel'])) {
277 $dataMap->setRootLevel($controlSection['rootLevel']);
278 }
279 if (isset($controlSection['is_static'])) {
280 $dataMap->setIsStatic($controlSection['is_static']);
281 }
282 if (isset($controlSection['enablecolumns']['disabled'])) {
283 $dataMap->setDisabledFlagColumnName($controlSection['enablecolumns']['disabled']);
284 }
285 if (isset($controlSection['enablecolumns']['starttime'])) {
286 $dataMap->setStartTimeColumnName($controlSection['enablecolumns']['starttime']);
287 }
288 if (isset($controlSection['enablecolumns']['endtime'])) {
289 $dataMap->setEndTimeColumnName($controlSection['enablecolumns']['endtime']);
290 }
291 if (isset($controlSection['enablecolumns']['fe_group'])) {
292 $dataMap->setFrontEndUserGroupColumnName($controlSection['enablecolumns']['fe_group']);
293 }
294 return $dataMap;
295 }
296
297 /**
298 * Set the table column type
299 *
300 * @param ColumnMap $columnMap
301 * @param array $columnConfiguration
302 * @return ColumnMap
303 */
304 protected function setType(ColumnMap $columnMap, $columnConfiguration)
305 {
306 $tableColumnType = $columnConfiguration['type'] ?? null;
307 $columnMap->setType(\TYPO3\CMS\Core\DataHandling\TableColumnType::cast($tableColumnType));
308 $tableColumnSubType = $columnConfiguration['internal_type'] ?? null;
309 $columnMap->setInternalType(\TYPO3\CMS\Core\DataHandling\TableColumnSubType::cast($tableColumnSubType));
310
311 return $columnMap;
312 }
313
314 /**
315 * This method tries to determine the type of type of relation to other tables and sets it based on
316 * the $TCA column configuration
317 *
318 * @param ColumnMap $columnMap The column map
319 * @param array|null $columnConfiguration The column configuration from $TCA
320 * @param array $propertyMetaData The property metadata as delivered by the reflection service
321 * @return ColumnMap
322 */
323 protected function setRelations(ColumnMap $columnMap, $columnConfiguration, $propertyMetaData)
324 {
325 if (isset($columnConfiguration)) {
326 if (isset($columnConfiguration['MM'])) {
327 $columnMap = $this->setManyToManyRelation($columnMap, $columnConfiguration);
328 } elseif (isset($propertyMetaData['elementType'])) {
329 $columnMap = $this->setOneToManyRelation($columnMap, $columnConfiguration);
330 } elseif (isset($propertyMetaData['type']) && strpbrk($propertyMetaData['type'], '_\\') !== false) {
331 $columnMap = $this->setOneToOneRelation($columnMap, $columnConfiguration);
332 } elseif (
333 isset($columnConfiguration['type'], $columnConfiguration['renderType'])
334 && $columnConfiguration['type'] === 'select'
335 && (
336 $columnConfiguration['renderType'] !== 'selectSingle'
337 || (isset($columnConfiguration['maxitems']) && $columnConfiguration['maxitems'] > 1)
338 )
339 ) {
340 $columnMap->setTypeOfRelation(ColumnMap::RELATION_HAS_MANY);
341 } elseif (
342 isset($columnConfiguration['type']) && $columnConfiguration['type'] === 'group'
343 && (!isset($columnConfiguration['maxitems']) || $columnConfiguration['maxitems'] > 1)
344 ) {
345 $columnMap->setTypeOfRelation(ColumnMap::RELATION_HAS_MANY);
346 } else {
347 $columnMap->setTypeOfRelation(ColumnMap::RELATION_NONE);
348 }
349 } else {
350 $columnMap->setTypeOfRelation(ColumnMap::RELATION_NONE);
351 }
352 return $columnMap;
353 }
354
355 /**
356 * Sets field evaluations based on $TCA column configuration.
357 *
358 * @param ColumnMap $columnMap The column map
359 * @param array|null $columnConfiguration The column configuration from $TCA
360 * @return ColumnMap
361 */
362 protected function setFieldEvaluations(ColumnMap $columnMap, array $columnConfiguration = null)
363 {
364 if (!empty($columnConfiguration['eval'])) {
365 $fieldEvaluations = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $columnConfiguration['eval'], true);
366 $dateTimeTypes = QueryHelper::getDateTimeTypes();
367
368 if (!empty(array_intersect($dateTimeTypes, $fieldEvaluations)) && !empty($columnConfiguration['dbType'])) {
369 $columnMap->setDateTimeStorageFormat($columnConfiguration['dbType']);
370 }
371 }
372
373 return $columnMap;
374 }
375
376 /**
377 * This method sets the configuration for a 1:1 relation based on
378 * the $TCA column configuration
379 *
380 * @param ColumnMap $columnMap The column map
381 * @param array|null $columnConfiguration The column configuration from $TCA
382 * @return ColumnMap
383 */
384 protected function setOneToOneRelation(ColumnMap $columnMap, array $columnConfiguration = null)
385 {
386 $columnMap->setTypeOfRelation(ColumnMap::RELATION_HAS_ONE);
387 $columnMap->setChildTableName($columnConfiguration['foreign_table']);
388 $columnMap->setChildTableWhereStatement($columnConfiguration['foreign_table_where']);
389 $columnMap->setChildSortByFieldName($columnConfiguration['foreign_sortby']);
390 $columnMap->setParentKeyFieldName($columnConfiguration['foreign_field']);
391 $columnMap->setParentTableFieldName($columnConfiguration['foreign_table_field']);
392 if (is_array($columnConfiguration['foreign_match_fields'])) {
393 $columnMap->setRelationTableMatchFields($columnConfiguration['foreign_match_fields']);
394 }
395 return $columnMap;
396 }
397
398 /**
399 * This method sets the configuration for a 1:n relation based on
400 * the $TCA column configuration
401 *
402 * @param ColumnMap $columnMap The column map
403 * @param array|null $columnConfiguration The column configuration from $TCA
404 * @return ColumnMap
405 */
406 protected function setOneToManyRelation(ColumnMap $columnMap, array $columnConfiguration = null)
407 {
408 $columnMap->setTypeOfRelation(ColumnMap::RELATION_HAS_MANY);
409 $columnMap->setChildTableName($columnConfiguration['foreign_table']);
410 $columnMap->setChildTableWhereStatement($columnConfiguration['foreign_table_where']);
411 $columnMap->setChildSortByFieldName($columnConfiguration['foreign_sortby']);
412 $columnMap->setParentKeyFieldName($columnConfiguration['foreign_field']);
413 $columnMap->setParentTableFieldName($columnConfiguration['foreign_table_field']);
414 if (is_array($columnConfiguration['foreign_match_fields'])) {
415 $columnMap->setRelationTableMatchFields($columnConfiguration['foreign_match_fields']);
416 }
417 return $columnMap;
418 }
419
420 /**
421 * This method sets the configuration for a m:n relation based on
422 * the $TCA column configuration
423 *
424 * @param ColumnMap $columnMap The column map
425 * @param array|null $columnConfiguration The column configuration from $TCA
426 * @throws \TYPO3\CMS\Extbase\Persistence\Generic\Exception\UnsupportedRelationException
427 * @return ColumnMap
428 */
429 protected function setManyToManyRelation(ColumnMap $columnMap, array $columnConfiguration = null)
430 {
431 if (isset($columnConfiguration['MM'])) {
432 $columnMap->setTypeOfRelation(ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY);
433 $columnMap->setChildTableName($columnConfiguration['foreign_table']);
434 $columnMap->setChildTableWhereStatement($columnConfiguration['foreign_table_where']);
435 $columnMap->setRelationTableName($columnConfiguration['MM']);
436 if (is_array($columnConfiguration['MM_match_fields'])) {
437 $columnMap->setRelationTableMatchFields($columnConfiguration['MM_match_fields']);
438 }
439 if (is_array($columnConfiguration['MM_insert_fields'])) {
440 $columnMap->setRelationTableInsertFields($columnConfiguration['MM_insert_fields']);
441 }
442 $columnMap->setRelationTableWhereStatement($columnConfiguration['MM_table_where']);
443 if (!empty($columnConfiguration['MM_opposite_field'])) {
444 $columnMap->setParentKeyFieldName('uid_foreign');
445 $columnMap->setChildKeyFieldName('uid_local');
446 $columnMap->setChildSortByFieldName('sorting_foreign');
447 } else {
448 $columnMap->setParentKeyFieldName('uid_local');
449 $columnMap->setChildKeyFieldName('uid_foreign');
450 $columnMap->setChildSortByFieldName('sorting');
451 }
452 } else {
453 throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception\UnsupportedRelationException('The given information to build a many-to-many-relation was not sufficient. Check your TCA definitions. mm-relations with IRRE must have at least a defined "MM" or "foreign_selector".', 1268817963);
454 }
455 if ($this->getControlSection($columnMap->getRelationTableName()) !== null) {
456 $columnMap->setRelationTablePageIdColumnName('pid');
457 }
458 return $columnMap;
459 }
460
461 /**
462 * Creates the ColumnMap object for the given columnName and propertyName
463 *
464 * @param string $columnName
465 * @param string $propertyName
466 *
467 * @return ColumnMap
468 */
469 protected function createColumnMap($columnName, $propertyName)
470 {
471 return $this->objectManager->get(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap::class, $columnName, $propertyName);
472 }
473 }