‪TYPO3CMS  10.4
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 {
36  private ‪$tagName;
37 
41  public function ‪__construct(string ‪$tagName)
42  {
43  $this->tagName = ‪$tagName;
44  }
45 
49  public function ‪process(ContainerBuilder $container): void
50  {
51  $widgetRegistryDefinition = $container->findDefinition(WidgetRegistry::class);
52  /* @var Definition|bool $widgetRegistryDefinition */
53  if (!$widgetRegistryDefinition) {
54  return;
55  }
56 
57  foreach ($container->findTaggedServiceIds($this->tagName) as $serviceName => $tags) {
58  $definition = $container->findDefinition($serviceName);
59  $definition->setPublic(true);
60 
61  foreach ($tags as $attributes) {
62  $identifier = $attributes['identifier'] ?? $serviceName;
63  $attributes['identifier'] = $identifier;
64  $attributes['serviceName'] = $serviceName;
65  $attributes = $this->‪convertAttributes($attributes);
66 
67  $configurationServiceName = $this->‪registerWidgetConfigurationService(
68  $container,
69  $identifier,
70  $attributes
71  );
72  $definition->setArgument('$configuration', new Reference($configurationServiceName));
73 
74  $widgetRegistryDefinition->addMethodCall('registerWidget', [$identifier . 'WidgetConfiguration']);
75  }
76  }
77  }
78 
79  private function ‪convertAttributes(array $attributes): array
80  {
81  $attributes = array_merge([
82  'iconIdentifier' => 'content-dashboard',
83  'height' => 'small',
84  'width' => 'small',
85  ], $attributes);
86 
87  if (isset($attributes['groupNames'])) {
88  $attributes['groupNames'] = ‪GeneralUtility::trimExplode(',', $attributes['groupNames'], true);
89  } else {
90  $attributes['groupNames'] = [];
91  }
92 
93  if (isset($attributes['additionalCssClasses'])) {
94  $attributes['additionalCssClasses'] = ‪GeneralUtility::trimExplode(' ', $attributes['additionalCssClasses'], true);
95  } else {
96  $attributes['additionalCssClasses'] = [];
97  }
98 
99  return $attributes;
100  }
101 
102  private function ‪registerWidgetConfigurationService(
103  ContainerBuilder $container,
104  string $widgetIdentifier,
105  array $arguments
106  ): string {
107  $serviceName = $widgetIdentifier . 'WidgetConfiguration';
108 
109  $definition = new Definition(
110  WidgetConfiguration::class,
111  $this->‪adjustArgumentsForDi($arguments)
112  );
113  $definition->setPublic(true);
114  $container->addDefinitions([$serviceName => $definition]);
115 
116  return $serviceName;
117  }
118 
119  private function ‪adjustArgumentsForDi(array $arguments): array
120  {
121  foreach ($arguments as $key => $value) {
122  $arguments['$' . $key] = $value;
123  unset($arguments[$key]);
124  }
125 
126  return $arguments;
127  }
128 }
‪TYPO3\CMS\Dashboard\DependencyInjection\DashboardWidgetPass\$tagName
‪string $tagName
Definition: DashboardWidgetPass.php:35
‪TYPO3\CMS\Dashboard\DependencyInjection
Definition: DashboardWidgetPass.php:18
‪TYPO3\CMS\Dashboard\Widgets\WidgetConfiguration
Definition: WidgetConfiguration.php:21
‪TYPO3\CMS\Dashboard\WidgetRegistry
Definition: WidgetRegistry.php:31
‪TYPO3\CMS\Dashboard\DependencyInjection\DashboardWidgetPass\convertAttributes
‪convertAttributes(array $attributes)
Definition: DashboardWidgetPass.php:78
‪TYPO3\CMS\Dashboard\DependencyInjection\DashboardWidgetPass\__construct
‪__construct(string $tagName)
Definition: DashboardWidgetPass.php:40
‪TYPO3\CMS\Dashboard\DependencyInjection\DashboardWidgetPass\registerWidgetConfigurationService
‪registerWidgetConfigurationService(ContainerBuilder $container, string $widgetIdentifier, array $arguments)
Definition: DashboardWidgetPass.php:101
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static string[] trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
Definition: GeneralUtility.php:1059
‪TYPO3\CMS\Dashboard\DependencyInjection\DashboardWidgetPass
Definition: DashboardWidgetPass.php:32
‪TYPO3\CMS\Dashboard\DependencyInjection\DashboardWidgetPass\process
‪process(ContainerBuilder $container)
Definition: DashboardWidgetPass.php:48
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Dashboard\DependencyInjection\DashboardWidgetPass\adjustArgumentsForDi
‪adjustArgumentsForDi(array $arguments)
Definition: DashboardWidgetPass.php:118