| Total Complexity | 77 |
| Total Lines | 359 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like BackendModuleRepository often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use BackendModuleRepository, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | class BackendModuleRepository implements \TYPO3\CMS\Core\SingletonInterface |
||
| 31 | { |
||
| 32 | /** |
||
| 33 | * @var \TYPO3\CMS\Backend\Module\ModuleStorage |
||
| 34 | */ |
||
| 35 | protected $moduleStorage; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Constructs the module menu and gets the Singleton instance of the menu |
||
| 39 | */ |
||
| 40 | public function __construct() |
||
| 41 | { |
||
| 42 | $this->moduleStorage = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Module\ModuleStorage::class); |
||
| 43 | |||
| 44 | $rawData = $this->getRawModuleMenuData(); |
||
| 45 | |||
| 46 | $this->convertRawModuleDataToModuleMenuObject($rawData); |
||
| 47 | $this->createMenuEntriesForTbeModulesExt(); |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * loads all module information in the module storage |
||
| 52 | * |
||
| 53 | * @param array $excludeGroupNames |
||
| 54 | * @return \SplObjectStorage |
||
| 55 | */ |
||
| 56 | public function loadAllowedModules(array $excludeGroupNames = []) |
||
| 57 | { |
||
| 58 | if (empty($excludeGroupNames)) { |
||
| 59 | return $this->moduleStorage->getEntries(); |
||
| 60 | } |
||
| 61 | |||
| 62 | $modules = new \SplObjectStorage(); |
||
| 63 | foreach ($this->moduleStorage->getEntries() as $moduleGroup) { |
||
| 64 | if (!in_array($moduleGroup->getName(), $excludeGroupNames, true)) { |
||
| 65 | if ($moduleGroup->getChildren()->count() > 0 || $moduleGroup->isStandalone()) { |
||
| 66 | $modules->attach($moduleGroup); |
||
| 67 | } |
||
| 68 | } |
||
| 69 | } |
||
| 70 | |||
| 71 | return $modules; |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @param string $groupName |
||
| 76 | * @return \SplObjectStorage|false |
||
| 77 | **/ |
||
| 78 | public function findByGroupName($groupName = '') |
||
| 79 | { |
||
| 80 | foreach ($this->moduleStorage->getEntries() as $moduleGroup) { |
||
| 81 | if ($moduleGroup->getName() === $groupName) { |
||
| 82 | return $moduleGroup; |
||
| 83 | } |
||
| 84 | } |
||
| 85 | |||
| 86 | return false; |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Finds a module menu entry by name |
||
| 91 | * |
||
| 92 | * @param string $name |
||
| 93 | * @return \TYPO3\CMS\Backend\Domain\Model\Module\BackendModule|bool |
||
| 94 | */ |
||
| 95 | public function findByModuleName($name) |
||
| 96 | { |
||
| 97 | $entries = $this->moduleStorage->getEntries(); |
||
| 98 | $entry = $this->findByModuleNameInGivenEntries($name, $entries); |
||
| 99 | return $entry; |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Finds a module menu entry by name in a given storage |
||
| 104 | * |
||
| 105 | * @param string $name |
||
| 106 | * @param \SplObjectStorage $entries |
||
| 107 | * @return \TYPO3\CMS\Backend\Domain\Model\Module\BackendModule|bool |
||
| 108 | */ |
||
| 109 | public function findByModuleNameInGivenEntries($name, \SplObjectStorage $entries) |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Creates the module menu object structure from the raw data array |
||
| 128 | * |
||
| 129 | * @param array $rawModuleData |
||
| 130 | */ |
||
| 131 | protected function convertRawModuleDataToModuleMenuObject(array $rawModuleData) |
||
| 142 | } |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Creates a menu entry object from an array |
||
| 147 | * |
||
| 148 | * @param array $module |
||
| 149 | * @return \TYPO3\CMS\Backend\Domain\Model\Module\BackendModule |
||
| 150 | */ |
||
| 151 | protected function createEntryFromRawData(array $module) |
||
| 152 | { |
||
| 153 | /** @var \TYPO3\CMS\Backend\Domain\Model\Module\BackendModule $entry */ |
||
| 154 | $entry = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Domain\Model\Module\BackendModule::class); |
||
| 155 | if (!empty($module['name']) && is_string($module['name'])) { |
||
| 156 | $entry->setName($module['name']); |
||
| 157 | } |
||
| 158 | if (!empty($module['title']) && is_string($module['title'])) { |
||
| 159 | $entry->setTitle($this->getLanguageService()->sL($module['title'])); |
||
| 160 | } |
||
| 161 | if (!empty($module['onclick']) && is_string($module['onclick'])) { |
||
| 162 | $entry->setOnClick($module['onclick']); |
||
| 163 | } |
||
| 164 | if (!empty($module['link']) && is_string($module['link'])) { |
||
| 165 | $entry->setLink($module['link']); |
||
| 166 | } elseif (empty($module['link']) && !empty($module['path']) && is_string($module['path'])) { |
||
| 167 | $entry->setLink($module['path']); |
||
| 168 | } |
||
| 169 | if (!empty($module['description']) && is_string($module['description'])) { |
||
| 170 | $entry->setDescription($this->getLanguageService()->sL($module['description'])); |
||
| 171 | } |
||
| 172 | if (!empty($module['icon'])) { |
||
| 173 | $entry->setIcon($module['icon']); |
||
| 174 | } |
||
| 175 | if (!empty($module['navigationComponentId']) && is_string($module['navigationComponentId'])) { |
||
| 176 | $entry->setNavigationComponentId($module['navigationComponentId']); |
||
| 177 | } |
||
| 178 | if (!empty($module['navigationFrameScript']) && is_string($module['navigationFrameScript'])) { |
||
| 179 | $entry->setNavigationFrameScript($module['navigationFrameScript']); |
||
| 180 | } elseif (!empty($module['parentNavigationFrameScript']) && is_string($module['parentNavigationFrameScript'])) { |
||
| 181 | $entry->setNavigationFrameScript($module['parentNavigationFrameScript']); |
||
| 182 | } |
||
| 183 | if (!empty($module['navigationFrameScriptParam']) && is_string($module['navigationFrameScriptParam'])) { |
||
| 184 | $entry->setNavigationFrameScriptParameters($module['navigationFrameScriptParam']); |
||
| 185 | } |
||
| 186 | if (!empty($module['standalone'])) { |
||
| 187 | $entry->setStandalone((bool)$module['standalone']); |
||
| 188 | } |
||
| 189 | $moduleMenuState = json_decode($this->getBackendUser()->uc['modulemenu'] ?? '{}', true); |
||
| 190 | $entry->setCollapsed(isset($moduleMenuState[$module['name']])); |
||
| 191 | return $entry; |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Creates the "third level" menu entries (submodules for the info module for |
||
| 196 | * example) from the TBE_MODULES_EXT array |
||
| 197 | */ |
||
| 198 | protected function createMenuEntriesForTbeModulesExt() |
||
| 199 | { |
||
| 200 | foreach ($GLOBALS['TBE_MODULES_EXT'] ?? [] as $mainModule => $tbeModuleExt) { |
||
| 201 | [$main] = explode('_', $mainModule); |
||
| 202 | $mainEntry = $this->findByModuleName($main); |
||
| 203 | if ($mainEntry === false) { |
||
| 204 | continue; |
||
| 205 | } |
||
| 206 | |||
| 207 | $subEntries = $mainEntry->getChildren(); |
||
| 208 | if (empty($subEntries)) { |
||
| 209 | continue; |
||
| 210 | } |
||
| 211 | $matchingSubEntry = $this->findByModuleName($mainModule); |
||
| 212 | if ($matchingSubEntry !== false) { |
||
| 213 | if (isset($tbeModuleExt['MOD_MENU']) && isset($tbeModuleExt['MOD_MENU']['function'])) { |
||
| 214 | foreach ($tbeModuleExt['MOD_MENU']['function'] as $subModule) { |
||
| 215 | $entry = $this->createEntryFromRawData($subModule); |
||
| 216 | $matchingSubEntry->addChild($entry); |
||
| 217 | } |
||
| 218 | } |
||
| 219 | } |
||
| 220 | } |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * loads the module menu from the moduleloader based on $GLOBALS['TBE_MODULES'] |
||
| 225 | * and compiles an array with all the data needed for menu etc. |
||
| 226 | * |
||
| 227 | * @return array |
||
| 228 | */ |
||
| 229 | public function getRawModuleMenuData() |
||
| 230 | { |
||
| 231 | // Loads the backend modules available for the logged in user. |
||
| 232 | /** @var ModuleLoader $moduleLoader */ |
||
| 233 | $moduleLoader = GeneralUtility::makeInstance(ModuleLoader::class); |
||
| 234 | $moduleLoader->observeWorkspaces = true; |
||
| 235 | $moduleLoader->load($GLOBALS['TBE_MODULES']); |
||
| 236 | $loadedModules = $moduleLoader->modules; |
||
| 237 | |||
| 238 | $modules = []; |
||
| 239 | |||
| 240 | // Unset modules that are meant to be hidden from the menu. |
||
| 241 | $loadedModules = $this->removeHiddenModules($loadedModules); |
||
| 242 | $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class); |
||
| 243 | $dummyScript = (string)$uriBuilder->buildUriFromRoute('dummy'); |
||
| 244 | foreach ($loadedModules as $moduleName => $moduleData) { |
||
| 245 | $moduleLink = ''; |
||
| 246 | if (!is_array($moduleData['sub'])) { |
||
| 247 | $moduleLink = $moduleData['script']; |
||
| 248 | } |
||
| 249 | $moduleLink = GeneralUtility::resolveBackPath($moduleLink); |
||
| 250 | $moduleLabels = $moduleLoader->getLabelsForModule($moduleName); |
||
| 251 | $moduleKey = 'modmenu_' . $moduleName; |
||
| 252 | $modules[$moduleKey] = [ |
||
| 253 | 'name' => $moduleName, |
||
| 254 | 'title' => $moduleLabels['title'], |
||
| 255 | 'onclick' => 'top.goToModule(' . GeneralUtility::quoteJSvalue($moduleName) . ');', |
||
| 256 | 'icon' => $this->getModuleIcon($moduleKey, $moduleData), |
||
| 257 | 'link' => $moduleLink, |
||
| 258 | 'description' => $moduleLabels['shortdescription'], |
||
| 259 | 'standalone' => (bool)$moduleData['standalone'] |
||
| 260 | ]; |
||
| 261 | if ((($moduleData['standalone'] ?? false) === false) && !is_array($moduleData['sub']) && $moduleData['script'] !== $dummyScript) { |
||
| 262 | // Work around for modules with own main entry, but being self the only submodule |
||
| 263 | $modules[$moduleKey]['subitems'][$moduleKey] = [ |
||
| 264 | 'name' => $moduleName, |
||
| 265 | 'title' => $moduleLabels['title'], |
||
| 266 | 'onclick' => 'top.goToModule(' . GeneralUtility::quoteJSvalue($moduleName) . ');', |
||
| 267 | 'icon' => $this->getModuleIcon($moduleKey, $moduleData), |
||
| 268 | 'link' => $moduleLink, |
||
| 269 | 'originalLink' => $moduleLink, |
||
| 270 | 'description' => $moduleLabels['shortdescription'], |
||
| 271 | 'navigationFrameScript' => null, |
||
| 272 | 'navigationFrameScriptParam' => null, |
||
| 273 | 'navigationComponentId' => null |
||
| 274 | ]; |
||
| 275 | } elseif (is_array($moduleData['sub'])) { |
||
| 276 | foreach ($moduleData['sub'] as $submoduleName => $submoduleData) { |
||
| 277 | if (isset($submoduleData['script'])) { |
||
| 278 | $submoduleLink = GeneralUtility::resolveBackPath($submoduleData['script']); |
||
| 279 | } else { |
||
| 280 | $submoduleLink = (string)$uriBuilder->buildUriFromRoute($submoduleData['name']); |
||
| 281 | } |
||
| 282 | $submoduleKey = $moduleName . '_' . $submoduleName; |
||
| 283 | $submoduleLabels = $moduleLoader->getLabelsForModule($submoduleKey); |
||
| 284 | $submoduleDescription = $submoduleLabels['shortdescription']; |
||
| 285 | $originalLink = $submoduleLink; |
||
| 286 | $navigationFrameScript = $submoduleData['navFrameScript']; |
||
| 287 | $modules[$moduleKey]['subitems'][$submoduleKey] = [ |
||
| 288 | 'name' => $moduleName . '_' . $submoduleName, |
||
| 289 | 'title' => $submoduleLabels['title'], |
||
| 290 | 'onclick' => 'top.goToModule(' . GeneralUtility::quoteJSvalue($moduleName . '_' . $submoduleName) . ');', |
||
| 291 | 'icon' => $this->getModuleIcon($moduleKey, $submoduleData), |
||
| 292 | 'link' => $submoduleLink, |
||
| 293 | 'originalLink' => $originalLink, |
||
| 294 | 'description' => $submoduleDescription, |
||
| 295 | 'navigationFrameScript' => $navigationFrameScript, |
||
| 296 | 'navigationFrameScriptParam' => $submoduleData['navFrameScriptParam'], |
||
| 297 | 'navigationComponentId' => $submoduleData['navigationComponentId'] |
||
| 298 | ]; |
||
| 299 | // if the main module has a navframe script, inherit to the submodule, |
||
| 300 | // but only if it is not disabled explicitly (option is set to FALSE) |
||
| 301 | if ($moduleData['navFrameScript'] && $submoduleData['inheritNavigationComponentFromMainModule'] !== false) { |
||
| 302 | $modules[$moduleKey]['subitems'][$submoduleKey]['parentNavigationFrameScript'] = $moduleData['navFrameScript']; |
||
| 303 | } |
||
| 304 | } |
||
| 305 | } |
||
| 306 | } |
||
| 307 | return $modules; |
||
| 308 | } |
||
| 309 | |||
| 310 | public function modulesHaveNavigationComponent(): bool |
||
| 311 | { |
||
| 312 | /** @var BackendModule $module */ |
||
| 313 | foreach ($this->moduleStorage->getEntries() as $module) { |
||
| 314 | if ($module->getNavigationComponentId() !== '') { |
||
| 315 | return true; |
||
| 316 | } |
||
| 317 | } |
||
| 318 | |||
| 319 | return false; |
||
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Reads User configuration from options.hideModules and removes |
||
| 324 | * modules accordingly. |
||
| 325 | * |
||
| 326 | * @param array $loadedModules |
||
| 327 | * @return array |
||
| 328 | */ |
||
| 329 | protected function removeHiddenModules($loadedModules) |
||
| 330 | { |
||
| 331 | $userTsConfig = $this->getBackendUser()->getTSConfig(); |
||
| 332 | |||
| 333 | // Hide modules if set in userTS. |
||
| 334 | $hiddenMainModules = GeneralUtility::trimExplode(',', $userTsConfig['options.']['hideModules'] ?? '', true); |
||
| 335 | foreach ($hiddenMainModules as $hiddenMainModule) { |
||
| 336 | unset($loadedModules[$hiddenMainModule]); |
||
| 337 | } |
||
| 338 | |||
| 339 | // Hide sub-modules if set in userTS. |
||
| 340 | $hiddenModules = $userTsConfig['options.']['hideModules.'] ?? []; |
||
| 341 | if (is_array($hiddenModules)) { |
||
| 342 | foreach ($hiddenModules as $mainModuleName => $subModules) { |
||
| 343 | $hiddenSubModules = GeneralUtility::trimExplode(',', $subModules, true); |
||
| 344 | foreach ($hiddenSubModules as $hiddenSubModule) { |
||
| 345 | unset($loadedModules[$mainModuleName]['sub'][$hiddenSubModule]); |
||
| 346 | } |
||
| 347 | } |
||
| 348 | } |
||
| 349 | |||
| 350 | return $loadedModules; |
||
| 351 | } |
||
| 352 | |||
| 353 | /** |
||
| 354 | * gets the module icon |
||
| 355 | * |
||
| 356 | * @param string $moduleKey Module key |
||
| 357 | * @param array $moduleData the compiled data associated with it |
||
| 358 | * @return string Icon data, either sprite or <img> tag |
||
| 359 | */ |
||
| 360 | protected function getModuleIcon($moduleKey, $moduleData) |
||
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Return language service instance |
||
| 375 | * |
||
| 376 | * @return LanguageService |
||
| 377 | */ |
||
| 378 | protected function getLanguageService() |
||
| 381 | } |
||
| 382 | |||
| 383 | /** |
||
| 384 | * @return BackendUserAuthentication |
||
| 385 | */ |
||
| 386 | protected function getBackendUser(): BackendUserAuthentication |
||
| 391 |