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