5571452a6ab10a600859e8046b67f173d4b5b650
[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\Modules\AdminPanelModuleInterface;
20 use TYPO3\CMS\Adminpanel\Modules\AdminPanelSubModuleInterface;
21 use TYPO3\CMS\Core\Service\DependencyOrderingService;
22 use TYPO3\CMS\Core\Utility\GeneralUtility;
23
24 /**
25 * Admin Panel Module Loader
26 *
27 * @internal
28 */
29 class ModuleLoader
30 {
31
32 /**
33 * Validates, sorts and initiates the registered modules
34 *
35 * @param array $modules
36 * @param string $type
37 * @return AdminPanelModuleInterface[]|AdminPanelSubModuleInterface[]
38 * @throws \RuntimeException
39 */
40 public function validateSortAndInitializeModules(array $modules, string $type = 'main'): array
41 {
42 if (empty($modules)) {
43 return [];
44 }
45 foreach ($modules as $identifier => $configuration) {
46 if (empty($configuration) || !is_array($configuration)) {
47 throw new \RuntimeException(
48 'Missing configuration for module "' . $identifier . '".',
49 1519490105
50 );
51 }
52 if (!is_string($configuration['module']) ||
53 empty($configuration['module']) ||
54 !class_exists($configuration['module']) ||
55 !is_subclass_of(
56 $configuration['module'],
57 ($type === 'main' ? AdminPanelModuleInterface::class : AdminPanelSubModuleInterface::class)
58 )
59 ) {
60 throw new \RuntimeException(
61 'The module "' .
62 $identifier .
63 '" defines an invalid module class. Ensure the class exists and implements the "' .
64 AdminPanelModuleInterface::class .
65 '".',
66 1519490112
67 );
68 }
69 }
70
71 $orderedModules = GeneralUtility::makeInstance(DependencyOrderingService::class)->orderByDependencies(
72 $modules
73 );
74
75 $moduleInstances = [];
76 foreach ($orderedModules as $module) {
77 $module = GeneralUtility::makeInstance($module['module']);
78 if ($module instanceof AdminPanelSubModuleInterface || ($module instanceof AdminPanelModuleInterface && $module->isEnabled())) {
79 $moduleInstances[] = $module;
80 }
81 }
82 return $moduleInstances;
83 }
84
85 /**
86 * Validates, sorts and initializes sub-modules
87 *
88 * @param array $modules
89 * @return AdminPanelSubModuleInterface[]
90 */
91 public function validateSortAndInitializeSubModules(array $modules): array
92 {
93 return $this->validateSortAndInitializeModules($modules, 'sub');
94 }
95 }