Passed
Branch master (c87ba8)
by Christian
16:02
created

DashboardWidgetPass::adjustArgumentsForDi()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
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\DependencyInjection;
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;
24
use TYPO3\CMS\Core\Utility\GeneralUtility;
25
use TYPO3\CMS\Dashboard\WidgetRegistry;
26
use TYPO3\CMS\Dashboard\Widgets\WidgetConfiguration;
27
28
/**
29
 * @internal
30
 */
31
final class DashboardWidgetPass implements CompilerPassInterface
32
{
33
    /**
34
     * @var string
35
     */
36
    private $tagName;
37
38
    /**
39
     * @param string $tagName
40
     */
41
    public function __construct(string $tagName)
42
    {
43
        $this->tagName = $tagName;
44
    }
45
46
    /**
47
     * @param ContainerBuilder $container
48
     */
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
}
129