‪TYPO3CMS  11.5
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 
21 use TYPO3\CMS\Core\Package\PackageManager;
25 
27 {
29 
30  private PackageManager ‪$packageManager;
31 
32  private string ‪$cacheIdentifier;
33 
35  {
36  $this->cache = ‪$cache;
37  $this->packageManager = ‪$packageManager;
38  $this->cacheIdentifier = ‪$cacheIdentifier;
39  }
40 
45  {
46  $classesConfigurationCache = $this->cache->get($this->cacheIdentifier);
47  if ($classesConfigurationCache !== false) {
48  return new ‪ClassesConfiguration($classesConfigurationCache);
49  }
50 
51  $classes = [];
52  foreach ($this->packageManager->getActivePackages() as $activePackage) {
53  $persistenceClassesFile = $activePackage->getPackagePath() . 'Configuration/Extbase/Persistence/Classes.php';
54  if (file_exists($persistenceClassesFile)) {
55  $definedClasses = require $persistenceClassesFile;
56  if (is_array($definedClasses)) {
58  $classes,
59  $definedClasses,
60  true,
61  false
62  );
63  }
64  }
65  }
66 
67  $classes = $this->‪inheritPropertiesFromParentClasses($classes);
68 
69  $this->cache->set($this->cacheIdentifier, $classes);
70 
71  return new ‪ClassesConfiguration($classes);
72  }
73 
80  private function ‪inheritPropertiesFromParentClasses(array $classes): array
81  {
82  foreach (array_keys($classes) as $className) {
83  if (!isset($classes[$className]['properties'])) {
84  $classes[$className]['properties'] = [];
85  }
86 
87  /*
88  * At first we need to clean the list of parent classes.
89  * This methods is expected to be called for models that either inherit
90  * AbstractEntity or AbstractValueObject, therefore we want to know all
91  * parents of $className until one of these parents.
92  */
93  $relevantParentClasses = [];
94  $parentClasses = class_parents($className) ?: [];
95  while (null !== $parentClass = array_shift($parentClasses)) {
96  if (in_array($parentClass, [AbstractEntity::class, AbstractValueObject::class], true)) {
97  break;
98  }
99 
100  $relevantParentClasses[] = $parentClass;
101  }
102 
103  /*
104  * Once we found all relevant parent classes of $class, we can check their
105  * property configuration and merge theirs with the current one. This is necessary
106  * to get the property configuration of parent classes in the current one to not
107  * miss data in the model later on.
108  */
109  foreach ($relevantParentClasses as $currentClassName) {
110  if (null === $properties = $classes[$currentClassName]['properties'] ?? null) {
111  continue;
112  }
113 
114  // Merge new properties over existing ones.
115  $classes[$className]['properties'] = array_replace_recursive($properties, $classes[$className]['properties'] ?? []);
116  }
117  }
118 
119  return $classes;
120  }
121 }
‪TYPO3\CMS\Extbase\Persistence\ClassesConfigurationFactory\$cacheIdentifier
‪string $cacheIdentifier
Definition: ClassesConfigurationFactory.php:32
‪TYPO3\CMS\Extbase\Persistence\ClassesConfigurationFactory\$packageManager
‪PackageManager $packageManager
Definition: ClassesConfigurationFactory.php:30
‪TYPO3\CMS\Core\Utility\ArrayUtility\mergeRecursiveWithOverrule
‪static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=true, $includeEmptyValues=true, $enableUnsetFeature=true)
Definition: ArrayUtility.php:654
‪TYPO3\CMS\Extbase\Persistence\ClassesConfigurationFactory\$cache
‪FrontendInterface $cache
Definition: ClassesConfigurationFactory.php:28
‪TYPO3\CMS\Extbase\DomainObject\AbstractEntity
Definition: AbstractEntity.php:22
‪TYPO3\CMS\Extbase\Persistence
Definition: ClassesConfiguration.php:18
‪TYPO3\CMS\Extbase\Persistence\ClassesConfigurationFactory\inheritPropertiesFromParentClasses
‪array inheritPropertiesFromParentClasses(array $classes)
Definition: ClassesConfigurationFactory.php:80
‪TYPO3\CMS\Extbase\DomainObject\AbstractValueObject
Definition: AbstractValueObject.php:24
‪TYPO3\CMS\Extbase\Persistence\ClassesConfiguration
Definition: ClassesConfiguration.php:21
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
Definition: FrontendInterface.php:22
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:24
‪TYPO3\CMS\Extbase\Persistence\ClassesConfigurationFactory\__construct
‪__construct(FrontendInterface $cache, PackageManager $packageManager, string $cacheIdentifier)
Definition: ClassesConfigurationFactory.php:34
‪TYPO3\CMS\Extbase\Persistence\ClassesConfigurationFactory\createClassesConfiguration
‪ClassesConfiguration createClassesConfiguration()
Definition: ClassesConfigurationFactory.php:44
‪TYPO3\CMS\Extbase\Persistence\ClassesConfigurationFactory
Definition: ClassesConfigurationFactory.php:27