‪TYPO3CMS  ‪main
ServiceProvider.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 
18 namespace ‪TYPO3\CMS\Dashboard;
19 
20 use Psr\Container\ContainerInterface;
25 use TYPO3\CMS\Core\Package\PackageManager;
27 
32 {
33  private const ‪CACHE_IDENTIFIER_PREFIX = 'Dashboard_';
34 
35  protected static function ‪getPackagePath(): string
36  {
37  return __DIR__ . '/../';
38  }
39 
40  protected static function ‪getPackageName(): string
41  {
42  return 'typo3/cms-dashboard';
43  }
44 
45  public function ‪getFactories(): array
46  {
47  return [
48  'dashboard.presets' => ‪self::getDashboardPresets(...),
49  'dashboard.widgetGroups' => ‪self::getWidgetGroups(...),
50  'dashboard.widgets' => ‪self::getWidgets(...),
51  'dashboard.configuration.warmer' => ‪self::getConfigurationWarmer(...),
52  ];
53  }
54 
55  public function ‪getExtensions(): array
56  {
57  return [
58  DashboardPresetRegistry::class => ‪self::configureDashboardPresetRegistry(...),
59  ListenerProvider::class => ‪self::addEventListeners(...),
60  WidgetGroupRegistry::class => ‪self::configureWidgetGroupRegistry(...),
61  'dashboard.presets' => ‪self::configureDashboardPresets(...),
62  'dashboard.widgetGroups' => ‪self::configureWidgetGroups(...),
63  'dashboard.widgets' => ‪self::configureWidgets(...),
64  ] + parent::getExtensions();
65  }
66 
67  public static function ‪getDashboardPresets(ContainerInterface $container): \ArrayObject
68  {
69  return new \ArrayObject();
70  }
71 
72  public static function ‪getWidgetGroups(ContainerInterface $container): \ArrayObject
73  {
74  return new \ArrayObject();
75  }
76 
77  public static function ‪getWidgets(ContainerInterface $container): \ArrayObject
78  {
79  return new \ArrayObject();
80  }
81 
82  public static function ‪configureDashboardPresetRegistry(
83  ContainerInterface $container,
84  ‪DashboardPresetRegistry $dashboardPresetRegistry = null
86  $dashboardPresetRegistry = $dashboardPresetRegistry ?? ‪self::new($container, DashboardPresetRegistry::class);
87  $cache = $container->get('cache.core');
88 
89  $cacheIdentifier = $container->get(PackageDependentCacheIdentifier::class)->withPrefix(self::CACHE_IDENTIFIER_PREFIX . 'Presets')->toString();
90  if (!$dashboardPresetsFromPackages = $cache->require($cacheIdentifier)) {
91  $dashboardPresetsFromPackages = $container->get('dashboard.presets')->getArrayCopy();
92  $cache->set($cacheIdentifier, 'return ' . var_export($dashboardPresetsFromPackages, true) . ';');
93  }
94 
95  foreach ($dashboardPresetsFromPackages as ‪$identifier => $options) {
96  $preset = new ‪DashboardPreset(
98  $options['title'],
99  $options['description'],
100  $options['iconIdentifier'],
101  $options['defaultWidgets'],
102  $options['showInWizard']
103  );
104  $dashboardPresetRegistry->registerDashboardPreset($preset);
105  }
106 
107  return $dashboardPresetRegistry;
108  }
109 
110  public static function ‪configureWidgetGroupRegistry(
111  ContainerInterface $container,
112  ‪WidgetGroupRegistry $widgetGroupRegistry = null
114  $widgetGroupRegistry = $widgetGroupRegistry ?? ‪self::new($container, WidgetGroupRegistry::class);
115  $cache = $container->get('cache.core');
116 
117  $cacheIdentifier = $container->get(PackageDependentCacheIdentifier::class)->withPrefix(self::CACHE_IDENTIFIER_PREFIX . 'WidgetGroups')->toString();
118  if (!$widgetGroupsFromPackages = $cache->require($cacheIdentifier)) {
119  $widgetGroupsFromPackages = $container->get('dashboard.widgetGroups')->getArrayCopy();
120  $cache->set($cacheIdentifier, 'return ' . var_export($widgetGroupsFromPackages, true) . ';');
121  }
122 
123  foreach ($widgetGroupsFromPackages as ‪$identifier => $options) {
124  $group = new ‪WidgetGroup(
126  $options['title']
127  );
128  $widgetGroupRegistry->registerWidgetGroup($group);
129  }
130 
131  return $widgetGroupRegistry;
132  }
133 
134  public static function ‪configureDashboardPresets(ContainerInterface $container, \ArrayObject $presets): \ArrayObject
135  {
137 
138  foreach ($paths as $pathOfPackage) {
139  $dashboardPresetsFileNameForPackage = $pathOfPackage . 'Configuration/Backend/DashboardPresets.php';
140  if (file_exists($dashboardPresetsFileNameForPackage)) {
141  $definedPresetsInPackage = require $dashboardPresetsFileNameForPackage;
142  if (is_array($definedPresetsInPackage)) {
143  $presets->exchangeArray(array_merge($presets->getArrayCopy(), $definedPresetsInPackage));
144  }
145  }
146  }
147 
148  return $presets;
149  }
150 
154  public static function ‪configureWidgetGroups(ContainerInterface $container, \ArrayObject $widgetGroups, string $path = null): \ArrayObject
155  {
157 
158  foreach ($paths as $pathOfPackage) {
159  $widgetGroupsFileNameForPackage = $pathOfPackage . 'Configuration/Backend/DashboardWidgetGroups.php';
160  if (file_exists($widgetGroupsFileNameForPackage)) {
161  $definedGroupsInPackage = require $widgetGroupsFileNameForPackage;
162  if (is_array($definedGroupsInPackage)) {
163  $widgetGroups->exchangeArray(array_merge($widgetGroups->getArrayCopy(), $definedGroupsInPackage));
164  }
165  }
166  }
167  return $widgetGroups;
168  }
169 
173  public static function ‪configureWidgets(ContainerInterface $container, \ArrayObject $widgets, string $path = null): \ArrayObject
174  {
176 
177  foreach ($paths as $pathOfPackage) {
178  $widgetsFileNameForPackage = $pathOfPackage . 'Configuration/Backend/DashboardWidgets.php';
179  if (file_exists($widgetsFileNameForPackage)) {
180  $definedWidgetsInPackage = require $widgetsFileNameForPackage;
181  if (is_array($definedWidgetsInPackage)) {
182  $widgets->exchangeArray(array_merge($widgets->getArrayCopy(), $definedWidgetsInPackage));
183  }
184  }
185  }
186  return $widgets;
187  }
188 
189  protected static function ‪getPathsOfInstalledPackages(): array
190  {
191  $paths = [];
192  $packageManager = GeneralUtility::makeInstance(PackageManager::class);
193 
194  foreach ($packageManager->getActivePackages() as $package) {
195  $paths[] = $package->getPackagePath();
196  }
197 
198  return $paths;
199  }
200 
201  public static function ‪getConfigurationWarmer(ContainerInterface $container): \Closure
202  {
203  $cacheIdentifier = $container->get(PackageDependentCacheIdentifier::class);
204  $presetsCacheIdentifier = $cacheIdentifier->withPrefix(self::CACHE_IDENTIFIER_PREFIX . 'Presets')->toString();
205  $widgetGroupsCacheIdentifier = $cacheIdentifier->withPrefix(self::CACHE_IDENTIFIER_PREFIX . 'WidgetGroups')->toString();
206  return static function (‪CacheWarmupEvent $event) use ($container, $presetsCacheIdentifier, $widgetGroupsCacheIdentifier) {
207  if ($event->hasGroup('system')) {
208  $cache = $container->get('cache.core');
209 
210  $dashboardPresetsFromPackages = $container->get('dashboard.presets')->getArrayCopy();
211  $cache->set($presetsCacheIdentifier, 'return ' . var_export($dashboardPresetsFromPackages, true) . ';');
212 
213  $widgetGroupsFromPackages = $container->get('dashboard.widgetGroups')->getArrayCopy();
214  $cache->set($widgetGroupsCacheIdentifier, 'return ' . var_export($widgetGroupsFromPackages, true) . ';');
215  }
216  };
217  }
218 
219  public static function ‪addEventListeners(ContainerInterface $container, ‪ListenerProvider $listenerProvider): ‪ListenerProvider
220  {
221  $listenerProvider->‪addListener(CacheWarmupEvent::class, 'dashboard.configuration.warmer');
222 
223  return $listenerProvider;
224  }
225 }
‪TYPO3\CMS\Dashboard\ServiceProvider\getConfigurationWarmer
‪static getConfigurationWarmer(ContainerInterface $container)
Definition: ServiceProvider.php:201
‪TYPO3\CMS\Dashboard\DashboardPreset
Definition: DashboardPreset.php:26
‪TYPO3\CMS\Core\Package\AbstractServiceProvider
Definition: AbstractServiceProvider.php:35
‪TYPO3\CMS\Dashboard\WidgetGroupRegistry
Definition: WidgetGroupRegistry.php:26
‪TYPO3\CMS\Dashboard\WidgetGroup
Definition: WidgetGroup.php:26
‪TYPO3\CMS\Dashboard\ServiceProvider\configureDashboardPresetRegistry
‪static configureDashboardPresetRegistry(ContainerInterface $container, DashboardPresetRegistry $dashboardPresetRegistry=null)
Definition: ServiceProvider.php:82
‪TYPO3\CMS\Core\Package\Cache\PackageDependentCacheIdentifier
Definition: PackageDependentCacheIdentifier.php:30
‪TYPO3\CMS\Core\Package\AbstractServiceProvider\new
‪static mixed new(ContainerInterface $container, string $className, array $constructorArguments=[])
Definition: AbstractServiceProvider.php:183
‪TYPO3\CMS\Dashboard\ServiceProvider\CACHE_IDENTIFIER_PREFIX
‪const CACHE_IDENTIFIER_PREFIX
Definition: ServiceProvider.php:33
‪TYPO3\CMS\Dashboard\ServiceProvider\getWidgetGroups
‪static getWidgetGroups(ContainerInterface $container)
Definition: ServiceProvider.php:72
‪TYPO3\CMS\Core\Cache\Event\CacheWarmupEvent
Definition: CacheWarmupEvent.php:24
‪TYPO3\CMS\Dashboard\ServiceProvider\getExtensions
‪getExtensions()
Definition: ServiceProvider.php:55
‪TYPO3\CMS\Dashboard\ServiceProvider\configureDashboardPresets
‪static configureDashboardPresets(ContainerInterface $container, \ArrayObject $presets)
Definition: ServiceProvider.php:134
‪TYPO3\CMS\Dashboard\ServiceProvider\getWidgets
‪static getWidgets(ContainerInterface $container)
Definition: ServiceProvider.php:77
‪TYPO3\CMS\Dashboard\ServiceProvider
Definition: ServiceProvider.php:32
‪TYPO3\CMS\Core\EventDispatcher\ListenerProvider\addListener
‪addListener(string $event, string $service, string $method=null, string $identifier=null)
Definition: ListenerProvider.php:50
‪TYPO3\CMS\Dashboard\ServiceProvider\configureWidgets
‪static configureWidgets(ContainerInterface $container, \ArrayObject $widgets, string $path=null)
Definition: ServiceProvider.php:173
‪TYPO3\CMS\Dashboard\ServiceProvider\getPathsOfInstalledPackages
‪static getPathsOfInstalledPackages()
Definition: ServiceProvider.php:189
‪TYPO3\CMS\Dashboard\ServiceProvider\getDashboardPresets
‪static getDashboardPresets(ContainerInterface $container)
Definition: ServiceProvider.php:67
‪TYPO3\CMS\Dashboard\ServiceProvider\configureWidgetGroupRegistry
‪static configureWidgetGroupRegistry(ContainerInterface $container, WidgetGroupRegistry $widgetGroupRegistry=null)
Definition: ServiceProvider.php:110
‪TYPO3\CMS\Dashboard\ServiceProvider\configureWidgetGroups
‪static configureWidgetGroups(ContainerInterface $container, \ArrayObject $widgetGroups, string $path=null)
Definition: ServiceProvider.php:154
‪TYPO3\CMS\Dashboard\ServiceProvider\getFactories
‪getFactories()
Definition: ServiceProvider.php:45
‪TYPO3\CMS\Dashboard\ServiceProvider\addEventListeners
‪static addEventListeners(ContainerInterface $container, ListenerProvider $listenerProvider)
Definition: ServiceProvider.php:219
‪TYPO3\CMS\Dashboard\ServiceProvider\getPackageName
‪static getPackageName()
Definition: ServiceProvider.php:40
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\EventDispatcher\ListenerProvider
Definition: ListenerProvider.php:30
‪TYPO3\CMS\Dashboard
‪TYPO3\CMS\Dashboard\ServiceProvider\getPackagePath
‪static getPackagePath()
Definition: ServiceProvider.php:35
‪TYPO3\CMS\Dashboard\DashboardPresetRegistry
Definition: DashboardPresetRegistry.php:26
‪TYPO3\CMS\Webhooks\Message\$identifier
‪identifier readonly string $identifier
Definition: FileAddedMessage.php:37