‪TYPO3CMS  ‪main
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 
42  {
43  $classesConfigurationCache = $this->cache->get($this->cacheIdentifier);
44  if ($classesConfigurationCache !== false) {
45  return new ‪ClassesConfiguration($classesConfigurationCache);
46  }
47 
48  $classes = [];
49  foreach ($this->packageManager->getActivePackages() as $activePackage) {
50  $persistenceClassesFile = $activePackage->getPackagePath() . 'Configuration/Extbase/Persistence/Classes.php';
51  if (file_exists($persistenceClassesFile)) {
52  $definedClasses = require $persistenceClassesFile;
53  if (is_array($definedClasses)) {
54  ArrayUtility::mergeRecursiveWithOverrule(
55  $classes,
56  $definedClasses,
57  true,
58  false
59  );
60  }
61  }
62  }
63 
64  $classes = $this->‪inheritPropertiesFromParentClasses($classes);
65 
66  $this->cache->set($this->cacheIdentifier, $classes);
67 
68  return new ‪ClassesConfiguration($classes);
69  }
70 
74  private function ‪inheritPropertiesFromParentClasses(array $classes): array
75  {
76  foreach (array_keys($classes) as $className) {
77  if (!isset($classes[$className]['properties'])) {
78  $classes[$className]['properties'] = [];
79  }
80 
81  /*
82  * At first we need to clean the list of parent classes.
83  * This methods is expected to be called for models that either inherit
84  * AbstractEntity or AbstractValueObject, therefore we want to know all
85  * parents of $className until one of these parents.
86  */
87  $relevantParentClasses = [];
88  $parentClasses = class_parents($className) ?: [];
89  while (null !== $parentClass = array_shift($parentClasses)) {
90  if (in_array($parentClass, [AbstractEntity::class, AbstractValueObject::class], true)) {
91  break;
92  }
93 
94  $relevantParentClasses[] = $parentClass;
95  }
96 
97  /*
98  * Once we found all relevant parent classes of $class, we can check their
99  * property configuration and merge theirs with the current one. This is necessary
100  * to get the property configuration of parent classes in the current one to not
101  * miss data in the model later on.
102  */
103  foreach ($relevantParentClasses as $currentClassName) {
104  if (null === $properties = $classes[$currentClassName]['properties'] ?? null) {
105  continue;
106  }
107 
108  // Merge new properties over existing ones.
109  $classes[$className]['properties'] = array_replace_recursive($properties, $classes[$className]['properties'] ?? []);
110  }
111  }
112 
113  return $classes;
114  }
115 }
‪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\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\DomainObject\AbstractValueObject
Definition: AbstractValueObject.php:26
‪TYPO3\CMS\Extbase\Persistence\ClassesConfigurationFactory\inheritPropertiesFromParentClasses
‪inheritPropertiesFromParentClasses(array $classes)
Definition: ClassesConfigurationFactory.php:74
‪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:26
‪TYPO3\CMS\Extbase\Persistence\ClassesConfigurationFactory\__construct
‪__construct(FrontendInterface $cache, PackageManager $packageManager, string $cacheIdentifier)
Definition: ClassesConfigurationFactory.php:34
‪TYPO3\CMS\Extbase\Persistence\ClassesConfigurationFactory\createClassesConfiguration
‪createClassesConfiguration()
Definition: ClassesConfigurationFactory.php:41
‪TYPO3\CMS\Extbase\Persistence\ClassesConfigurationFactory
Definition: ClassesConfigurationFactory.php:27