‪TYPO3CMS  ‪main
DashboardWidgetPass.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 Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
21 use Symfony\Component\DependencyInjection\ContainerBuilder;
22 use Symfony\Component\DependencyInjection\Definition;
23 use Symfony\Component\DependencyInjection\Reference;
27 
31 final class ‪DashboardWidgetPass implements CompilerPassInterface
32 {
33  public function ‪__construct(private readonly string $tagName) {}
34 
35  public function ‪process(ContainerBuilder $container): void
36  {
37  if (!$container->hasDefinition(WidgetRegistry::class)) {
38  return;
39  }
40  $widgetRegistryDefinition = $container->findDefinition(WidgetRegistry::class);
41 
42  foreach ($container->findTaggedServiceIds($this->tagName) as $serviceName => $tags) {
43  $definition = $container->findDefinition($serviceName);
44  $definition->setPublic(true);
45 
46  foreach ($tags as $attributes) {
47  ‪$identifier = $attributes['identifier'] ?? $serviceName;
48  $attributes['identifier'] = ‪$identifier;
49  $attributes['serviceName'] = $serviceName;
50  $attributes = $this->‪convertAttributes($attributes);
51 
52  $configurationServiceName = $this->‪registerWidgetConfigurationService(
53  $container,
55  $attributes
56  );
57  $definition->setArgument('$configuration', new Reference($configurationServiceName));
58 
59  $widgetRegistryDefinition->addMethodCall('registerWidget', [‪$identifier . 'WidgetConfiguration']);
60  }
61  }
62  }
63 
64  private function ‪convertAttributes(array $attributes): array
65  {
66  $attributes = array_merge([
67  'iconIdentifier' => 'content-dashboard',
68  'height' => 'small',
69  'width' => 'small',
70  ], $attributes);
71 
72  if (isset($attributes['groupNames'])) {
73  $attributes['groupNames'] = ‪GeneralUtility::trimExplode(',', $attributes['groupNames'], true);
74  } else {
75  $attributes['groupNames'] = [];
76  }
77 
78  if (isset($attributes['additionalCssClasses'])) {
79  $attributes['additionalCssClasses'] = ‪GeneralUtility::trimExplode(' ', $attributes['additionalCssClasses'], true);
80  } else {
81  $attributes['additionalCssClasses'] = [];
82  }
83 
84  return $attributes;
85  }
86 
88  ContainerBuilder $container,
89  string $widgetIdentifier,
90  array $arguments
91  ): string {
92  $serviceName = $widgetIdentifier . 'WidgetConfiguration';
93 
94  $definition = new Definition(
95  WidgetConfiguration::class,
96  $this->‪adjustArgumentsForDi($arguments)
97  );
98  $definition->setPublic(true);
99  $container->addDefinitions([$serviceName => $definition]);
100 
101  return $serviceName;
102  }
103 
104  private function ‪adjustArgumentsForDi(array $arguments): array
105  {
106  foreach ($arguments as $key => $value) {
107  $arguments['$' . $key] = $value;
108  unset($arguments[$key]);
109  }
110 
111  return $arguments;
112  }
113 }
‪TYPO3\CMS\Dashboard\DependencyInjection\DashboardWidgetPass\__construct
‪__construct(private readonly string $tagName)
Definition: DashboardWidgetPass.php:33
‪TYPO3\CMS\Dashboard\DependencyInjection
Definition: DashboardWidgetPass.php:18
‪TYPO3\CMS\Dashboard\Widgets\WidgetConfiguration
Definition: WidgetConfiguration.php:21
‪TYPO3\CMS\Dashboard\WidgetRegistry
Definition: WidgetRegistry.php:33
‪TYPO3\CMS\Dashboard\DependencyInjection\DashboardWidgetPass\convertAttributes
‪convertAttributes(array $attributes)
Definition: DashboardWidgetPass.php:64
‪TYPO3\CMS\Dashboard\DependencyInjection\DashboardWidgetPass\registerWidgetConfigurationService
‪registerWidgetConfigurationService(ContainerBuilder $container, string $widgetIdentifier, array $arguments)
Definition: DashboardWidgetPass.php:87
‪TYPO3\CMS\Dashboard\DependencyInjection\DashboardWidgetPass
Definition: DashboardWidgetPass.php:32
‪TYPO3\CMS\Dashboard\DependencyInjection\DashboardWidgetPass\process
‪process(ContainerBuilder $container)
Definition: DashboardWidgetPass.php:35
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Dashboard\DependencyInjection\DashboardWidgetPass\adjustArgumentsForDi
‪adjustArgumentsForDi(array $arguments)
Definition: DashboardWidgetPass.php:104
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static list< string > trimExplode(string $delim, string $string, bool $removeEmptyValues=false, int $limit=0)
Definition: GeneralUtility.php:817
‪TYPO3\CMS\Webhooks\Message\$identifier
‪identifier readonly string $identifier
Definition: FileAddedMessage.php:37