‪TYPO3CMS  ‪main
DataMapFactory.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
28 
34 {
38  protected ‪$reflectionService;
39 
43  protected ‪$configurationManager;
44 
48  protected ‪$cacheManager;
49 
53  protected ‪$dataMapCache;
54 
60  protected ‪$dataMaps = [];
61 
66 
68 
69  protected string ‪$baseCacheIdentifier;
70 
71  public function ‪__construct(
78  ) {
79  $this->reflectionService = ‪$reflectionService;
80  $this->configurationManager = ‪$configurationManager;
81  $this->cacheManager = ‪$cacheManager;
82  $this->dataMapCache = $this->cacheManager->‪getCache('extbase');
83  $this->classesConfiguration = ‪$classesConfiguration;
84  $this->columnMapFactory = ‪$columnMapFactory;
85  $this->baseCacheIdentifier = ‪$baseCacheIdentifier;
86  }
87 
96  public function ‪buildDataMap(string $className): ‪DataMap
97  {
98  $className = ltrim($className, '\\');
99  if (isset($this->dataMaps[$className])) {
100  return $this->dataMaps[$className];
101  }
102  $cacheIdentifierClassName = str_replace('\\', '', $className) . '_';
103  $cacheIdentifier = 'DataMap_' . $cacheIdentifierClassName . ‪$this->baseCacheIdentifier;
104  $dataMap = $this->dataMapCache->get($cacheIdentifier);
105  if ($dataMap === false) {
106  $dataMap = $this->‪buildDataMapInternal($className);
107  $this->dataMapCache->set($cacheIdentifier, $dataMap);
108  }
109  $this->dataMaps[$className] = $dataMap;
110  return $dataMap;
111  }
112 
122  protected function ‪buildDataMapInternal(string $className): DataMap
123  {
124  if (!class_exists($className)) {
125  throw new InvalidClassException(
126  'Could not find class definition for name "' . $className . '". This could be caused by a mis-spelling of the class name in the class definition.',
127  1476045117
128  );
129  }
130  $recordType = null;
131  $subclasses = [];
132  $tableName = $this->‪resolveTableName($className);
133  $fieldNameToPropertyNameMapping = [];
134  if ($this->classesConfiguration->hasClass($className)) {
135  $classSettings = $this->classesConfiguration->getConfigurationFor($className);
136  $subclasses = $this->classesConfiguration->getSubClasses($className);
137  if (isset($classSettings['recordType']) && $classSettings['recordType'] !== '') {
138  $recordType = $classSettings['recordType'];
139  }
140  if (isset($classSettings['tableName']) && $classSettings['tableName'] !== '') {
141  $tableName = $classSettings['tableName'];
142  }
143  foreach ($classSettings['properties'] ?? [] as $propertyName => $propertyDefinition) {
144  $fieldNameToPropertyNameMapping[$propertyDefinition['fieldName']] = $propertyName;
145  }
146  }
147  $dataMap = GeneralUtility::makeInstance(DataMap::class, $className, $tableName, $recordType, $subclasses);
148  $dataMap = $this->‪addMetaDataColumnNames($dataMap, $tableName);
149 
150  foreach ($this->‪getColumnsDefinition($tableName) as $columnName => $columnDefinition) {
151  $propertyName = $fieldNameToPropertyNameMapping[$columnName]
153  $dataMap->addColumnMap(
154  $propertyName,
155  $this->columnMapFactory->create(
156  $columnName,
157  $columnDefinition,
158  $propertyName,
159  $className
160  )
161  );
162  }
163  return $dataMap;
164  }
165 
171  protected function ‪resolveTableName(string $className): string
172  {
173  $className = ltrim($className, '\\');
174  $classNameParts = explode('\\', $className);
175  // Skip vendor and product name for core classes
176  if (str_starts_with($className, 'TYPO3\\CMS\\')) {
177  $classPartsToSkip = 2;
178  } else {
179  $classPartsToSkip = 1;
180  }
181  $tableName = 'tx_' . strtolower(implode('_', array_slice($classNameParts, $classPartsToSkip)));
182 
183  return $tableName;
184  }
185 
192  protected function ‪getColumnsDefinition(string $tableName): array
193  {
194  return is_array(‪$GLOBALS['TCA'][$tableName]['columns'] ?? null) ? ‪$GLOBALS['TCA'][$tableName]['columns'] : [];
195  }
196 
197  protected function ‪addMetaDataColumnNames(DataMap $dataMap, string $tableName): DataMap
198  {
199  $controlSection = ‪$GLOBALS['TCA'][$tableName]['ctrl'] ?? null;
200  if ($controlSection === null) {
201  return $dataMap;
202  }
203  $dataMap->setPageIdColumnName('pid');
204  if (isset($controlSection['tstamp'])) {
205  $dataMap->setModificationDateColumnName($controlSection['tstamp']);
206  }
207  if (isset($controlSection['crdate'])) {
208  $dataMap->setCreationDateColumnName($controlSection['crdate']);
209  }
210  if (isset($controlSection['delete'])) {
211  $dataMap->setDeletedFlagColumnName($controlSection['delete']);
212  }
213  if (isset($controlSection['languageField'])) {
214  $dataMap->setLanguageIdColumnName($controlSection['languageField']);
215  }
216  if (isset($controlSection['transOrigPointerField'])) {
217  $dataMap->setTranslationOriginColumnName($controlSection['transOrigPointerField']);
218  }
219  if (isset($controlSection['transOrigDiffSourceField'])) {
220  $dataMap->setTranslationOriginDiffSourceName($controlSection['transOrigDiffSourceField']);
221  }
222  if (isset($controlSection['type'])) {
223  $dataMap->setRecordTypeColumnName($controlSection['type']);
224  }
225  if (isset($controlSection['rootLevel'])) {
226  $dataMap->setRootLevel($controlSection['rootLevel']);
227  }
228  if (isset($controlSection['is_static'])) {
229  $dataMap->setIsStatic($controlSection['is_static']);
230  }
231  if (isset($controlSection['enablecolumns']['disabled'])) {
232  $dataMap->setDisabledFlagColumnName($controlSection['enablecolumns']['disabled']);
233  }
234  if (isset($controlSection['enablecolumns']['starttime'])) {
235  $dataMap->setStartTimeColumnName($controlSection['enablecolumns']['starttime']);
236  }
237  if (isset($controlSection['enablecolumns']['endtime'])) {
238  $dataMap->setEndTimeColumnName($controlSection['enablecolumns']['endtime']);
239  }
240  if (isset($controlSection['enablecolumns']['fe_group'])) {
241  $dataMap->setFrontEndUserGroupColumnName($controlSection['enablecolumns']['fe_group']);
242  }
243  return $dataMap;
244  }
245 }
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMap\setDeletedFlagColumnName
‪setDeletedFlagColumnName($deletedFlagColumnName)
Definition: DataMap.php:365
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapFactory\$classesConfiguration
‪ClassesConfiguration $classesConfiguration
Definition: DataMapFactory.php:59
‪TYPO3\CMS\Core\Utility\GeneralUtility\underscoredToLowerCamelCase
‪static string underscoredToLowerCamelCase($string)
Definition: GeneralUtility.php:671
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapFactory\$columnMapFactory
‪ColumnMapFactory $columnMapFactory
Definition: DataMapFactory.php:61
‪TYPO3\CMS\Core\Cache\CacheManager\getCache
‪FrontendInterface getCache($identifier)
Definition: CacheManager.php:128
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapFactory\__construct
‪__construct(ReflectionService $reflectionService, ConfigurationManagerInterface $configurationManager, CacheManager $cacheManager, ClassesConfiguration $classesConfiguration, ColumnMapFactory $columnMapFactory, string $baseCacheIdentifier)
Definition: DataMapFactory.php:65
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMapFactory
Definition: ColumnMapFactory.php:33
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMap
Definition: DataMap.php:25
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapFactory\addMetaDataColumnNames
‪addMetaDataColumnNames(DataMap $dataMap, string $tableName)
Definition: DataMapFactory.php:191
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMap\setPageIdColumnName
‪setPageIdColumnName($pageIdColumnName)
Definition: DataMap.php:245
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapFactory\$configurationManager
‪TYPO3 CMS Extbase Configuration ConfigurationManagerInterface $configurationManager
Definition: DataMapFactory.php:41
‪TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
Definition: ConfigurationManagerInterface.php:27
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapFactory\getColumnsDefinition
‪array getColumnsDefinition(string $tableName)
Definition: DataMapFactory.php:186
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMap\setDisabledFlagColumnName
‪setDisabledFlagColumnName($disabledFlagColumnName)
Definition: DataMap.php:385
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapFactory\buildDataMap
‪TYPO3 CMS Extbase Persistence Generic Mapper DataMap buildDataMap(string $className)
Definition: DataMapFactory.php:90
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMap\setIsStatic
‪setIsStatic($isStatic)
Definition: DataMap.php:483
‪TYPO3\CMS\Extbase\Persistence\Generic\Exception\InvalidClassException
Definition: InvalidClassException.php:25
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapFactory
Definition: DataMapFactory.php:34
‪TYPO3\CMS\Extbase\Reflection\ReflectionService
Definition: ReflectionService.php:28
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapFactory\$dataMaps
‪array $dataMaps
Definition: DataMapFactory.php:55
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMap\setStartTimeColumnName
‪setStartTimeColumnName($startTimeColumnName)
Definition: DataMap.php:405
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapFactory\$cacheManager
‪TYPO3 CMS Core Cache CacheManager $cacheManager
Definition: DataMapFactory.php:45
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMap\setFrontEndUserGroupColumnName
‪setFrontEndUserGroupColumnName($frontendUserGroupColumnName)
Definition: DataMap.php:445
‪TYPO3\CMS\Core\Cache\CacheManager
Definition: CacheManager.php:36
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMap\setEndTimeColumnName
‪setEndTimeColumnName($endTimeColumnName)
Definition: DataMap.php:425
‪TYPO3\CMS\Extbase\Persistence\ClassesConfiguration
Definition: ClassesConfiguration.php:21
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapFactory\$baseCacheIdentifier
‪string $baseCacheIdentifier
Definition: DataMapFactory.php:63
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
Definition: FrontendInterface.php:22
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapFactory\resolveTableName
‪string resolveTableName(string $className)
Definition: DataMapFactory.php:165
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMap\setRecordTypeColumnName
‪setRecordTypeColumnName($recordTypeColumnName)
Definition: DataMap.php:465
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:22
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMap\setTranslationOriginColumnName
‪setTranslationOriginColumnName($translationOriginColumnName)
Definition: DataMap.php:285
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapFactory\$dataMapCache
‪FrontendInterface $dataMapCache
Definition: DataMapFactory.php:49
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMap\setModificationDateColumnName
‪setModificationDateColumnName($modificationDateColumnName)
Definition: DataMap.php:325
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapFactory\buildDataMapInternal
‪TYPO3 CMS Extbase Persistence Generic Mapper DataMap buildDataMapInternal(string $className)
Definition: DataMapFactory.php:116
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMap\setCreationDateColumnName
‪setCreationDateColumnName($creationDateColumnName)
Definition: DataMap.php:345
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMap\setTranslationOriginDiffSourceName
‪setTranslationOriginDiffSourceName($translationOriginDiffSourceName)
Definition: DataMap.php:305
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMap\setLanguageIdColumnName
‪setLanguageIdColumnName($languageIdColumnName)
Definition: DataMap.php:265
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapFactory\$reflectionService
‪TYPO3 CMS Extbase Reflection ReflectionService $reflectionService
Definition: DataMapFactory.php:37
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMap\setRootLevel
‪setRootLevel($rootLevel)
Definition: DataMap.php:499