‪TYPO3CMS  10.4
ClassesConfigurationFactory.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 
26 use TYPO3\CMS\Core\Package\PackageManager;
32 
37 {
41  private ‪$cacheFrontend;
42 
46  public function ‪__construct(‪CacheManager $cacheManager = null)
47  {
48  $cacheIdentifier = 'extbase';
49 
50  ‪$cacheFrontend = new ‪NullFrontend($cacheIdentifier);
51  if ($cacheManager !== null) {
52  try {
53  ‪$cacheFrontend = $cacheManager->getCache($cacheIdentifier);
54  } catch (‪NoSuchCacheException $e) {
55  // Handling this exception is not needed as $cacheFrontend is
56  // a NullFrontend at this moment.
57  }
58  }
59 
60  $this->cacheFrontend = ‪$cacheFrontend;
61  }
62 
67  {
68  $cacheEntryIdentifier = 'PersistenceClasses_' . sha1((new ‪Typo3Version())->getVersion() . ‪Environment::getProjectPath());
69 
70  $classesConfigurationCache = $this->cacheFrontend->get($cacheEntryIdentifier);
71  if ($classesConfigurationCache !== false) {
72  return new ‪ClassesConfiguration($classesConfigurationCache);
73  }
74 
75  $classes = [];
76  foreach (GeneralUtility::makeInstance(PackageManager::class)->getActivePackages() as $activePackage) {
77  $persistenceClassesFile = $activePackage->getPackagePath() . 'Configuration/Extbase/Persistence/Classes.php';
78  if (file_exists($persistenceClassesFile)) {
79  $definedClasses = require $persistenceClassesFile;
80  if (is_array($definedClasses)) {
82  $classes,
83  $definedClasses,
84  true,
85  false
86  );
87  }
88  }
89  }
90 
91  $classes = $this->‪inheritPropertiesFromParentClasses($classes);
92 
93  $this->cacheFrontend->set($cacheEntryIdentifier, $classes);
94 
95  return new ‪ClassesConfiguration($classes);
96  }
97 
104  private function ‪inheritPropertiesFromParentClasses(array $classes): array
105  {
106  foreach (array_keys($classes) as $className) {
107  if (!isset($classes[$className]['properties'])) {
108  $classes[$className]['properties'] = [];
109  }
110 
111  /*
112  * At first we need to clean the list of parent classes.
113  * This methods is expected to be called for models that either inherit
114  * AbstractEntity or AbstractValueObject, therefore we want to know all
115  * parents of $className until one of these parents.
116  */
117  $relevantParentClasses = [];
118  $parentClasses = class_parents($className);
119  while (null !== $parentClass = array_shift($parentClasses)) {
120  if (in_array($parentClass, [AbstractEntity::class, AbstractValueObject::class], true)) {
121  break;
122  }
123 
124  $relevantParentClasses[] = $parentClass;
125  }
126 
127  /*
128  * Once we found all relevant parent classes of $class, we can check their
129  * property configuration and merge theirs with the current one. This is necessary
130  * to get the property configuration of parent classes in the current one to not
131  * miss data in the model later on.
132  */
133  foreach ($relevantParentClasses as $currentClassName) {
134  if (null === $properties = $classes[$currentClassName]['properties'] ?? null) {
135  continue;
136  }
137 
138  // Merge new properties over existing ones.
139  $classes[$className]['properties'] = array_replace_recursive($properties, $classes[$className]['properties'] ?? []);
140  }
141  }
142 
143  return $classes;
144  }
145 }
‪TYPO3\CMS\Core\Information\Typo3Version
Definition: Typo3Version.php:21
‪TYPO3\CMS\Core\Utility\ArrayUtility\mergeRecursiveWithOverrule
‪static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=true, $includeEmptyValues=true, $enableUnsetFeature=true)
Definition: ArrayUtility.php:654
‪TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException
Definition: NoSuchCacheException.php:24
‪TYPO3\CMS\Core\Cache\Frontend\NullFrontend
Definition: NullFrontend.php:29
‪TYPO3\CMS\Extbase\DomainObject\AbstractEntity
Definition: AbstractEntity.php:23
‪TYPO3\CMS\Extbase\Persistence
Definition: ClassesConfiguration.php:18
‪TYPO3\CMS\Core\Core\Environment\getProjectPath
‪static string getProjectPath()
Definition: Environment.php:169
‪TYPO3\CMS\Extbase\Persistence\ClassesConfigurationFactory\inheritPropertiesFromParentClasses
‪array inheritPropertiesFromParentClasses(array $classes)
Definition: ClassesConfigurationFactory.php:103
‪TYPO3\CMS\Extbase\DomainObject\AbstractValueObject
Definition: AbstractValueObject.php:24
‪TYPO3\CMS\Core\Cache\CacheManager
Definition: CacheManager.php:35
‪TYPO3\CMS\Extbase\Persistence\ClassesConfiguration
Definition: ClassesConfiguration.php:24
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
Definition: FrontendInterface.php:22
‪TYPO3\CMS\Extbase\Persistence\ClassesConfigurationFactory\__construct
‪__construct(CacheManager $cacheManager=null)
Definition: ClassesConfigurationFactory.php:45
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:24
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:23
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:40
‪TYPO3\CMS\Extbase\Persistence\ClassesConfigurationFactory\$cacheFrontend
‪FrontendInterface $cacheFrontend
Definition: ClassesConfigurationFactory.php:40
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Extbase\Persistence\ClassesConfigurationFactory\createClassesConfiguration
‪ClassesConfiguration createClassesConfiguration()
Definition: ClassesConfigurationFactory.php:65
‪TYPO3\CMS\Extbase\Persistence\ClassesConfigurationFactory
Definition: ClassesConfigurationFactory.php:37