[TASK] Stabilize / Solidify public API of adminPanel
[Packages/TYPO3.CMS.git] / typo3 / sysext / adminpanel / Classes / Service / ModuleLoader.php
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\Modules\AdminPanelModuleInterface;
21 use TYPO3\CMS\Adminpanel\Modules\AdminPanelSubModuleInterface;
22 use TYPO3\CMS\Core\Service\DependencyOrderingService;
23 use TYPO3\CMS\Core\Utility\GeneralUtility;
24
25 /**
26 * Admin Panel Module Loader
27 *
28 * @internal
29 */
30 class ModuleLoader
31 {
32
33 /**
34 * Validates, sorts and initiates the registered modules
35 *
36 * @param array $modules
37 * @param string $type
38 * @return AdminPanelModuleInterface[]|AdminPanelSubModuleInterface[]
39 * @throws \RuntimeException
40 */
41 public function validateSortAndInitializeModules(array $modules, string $type = 'main'): 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 ($type === 'main' ? AdminPanelModuleInterface::class : AdminPanelSubModuleInterface::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 AdminPanelModuleInterface::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 $module) {
79 $module = GeneralUtility::makeInstance($module['module']);
80 if ($module instanceof AdminPanelSubModuleInterface || ($module instanceof AdminPanelModuleInterface && $module->isEnabled())) {
81 $moduleInstances[] = $module;
82 }
83 }
84 return $moduleInstances;
85 }
86
87 /**
88 * Validates, sorts and initializes sub-modules
89 *
90 * @param array $modules
91 * @return AdminPanelSubModuleInterface[]
92 */
93 public function validateSortAndInitializeSubModules(array $modules): array
94 {
95 return $this->validateSortAndInitializeModules($modules, 'sub');
96 }
97 }