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