‪TYPO3CMS  ‪main
SetRegistry.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 
20 use Psr\Log\LoggerInterface;
21 use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
22 use Symfony\Component\DependencyInjection\Attribute\Autowire;
27 
28 #[Autoconfigure(public: true)]
30 {
32  protected ?array ‪$orderedSets = null;
33 
34  public function ‪__construct(
35  protected ‪DependencyOrderingService $dependencyOrderingService,
36  #[Autowire(expression: 'service("package-dependent-cache-identifier").withPrefix("Sets").toString()')]
37  protected readonly string $cacheIdentifier,
38  #[Autowire(service: 'cache.core')]
39  protected readonly ‪PhpFrontend $cache,
40  #[Autowire(lazy: true)]
41  protected ‪SetCollector $setCollector,
42  protected LoggerInterface $logger,
43  ) {}
44 
51  public function ‪getSets(string ...$setNames): array
52  {
53  return array_values(array_filter(
54  $this->getOrderedSets(),
55  fn(‪SetDefinition $set): bool =>
56  in_array($set->name, $setNames, true) ||
57  $this->hasDependency($setNames, $set->name)
58  ));
59  }
60 
61  public function ‪hasSet(string $setName): bool
62  {
63  return isset($this->getOrderedSets()[$setName]);
64  }
65 
66  public function ‪getSet(string $setName): ?‪SetDefinition
67  {
68  return $this->getOrderedSets()[$setName] ?? null;
69  }
70 
74  protected function getOrderedSets(): array
75  {
76  return $this->‪orderedSets ?? $this->‪getFromCache() ?? $this->computeOrderedSets();
77  }
78 
82  protected function ‪getFromCache(): ?array
83  {
84  if (!$this->cache->has($this->cacheIdentifier)) {
85  return null;
86  }
87  try {
88  $this->‪orderedSets = $this->cache->require($this->cacheIdentifier);
89  } catch (\Error) {
90  return null;
91  }
93  }
94 
98  protected function computeOrderedSets(): array
99  {
100  $tmp = [];
101  $sets = $this->setCollector->getSetDefinitions();
102  foreach ($sets as $set) {
103  foreach ($set->dependencies as $dependencyName) {
104  if (isset($sets[$dependencyName])) {
105  continue;
106  }
107  $this->logger->error('Invalid set "{name}": Missing dependency "{dependency}"', [
108  'name' => $set->name,
109  'dependency' => $dependencyName,
110  ]);
111  continue 2;
112  }
113  $tmp[$set->name] = [
114  'set' => $set,
115  'after' => $set->dependencies,
116  'after-resilient' => $set->optionalDependencies,
117  ];
118  }
119 
120  $this->‪orderedSets = array_map(
121  static fn(array $data): ‪SetDefinition => $data['set'],
122  $this->dependencyOrderingService->orderByDependencies($tmp)
123  );
124  $this->cache->set($this->cacheIdentifier, 'return ' . var_export($this->‪orderedSets, true) . ';');
126  }
127 
128  protected function ‪hasDependency(array $setNames, string $dependency): bool
129  {
130  foreach ($setNames as $setName) {
131  $set = $this->‪getSet($setName);
132  if ($set === null) {
133  continue;
134  }
135 
136  if (in_array($dependency, $set->dependencies, true)) {
137  return true;
138  }
139 
140  if ($this->‪hasDependency($set->dependencies, $dependency)) {
141  return true;
142  }
143  }
144  return false;
145  }
146 
147  #[AsEventListener('typo3-core/set-registry')]
148  public function ‪warmupCaches(‪CacheWarmupEvent $event): void
149  {
150  if ($event->‪hasGroup('system')) {
151  $this->computeOrderedSets();
152  }
153  }
154 }
‪TYPO3\CMS\Core\Site\Set\SetRegistry\hasDependency
‪hasDependency(array $setNames, string $dependency)
Definition: SetRegistry.php:128
‪TYPO3\CMS\Core\Site\Set\SetRegistry\getSets
‪list< SetDefinition > getSets(string ... $setNames)
Definition: SetRegistry.php:51
‪TYPO3\CMS\Core\Site\Set
Definition: InvalidSetException.php:18
‪TYPO3\CMS\Core\Site\Set\SetRegistry\orderedSets
‪array< string, function getFromCache():?array { if(! $this->cache->has( $this->cacheIdentifier)) { return null;} try { $this-> orderedSets
Definition: SetRegistry.php:88
‪TYPO3\CMS\Core\Attribute\AsEventListener
Definition: AsEventListener.php:25
‪TYPO3\CMS\Core\Cache\Frontend\PhpFrontend
Definition: PhpFrontend.php:25
‪TYPO3\CMS\Core\Cache\Event\CacheWarmupEvent
Definition: CacheWarmupEvent.php:24
‪TYPO3\CMS\Core\Site\Set\SetRegistry\getFromCache
‪array< string, getOrderedSets():array { return $this-> orderedSets $this getFromCache() ?? $this -> computeOrderedSets()
‪TYPO3\CMS\Core\Site\Set\SetRegistry
Definition: SetRegistry.php:30
‪TYPO3\CMS\Core\Site\Set\SetRegistry\getSet
‪getSet(string $setName)
Definition: SetRegistry.php:66
‪TYPO3\CMS\Core\Site\Set\SetRegistry\warmupCaches
‪warmupCaches(CacheWarmupEvent $event)
Definition: SetRegistry.php:148
‪TYPO3\CMS\Core\Service\DependencyOrderingService
Definition: DependencyOrderingService.php:32
‪TYPO3\CMS\Core\Site\Set\SetRegistry\$orderedSets
‪array $orderedSets
Definition: SetRegistry.php:32
‪TYPO3\CMS\Core\Site\Set\SetRegistry\hasSet
‪hasSet(string $setName)
Definition: SetRegistry.php:61
‪TYPO3\CMS\Core\Site\Set\SetRegistry\__construct
‪__construct(protected DependencyOrderingService $dependencyOrderingService, #[Autowire(expression:'service("package-dependent-cache-identifier").withPrefix("Sets").toString()')] protected readonly string $cacheIdentifier, #[Autowire(service:'cache.core')] protected readonly PhpFrontend $cache, #[Autowire(lazy:true)] protected SetCollector $setCollector, protected LoggerInterface $logger,)
Definition: SetRegistry.php:34
‪TYPO3\CMS\Core\Site\Set\SetDefinition
Definition: SetDefinition.php:23
‪TYPO3\CMS\Core\Site\Set\SetCollector
Definition: SetCollector.php:24
‪TYPO3\CMS\Core\Cache\Event\CacheWarmupEvent\hasGroup
‪hasGroup(string $group)
Definition: CacheWarmupEvent.php:34