Completed
Branch master (d17104)
by Christian
21:20
created

ModuleLoader   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 37
dl 0
loc 64
rs 10
c 0
b 0
f 0
wmc 15

1 Method

Rating   Name   Duplication   Size   Complexity  
C validateSortAndInitializeModules() 0 54 15
1
<?php
2
declare(strict_types = 1);
3
4
namespace TYPO3\CMS\Adminpanel\Service;
5
6
/*
7
 * This file is part of the TYPO3 CMS project.
8
 *
9
 * It is free software; you can redistribute it and/or modify it under
10
 * the terms of the GNU General Public License, either version 2
11
 * of the License, or any later version.
12
 *
13
 * For the full copyright and license information, please read the
14
 * LICENSE.txt file that was distributed with this source code.
15
 *
16
 * The TYPO3 project - inspiring people to share!
17
 */
18
19
use TYPO3\CMS\Adminpanel\Exceptions\InvalidConfigurationException;
20
use TYPO3\CMS\Adminpanel\ModuleApi\ConfigurableInterface;
21
use TYPO3\CMS\Adminpanel\ModuleApi\ModuleInterface;
22
use TYPO3\CMS\Adminpanel\ModuleApi\SubmoduleProviderInterface;
23
use TYPO3\CMS\Core\Service\DependencyOrderingService;
24
use TYPO3\CMS\Core\Utility\GeneralUtility;
25
26
/**
27
 * Admin Panel Module Loader
28
 *
29
 * @internal
30
 */
31
class ModuleLoader
32
{
33
34
    /**
35
     * Validates, sorts and initiates the registered modules
36
     *
37
     * @param array $modules
38
     * @return \TYPO3\CMS\Adminpanel\ModuleApi\ModuleInterface[]
39
     * @throws \RuntimeException
40
     */
41
    public function validateSortAndInitializeModules(array $modules): array
42
    {
43
        if (empty($modules)) {
44
            return [];
45
        }
46
        foreach ($modules as $identifier => $configuration) {
47
            if (empty($configuration) || !is_array($configuration)) {
48
                throw new InvalidConfigurationException(
49
                    'Missing configuration for module "' . $identifier . '".',
50
                    1519490105
51
                );
52
            }
53
            if (!is_string($configuration['module']) ||
54
                empty($configuration['module']) ||
55
                !class_exists($configuration['module']) ||
56
                !is_subclass_of(
57
                    $configuration['module'],
58
                    ModuleInterface::class,
59
                    true
60
                )
61
            ) {
62
                throw new InvalidConfigurationException(
63
                    'The module "' .
64
                    $identifier .
65
                    '" defines an invalid module class. Ensure the class exists and implements the "' .
66
                    ModuleInterface::class .
67
                    '".',
68
                    1519490112
69
                );
70
            }
71
        }
72
73
        $orderedModules = GeneralUtility::makeInstance(DependencyOrderingService::class)->orderByDependencies(
74
            $modules
75
        );
76
77
        $moduleInstances = [];
78
        foreach ($orderedModules as $moduleConfiguration) {
79
            $module = GeneralUtility::makeInstance($moduleConfiguration['module']);
80
            if (
81
                $module instanceof ModuleInterface
82
                && (
83
                    ($module instanceof ConfigurableInterface && $module->isEnabled())
84
                    || !($module instanceof ConfigurableInterface)
85
                )
86
            ) {
87
                $moduleInstances[$module->getIdentifier()] = $module;
88
            }
89
            if ($module instanceof SubmoduleProviderInterface) {
90
                $subModuleInstances = $this->validateSortAndInitializeModules($moduleConfiguration['submodules'] ?? []);
91
                $module->setSubModules($subModuleInstances);
92
            }
93
        }
94
        return $moduleInstances;
95
    }
96
}
97