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

ServiceProvider::configureWidgetGroups()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 4
nop 3
dl 0
loc 14
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;
19
20
use ArrayObject;
21
use Psr\Container\ContainerInterface;
22
use TYPO3\CMS\Core\Core\Environment;
23
use TYPO3\CMS\Core\Information\Typo3Version;
24
use TYPO3\CMS\Core\Package\AbstractServiceProvider;
25
use TYPO3\CMS\Core\Package\PackageManager;
26
use TYPO3\CMS\Core\Utility\GeneralUtility;
27
28
/**
29
 * @internal
30
 */
31
class ServiceProvider extends AbstractServiceProvider
32
{
33
34
    /**
35
     * @inheritDoc
36
     */
37
    protected static function getPackagePath(): string
38
    {
39
        return __DIR__ . '/../';
40
    }
41
42
    /**
43
     * @inheritDoc
44
     */
45
    public function getFactories(): array
46
    {
47
        return [
48
            'dashboard.presets' => [ static::class, 'getDashboardPresets' ],
49
            'dashboard.widgetGroups' => [ static::class, 'getWidgetGroups' ],
50
            'dashboard.widgets' => [ static::class, 'getWidgets' ],
51
        ];
52
    }
53
54
    public function getExtensions(): array
55
    {
56
        return [
57
            DashboardPresetRegistry::class => [ static::class, 'configureDashboardPresetRegistry' ],
58
            WidgetGroupRegistry::class => [ static::class, 'configureWidgetGroupRegistry' ],
59
            'dashboard.presets' => [ static::class, 'configureDashboardPresets' ],
60
            'dashboard.widgetGroups' => [ static::class, 'configureWidgetGroups' ],
61
            'dashboard.widgets' => [ static::class, 'configureWidgets' ]
62
        ] + parent::getExtensions();
63
    }
64
65
    public static function getDashboardPresets(ContainerInterface $container): ArrayObject
66
    {
67
        return new ArrayObject();
68
    }
69
70
    public static function getWidgetGroups(ContainerInterface $container): ArrayObject
71
    {
72
        return new ArrayObject();
73
    }
74
75
    public static function getWidgets(ContainerInterface $container): ArrayObject
76
    {
77
        return new ArrayObject();
78
    }
79
80
    public static function configureDashboardPresetRegistry(
81
        ContainerInterface $container,
82
        DashboardPresetRegistry $dashboardPresetRegistry = null
83
    ): DashboardPresetRegistry {
84
        $dashboardPresetRegistry = $dashboardPresetRegistry ?? self::new($container, DashboardPresetRegistry::class);
85
        $cache = $container->get('cache.core');
86
87
        $cacheIdentifier = 'Dashboard_' . sha1((string)(new Typo3Version()) . Environment::getProjectPath() . 'DashboardPresets');
88
        if ($cache->has($cacheIdentifier)) {
89
            $dashboardPresetsFromPackages = $cache->require($cacheIdentifier);
90
        } else {
91
            $dashboardPresetsFromPackages = $container->get('dashboard.presets')->getArrayCopy();
92
            $cache->set($cacheIdentifier, 'return ' . var_export($dashboardPresetsFromPackages, true) . ';');
93
        }
94
95
        foreach ($dashboardPresetsFromPackages as $identifier => $options) {
96
            $preset = new DashboardPreset(
97
                $identifier,
98
                $options['title'],
99
                $options['description'],
100
                $options['iconIdentifier'],
101
                $options['defaultWidgets'],
102
                $options['showInWizard']
103
            );
104
            $dashboardPresetRegistry->registerDashboardPreset($preset);
105
        }
106
107
        return $dashboardPresetRegistry;
108
    }
109
110
    public static function configureWidgetGroupRegistry(
111
        ContainerInterface $container,
112
        WidgetGroupRegistry $widgetGroupRegistry = null
113
    ): WidgetGroupRegistry {
114
        $widgetGroupRegistry = $widgetGroupRegistry ?? self::new($container, WidgetGroupRegistry::class);
115
        $cache = $container->get('cache.core');
116
117
        $cacheIdentifier = 'Dashboard_' . sha1((string)(new Typo3Version()) . Environment::getProjectPath() . 'WidgetGroups');
118
        if ($cache->has($cacheIdentifier)) {
119
            $widgetGroupsFromPackages = $cache->require($cacheIdentifier);
120
        } else {
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
136
    /**
137
     * @param ContainerInterface $container
138
     * @param ArrayObject $presets
139
     * @return ArrayObject
140
     */
141
    public static function configureDashboardPresets(ContainerInterface $container, ArrayObject $presets): ArrayObject
142
    {
143
        $paths = self::getPathsOfInstalledPackages();
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
158
    /**
159
     * @param ContainerInterface $container
160
     * @param ArrayObject $widgetGroups
161
     * @param string $path supplied when invoked internally through PseudoServiceProvider
162
     * @return ArrayObject
163
     */
164
    public static function configureWidgetGroups(ContainerInterface $container, ArrayObject $widgetGroups, string $path = null): ArrayObject
165
    {
166
        $paths = self::getPathsOfInstalledPackages();
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
180
    /**
181
     * @param ContainerInterface $container
182
     * @param ArrayObject $widgets
183
     * @param string $path supplied when invoked internally through PseudoServiceProvider
184
     * @return ArrayObject
185
     */
186
    public static function configureWidgets(ContainerInterface $container, ArrayObject $widgets, string $path = null): ArrayObject
187
    {
188
        $paths = self::getPathsOfInstalledPackages();
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