| Total Complexity | 49 | 
| Total Lines | 605 | 
| Duplicated Lines | 0 % | 
| Changes | 3 | ||
| Bugs | 0 | Features | 0 | 
Complex classes like ModuleService 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 ModuleService, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 222 | class ModuleService  | 
            ||
| 223 | { | 
            ||
| 224 | // Components are pieces of user-facing functionality, are managed together in the control panel.  | 
            ||
| 225 | private const COMPONENTS = [  | 
            ||
| 226 | ModuleAnalyticsInterface::class,  | 
            ||
| 227 | ModuleBlockInterface::class,  | 
            ||
| 228 | ModuleChartInterface::class,  | 
            ||
| 229 | ModuleFooterInterface::class,  | 
            ||
| 230 | ModuleHistoricEventsInterface::class,  | 
            ||
| 231 | ModuleLanguageInterface::class,  | 
            ||
| 232 | ModuleListInterface::class,  | 
            ||
| 233 | ModuleMenuInterface::class,  | 
            ||
| 234 | ModuleReportInterface::class,  | 
            ||
| 235 | ModuleSidebarInterface::class,  | 
            ||
| 236 | ModuleTabInterface::class,  | 
            ||
| 237 | ModuleThemeInterface::class,  | 
            ||
| 238 | ];  | 
            ||
| 239 | |||
| 240 | // Components that have access levels.  | 
            ||
| 241 | private const COMPONENTS_WITH_ACCESS = [  | 
            ||
| 242 | ModuleBlockInterface::class,  | 
            ||
| 243 | ModuleChartInterface::class,  | 
            ||
| 244 | ModuleListInterface::class,  | 
            ||
| 245 | ModuleMenuInterface::class,  | 
            ||
| 246 | ModuleReportInterface::class,  | 
            ||
| 247 | ModuleSidebarInterface::class,  | 
            ||
| 248 | ModuleTabInterface::class,  | 
            ||
| 249 | ];  | 
            ||
| 250 | |||
| 251 | // Components that are displayed in a particular order  | 
            ||
| 252 | private const COMPONENTS_WITH_SORT = [  | 
            ||
| 253 | ModuleFooterInterface::class,  | 
            ||
| 254 | ModuleMenuInterface::class,  | 
            ||
| 255 | ModuleSidebarInterface::class,  | 
            ||
| 256 | ModuleTabInterface::class,  | 
            ||
| 257 | ];  | 
            ||
| 258 | |||
| 259 | // Array keys are module names, and should match module names from earlier versions of webtrees.  | 
            ||
| 260 | private const CORE_MODULES = [  | 
            ||
| 261 | 'GEDFact_assistant' => CensusAssistantModule::class,  | 
            ||
| 262 | 'ahnentafel_report' => AhnentafelReportModule::class,  | 
            ||
| 263 | 'ancestors_chart' => AncestorsChartModule::class,  | 
            ||
| 264 | 'batch_update' => BatchUpdateModule::class,  | 
            ||
| 265 | 'bdm_report' => BirthDeathMarriageReportModule::class,  | 
            ||
| 266 | 'bing-webmaster-tools' => BingWebmasterToolsModule::class,  | 
            ||
| 267 | 'birth_report' => BirthReportModule::class,  | 
            ||
| 268 | 'branches_list' => BranchesListModule::class,  | 
            ||
| 269 | 'british-monarchs' => BritishMonarchs::class,  | 
            ||
| 270 | 'british-prime-ministers' => BritishPrimeMinisters::class,  | 
            ||
| 271 | 'british-social-history' => BritishSocialHistory::class,  | 
            ||
| 272 | 'calendar-menu' => CalendarMenuModule::class,  | 
            ||
| 273 | 'cemetery_report' => CemeteryReportModule::class,  | 
            ||
| 274 | 'change_report' => ChangeReportModule::class,  | 
            ||
| 275 | 'charts' => ChartsBlockModule::class,  | 
            ||
| 276 | 'charts-menu' => ChartsMenuModule::class,  | 
            ||
| 277 | 'ckeditor' => CkeditorModule::class,  | 
            ||
| 278 | 'clippings' => ClippingsCartModule::class,  | 
            ||
| 279 | 'clouds' => CloudsTheme::class,  | 
            ||
| 280 | 'colors' => ColorsTheme::class,  | 
            ||
| 281 | 'compact-chart' => CompactTreeChartModule::class,  | 
            ||
| 282 | 'contact-links' => ContactsFooterModule::class,  | 
            ||
| 283 | 'cookie-warning' => CookieWarningModule::class,  | 
            ||
| 284 | 'custom-css-js' => CustomCssJsModule::class,  | 
            ||
| 285 | 'death_report' => DeathReportModule::class,  | 
            ||
| 286 | 'descendancy' => DescendancyModule::class,  | 
            ||
| 287 | 'descendancy_chart' => DescendancyChartModule::class,  | 
            ||
| 288 | 'descendancy_report' => DescendancyReportModule::class,  | 
            ||
| 289 | 'extra_info' => IndividualMetadataModule::class,  | 
            ||
| 290 | 'fab' => FabTheme::class,  | 
            ||
| 291 | 'fact_sources' => FactSourcesReportModule::class,  | 
            ||
| 292 | 'family_book_chart' => FamilyBookChartModule::class,  | 
            ||
| 293 | 'family_group_report' => FamilyGroupReportModule::class,  | 
            ||
| 294 | 'family_list' => FamilyListModule::class,  | 
            ||
| 295 | 'family_nav' => FamilyNavigatorModule::class,  | 
            ||
| 296 | 'fan_chart' => FanChartModule::class,  | 
            ||
| 297 | 'faq' => FrequentlyAskedQuestionsModule::class,  | 
            ||
| 298 | 'gedcom_block' => WelcomeBlockModule::class,  | 
            ||
| 299 | 'gedcom_favorites' => FamilyTreeFavoritesModule::class,  | 
            ||
| 300 | 'gedcom_news' => FamilyTreeNewsModule::class,  | 
            ||
| 301 | 'gedcom_stats' => FamilyTreeStatisticsModule::class,  | 
            ||
| 302 | 'google-analytics' => GoogleAnalyticsModule::class,  | 
            ||
| 303 | 'google-webmaster-tools' => GoogleWebmasterToolsModule::class,  | 
            ||
| 304 | 'hit-counter' => HitCountFooterModule::class,  | 
            ||
| 305 | 'hourglass_chart' => HourglassChartModule::class,  | 
            ||
| 306 | 'html' => HtmlBlockModule::class,  | 
            ||
| 307 | 'individual_ext_report' => IndividualFamiliesReportModule::class,  | 
            ||
| 308 | 'individual_list' => IndividualListModule::class,  | 
            ||
| 309 | 'individual_report' => IndividualReportModule::class,  | 
            ||
| 310 | 'language-af' => LanguageAfrikaans::class,  | 
            ||
| 311 | 'language-ar' => LanguageArabic::class,  | 
            ||
| 312 | 'language-bs' => LanguageBosnian::class,  | 
            ||
| 313 | 'language-bu' => LanguageBulgarian::class,  | 
            ||
| 314 | 'language-ca' => LanguageCatalan::class,  | 
            ||
| 315 | 'language-cs' => LanguageCzech::class,  | 
            ||
| 316 | 'language-da' => LanguageDanish::class,  | 
            ||
| 317 | 'language-de' => LanguageGerman::class,  | 
            ||
| 318 | 'language-dv' => LanguageDivehi::class,  | 
            ||
| 319 | 'language-el' => LanguageGreek::class,  | 
            ||
| 320 | 'language-en-AU' => LanguageEnglishAustralia::class,  | 
            ||
| 321 | 'language-en-GB' => LanguageEnglishGreatBritain::class,  | 
            ||
| 322 | 'language-en-US' => LanguageEnglishUnitedStates::class,  | 
            ||
| 323 | 'language-es' => LanguageSpanish::class,  | 
            ||
| 324 | 'language-et' => LanguageEstonian::class,  | 
            ||
| 325 | 'language-fa' => LanguageFarsi::class,  | 
            ||
| 326 | 'language-fi' => LanguageFinnish::class,  | 
            ||
| 327 | 'language-fo' => LanguageFaroese::class,  | 
            ||
| 328 | 'language-fr' => LanguageFrench::class,  | 
            ||
| 329 | 'language-fr-CA' => LanguageFrenchCanada::class,  | 
            ||
| 330 | 'language-gl' => LanguageGalician::class,  | 
            ||
| 331 | 'language-he' => LanguageHebrew::class,  | 
            ||
| 332 | 'language-hr' => LanguageCroatian::class,  | 
            ||
| 333 | 'language-hu' => LanguageHungarian::class,  | 
            ||
| 334 | 'language-id' => LanguageIndonesian::class,  | 
            ||
| 335 | 'language-is' => LanguageIcelandic::class,  | 
            ||
| 336 | 'language-it' => LanguageItalian::class,  | 
            ||
| 337 | 'language-ja' => LanguageJapanese::class,  | 
            ||
| 338 | 'language-jv' => LanguageJavanese::class,  | 
            ||
| 339 | 'language-ka' => LanguageGeorgian::class,  | 
            ||
| 340 | 'language-kk' => LanguageKazhak::class,  | 
            ||
| 341 | 'language-ko' => LanguageKorean::class,  | 
            ||
| 342 | 'language-ln' => LanguageLingala::class,  | 
            ||
| 343 | 'language-lt' => LanguageLithuanian::class,  | 
            ||
| 344 | 'language-lv' => LanguageLatvian::class,  | 
            ||
| 345 | 'language-mi' => LanguageMaori::class,  | 
            ||
| 346 | 'language-mr' => LanguageMarathi::class,  | 
            ||
| 347 | 'language-ms' => LanguageMalay::class,  | 
            ||
| 348 | 'language-nb' => LanguageNorwegianBokmal::class,  | 
            ||
| 349 | 'language-ne' => LanguageNepalese::class,  | 
            ||
| 350 | 'language-nl' => LanguageDutch::class,  | 
            ||
| 351 | 'language-nn' => LanguageNorwegianNynorsk::class,  | 
            ||
| 352 | 'language-oc' => LanguageOccitan::class,  | 
            ||
| 353 | 'language-pl' => LanguagePolish::class,  | 
            ||
| 354 | 'language-pt' => LanguagePortuguese::class,  | 
            ||
| 355 | 'language-pt-BR' => LanguagePortugueseBrazil::class,  | 
            ||
| 356 | 'language-ro' => LanguageRomanian::class,  | 
            ||
| 357 | 'language-ru' => LanguageRussian::class,  | 
            ||
| 358 | 'language-sk' => LanguageSlovakian::class,  | 
            ||
| 359 | 'language-sl' => LanguageSlovenian::class,  | 
            ||
| 360 | 'language-sq' => LanguageAlbanian::class,  | 
            ||
| 361 | 'language-sr' => LanguageSerbian::class,  | 
            ||
| 362 | 'language-sr-Latn' => LanguageSerbianLatin::class,  | 
            ||
| 363 | 'language-su' => LanguageSundanese::class,  | 
            ||
| 364 | 'language-sv' => LanguageSwedish::class,  | 
            ||
| 365 | 'language-sw' => LanguageSwahili::class,  | 
            ||
| 366 | 'language-ta' => LanguageTamil::class,  | 
            ||
| 367 | 'language-th' => LanguageThai::class,  | 
            ||
| 368 | 'language-tr' => LanguageTurkish::class,  | 
            ||
| 369 | 'language-tt' => LanguageTatar::class,  | 
            ||
| 370 | 'language-uk' => LanguageUkranian::class,  | 
            ||
| 371 | 'language-vi' => LanguageVietnamese::class,  | 
            ||
| 372 | 'language-yi' => LanguageYiddish::class,  | 
            ||
| 373 | 'language-zh-Hans' => LanguageChineseSimplified::class,  | 
            ||
| 374 | 'language-zh-Hant' => LanguageChineseTraditional::class,  | 
            ||
| 375 | 'lifespans_chart' => LifespansChartModule::class,  | 
            ||
| 376 | 'lightbox' => AlbumModule::class,  | 
            ||
| 377 | 'lists-menu' => ListsMenuModule::class,  | 
            ||
| 378 | 'logged_in' => LoggedInUsersModule::class,  | 
            ||
| 379 | 'login_block' => LoginBlockModule::class,  | 
            ||
| 380 | 'marriage_report' => MarriageReportModule::class,  | 
            ||
| 381 | 'matomo-analytics' => MatomoAnalyticsModule::class,  | 
            ||
| 382 | 'media' => MediaTabModule::class,  | 
            ||
| 383 | 'media_list' => MediaListModule::class,  | 
            ||
| 384 | 'minimal' => MinimalTheme::class,  | 
            ||
| 385 | 'missing_facts_report' => MissingFactsReportModule::class,  | 
            ||
| 386 | 'notes' => NotesTabModule::class,  | 
            ||
| 387 | 'note_list' => NoteListModule::class,  | 
            ||
| 388 | 'occupation_report' => OccupationReportModule::class,  | 
            ||
| 389 | 'pedigree-map' => PedigreeMapModule::class,  | 
            ||
| 390 | 'pedigree_chart' => PedigreeChartModule::class,  | 
            ||
| 391 | 'pedigree_report' => PedigreeReportModule::class,  | 
            ||
| 392 | 'personal_facts' => IndividualFactsTabModule::class,  | 
            ||
| 393 | 'places' => PlacesModule::class,  | 
            ||
| 394 | 'places_list' => PlaceHierarchyListModule::class,  | 
            ||
| 395 | 'powered-by-webtrees' => PoweredByWebtreesModule::class,  | 
            ||
| 396 | 'random_media' => SlideShowModule::class,  | 
            ||
| 397 | 'recent_changes' => RecentChangesModule::class,  | 
            ||
| 398 | 'relationships_chart' => RelationshipsChartModule::class,  | 
            ||
| 399 | 'relative_ext_report' => RelatedIndividualsReportModule::class,  | 
            ||
| 400 | 'relatives' => RelativesTabModule::class,  | 
            ||
| 401 | 'reports-menu' => ReportsMenuModule::class,  | 
            ||
| 402 | 'repository_list' => RepositoryListModule::class,  | 
            ||
| 403 | 'review_changes' => ReviewChangesModule::class,  | 
            ||
| 404 | 'search-menu' => SearchMenuModule::class,  | 
            ||
| 405 | 'sitemap' => SiteMapModule::class,  | 
            ||
| 406 | 'source_list' => SourceListModule::class,  | 
            ||
| 407 | 'sources_tab' => SourcesTabModule::class,  | 
            ||
| 408 | 'statcounter' => StatcounterModule::class,  | 
            ||
| 409 | 'statistics_chart' => StatisticsChartModule::class,  | 
            ||
| 410 | 'stories' => StoriesModule::class,  | 
            ||
| 411 | 'theme_select' => ThemeSelectModule::class,  | 
            ||
| 412 | 'timeline_chart' => TimelineChartModule::class,  | 
            ||
| 413 | 'todays_events' => OnThisDayModule::class,  | 
            ||
| 414 | 'todo' => ResearchTaskModule::class,  | 
            ||
| 415 | 'top10_givnnames' => TopGivenNamesModule::class,  | 
            ||
| 416 | 'top10_pageviews' => TopPageViewsModule::class,  | 
            ||
| 417 | 'top10_surnames' => TopSurnamesModule::class,  | 
            ||
| 418 | 'tree' => InteractiveTreeModule::class,  | 
            ||
| 419 | 'trees-menu' => TreesMenuModule::class,  | 
            ||
| 420 | 'upcoming_events' => UpcomingAnniversariesModule::class,  | 
            ||
| 421 | 'us-presidents' => USPresidents::class,  | 
            ||
| 422 | 'user_blog' => UserJournalModule::class,  | 
            ||
| 423 | 'user_favorites' => UserFavoritesModule::class,  | 
            ||
| 424 | 'user_messages' => UserMessagesModule::class,  | 
            ||
| 425 | 'user_welcome' => UserWelcomeModule::class,  | 
            ||
| 426 | 'webtrees' => WebtreesTheme::class,  | 
            ||
| 427 | 'xenea' => XeneaTheme::class,  | 
            ||
| 428 | 'yahrzeit' => YahrzeitModule::class,  | 
            ||
| 429 | ];  | 
            ||
| 430 | |||
| 431 | /**  | 
            ||
| 432 | * A function to convert modules into their titles - to create option lists, etc.  | 
            ||
| 433 | *  | 
            ||
| 434 | * @return Closure  | 
            ||
| 435 | */  | 
            ||
| 436 | public function titleMapper(): Closure  | 
            ||
| 440 | };  | 
            ||
| 441 | }  | 
            ||
| 442 | |||
| 443 | /**  | 
            ||
| 444 | * Modules which (a) provide a specific function and (b) we have permission to see.  | 
            ||
| 445 | *  | 
            ||
| 446 | * @param string $interface  | 
            ||
| 447 | * @param Tree $tree  | 
            ||
| 448 | * @param UserInterface $user  | 
            ||
| 449 | *  | 
            ||
| 450 | * @return Collection  | 
            ||
| 451 | */  | 
            ||
| 452 | public function findByComponent(string $interface, Tree $tree, UserInterface $user): Collection  | 
            ||
| 453 |     { | 
            ||
| 454 | return $this->findByInterface($interface, false, true)  | 
            ||
| 455 |             ->filter(static function (ModuleInterface $module) use ($interface, $tree, $user): bool { | 
            ||
| 456 | return $module->accessLevel($tree, $interface) >= Auth::accessLevel($tree, $user);  | 
            ||
| 457 | });  | 
            ||
| 458 | }  | 
            ||
| 459 | |||
| 460 | /**  | 
            ||
| 461 | * All modules which provide a specific function.  | 
            ||
| 462 | *  | 
            ||
| 463 | * @param string $interface  | 
            ||
| 464 | * @param bool $include_disabled  | 
            ||
| 465 | * @param bool $sort  | 
            ||
| 466 | *  | 
            ||
| 467 | * @return Collection  | 
            ||
| 468 | */  | 
            ||
| 469 | public function findByInterface(string $interface, $include_disabled = false, $sort = false): Collection  | 
            ||
| 470 |     { | 
            ||
| 471 | $modules = $this->all($include_disabled)  | 
            ||
| 472 | ->filter($this->interfaceFilter($interface));  | 
            ||
| 473 | |||
| 474 |         switch ($interface) { | 
            ||
| 475 | case ModuleFooterInterface::class:  | 
            ||
| 476 | return $modules->sort($this->footerSorter());  | 
            ||
| 477 | |||
| 478 | case ModuleMenuInterface::class:  | 
            ||
| 479 | return $modules->sort($this->menuSorter());  | 
            ||
| 480 | |||
| 481 | case ModuleSidebarInterface::class:  | 
            ||
| 482 | return $modules->sort($this->sidebarSorter());  | 
            ||
| 483 | |||
| 484 | case ModuleTabInterface::class:  | 
            ||
| 485 | return $modules->sort($this->tabSorter());  | 
            ||
| 486 | |||
| 487 | default:  | 
            ||
| 488 |                 if ($sort) { | 
            ||
| 489 | return $modules->sort($this->moduleSorter());  | 
            ||
| 490 | }  | 
            ||
| 491 | |||
| 492 | return $modules;  | 
            ||
| 493 | }  | 
            ||
| 494 | }  | 
            ||
| 495 | |||
| 496 | /**  | 
            ||
| 497 | * All modules.  | 
            ||
| 498 | *  | 
            ||
| 499 | * @param bool $include_disabled  | 
            ||
| 500 | *  | 
            ||
| 501 | * @return Collection  | 
            ||
| 502 | */  | 
            ||
| 503 | public function all(bool $include_disabled = false): Collection  | 
            ||
| 504 |     { | 
            ||
| 505 |         return app('cache.array')->rememberForever('all_modules', function (): Collection { | 
            ||
| 506 | // Modules have a default status, order etc.  | 
            ||
| 507 | // We can override these from database settings.  | 
            ||
| 508 |             $module_info = DB::table('module') | 
            ||
| 509 | ->get()  | 
            ||
| 510 |                 ->mapWithKeys(static function (stdClass $row): array { | 
            ||
| 511 | return [$row->module_name => $row];  | 
            ||
| 512 | });  | 
            ||
| 513 | |||
| 514 | return $this->coreModules()  | 
            ||
| 515 | ->merge($this->customModules())  | 
            ||
| 516 |                 ->map(static function (ModuleInterface $module) use ($module_info): ModuleInterface { | 
            ||
| 517 | $info = $module_info->get($module->name());  | 
            ||
| 518 | |||
| 519 |                     if ($info instanceof stdClass) { | 
            ||
| 520 | $module->setEnabled($info->status === 'enabled');  | 
            ||
| 521 | |||
| 522 |                         if ($module instanceof ModuleFooterInterface && $info->footer_order !== null) { | 
            ||
| 523 | $module->setFooterOrder((int) $info->footer_order);  | 
            ||
| 524 | }  | 
            ||
| 525 | |||
| 526 |                         if ($module instanceof ModuleMenuInterface && $info->menu_order !== null) { | 
            ||
| 527 | $module->setMenuOrder((int) $info->menu_order);  | 
            ||
| 528 | }  | 
            ||
| 529 | |||
| 530 |                         if ($module instanceof ModuleSidebarInterface && $info->sidebar_order !== null) { | 
            ||
| 531 | $module->setSidebarOrder((int) $info->sidebar_order);  | 
            ||
| 532 | }  | 
            ||
| 533 | |||
| 534 |                         if ($module instanceof ModuleTabInterface && $info->tab_order !== null) { | 
            ||
| 535 | $module->setTabOrder((int) $info->tab_order);  | 
            ||
| 536 | }  | 
            ||
| 537 |                     } else { | 
            ||
| 538 | $module->setEnabled($module->isEnabledByDefault());  | 
            ||
| 539 | |||
| 540 |                         DB::table('module')->insert([ | 
            ||
| 541 | 'module_name' => $module->name(),  | 
            ||
| 542 | 'status' => $module->isEnabled() ? 'enabled' : 'disabled',  | 
            ||
| 543 | ]);  | 
            ||
| 544 | }  | 
            ||
| 545 | |||
| 546 | return $module;  | 
            ||
| 547 | });  | 
            ||
| 548 | })->filter($this->enabledFilter($include_disabled));  | 
            ||
| 549 | }  | 
            ||
| 550 | |||
| 551 | /**  | 
            ||
| 552 | * All core modules in the system.  | 
            ||
| 553 | *  | 
            ||
| 554 | * @return Collection  | 
            ||
| 555 | */  | 
            ||
| 556 | private function coreModules(): Collection  | 
            ||
| 557 |     { | 
            ||
| 558 | return Collection::make(self::CORE_MODULES)  | 
            ||
| 559 |             ->map(static function (string $class, string $name): ModuleInterface { | 
            ||
| 560 | $module = app($class);  | 
            ||
| 561 | |||
| 562 | $module->setName($name);  | 
            ||
| 563 | |||
| 564 | return $module;  | 
            ||
| 565 | });  | 
            ||
| 566 | }  | 
            ||
| 567 | |||
| 568 | /**  | 
            ||
| 569 | * All custom modules in the system. Custom modules are defined in modules_v4/  | 
            ||
| 570 | *  | 
            ||
| 571 | * @return Collection  | 
            ||
| 572 | */  | 
            ||
| 573 | private function customModules(): Collection  | 
            ||
| 574 |     { | 
            ||
| 575 | $pattern = Webtrees::MODULES_DIR . '*/module.php';  | 
            ||
| 576 | $filenames = glob($pattern, GLOB_NOSORT);  | 
            ||
| 577 | |||
| 578 | return Collection::make($filenames)  | 
            ||
| 579 |             ->filter(static function (string $filename): bool { | 
            ||
| 580 | // Special characters will break PHP variable names.  | 
            ||
| 581 | // This also allows us to ignore modules called "foo.example" and "foo.disable"  | 
            ||
| 582 | $module_name = basename(dirname($filename));  | 
            ||
| 583 | |||
| 584 | return !Str::contains($module_name, ['.', ' ', '[', ']']) && Str::length($module_name) <= 30;  | 
            ||
| 585 | })  | 
            ||
| 586 |             ->map(static function (string $filename): ?ModuleCustomInterface { | 
            ||
| 587 |                 try { | 
            ||
| 588 | $module = self::load($filename);  | 
            ||
| 589 | |||
| 590 |                     if ($module instanceof ModuleCustomInterface) { | 
            ||
| 591 | $module_name = '_' . basename(dirname($filename)) . '_';  | 
            ||
| 592 | |||
| 593 | $module->setName($module_name);  | 
            ||
| 594 |                     } else { | 
            ||
| 595 | return null;  | 
            ||
| 596 | }  | 
            ||
| 597 | |||
| 598 | return $module;  | 
            ||
| 599 |                 } catch (Throwable $ex) { | 
            ||
| 600 | // It would be nice to show this error in a flash-message or similar, but the framework  | 
            ||
| 601 | // has not yet been initialised so we have no themes, languages, sessions, etc.  | 
            ||
| 602 | throw $ex;  | 
            ||
| 603 | }  | 
            ||
| 604 | })  | 
            ||
| 605 | ->filter()  | 
            ||
| 606 |             ->mapWithKeys(static function (ModuleCustomInterface $module): array { | 
            ||
| 607 | return [$module->name() => $module];  | 
            ||
| 608 | });  | 
            ||
| 609 | }  | 
            ||
| 610 | |||
| 611 | /**  | 
            ||
| 612 | * During setup, we'll need access to some languages.  | 
            ||
| 613 | *  | 
            ||
| 614 | * @return Collection  | 
            ||
| 615 | */  | 
            ||
| 616 | public function setupLanguages(): Collection  | 
            ||
| 617 |     { | 
            ||
| 618 | return $this->coreModules()  | 
            ||
| 619 |             ->filter(static function (ModuleInterface $module): bool { | 
            ||
| 620 | return $module instanceof ModuleLanguageInterface && $module->isEnabledByDefault();  | 
            ||
| 621 | })  | 
            ||
| 622 |             ->sort(static function (ModuleLanguageInterface $x, ModuleLanguageInterface $y): int { | 
            ||
| 623 | return $x->locale()->endonymSortable() <=> $y->locale()->endonymSortable();  | 
            ||
| 624 | });  | 
            ||
| 625 | }  | 
            ||
| 626 | |||
| 627 | /**  | 
            ||
| 628 | * Load a custom module in a static scope, to prevent it from modifying local or object variables.  | 
            ||
| 629 | *  | 
            ||
| 630 | * @param string $filename  | 
            ||
| 631 | *  | 
            ||
| 632 | * @return mixed  | 
            ||
| 633 | */  | 
            ||
| 634 | private static function load(string $filename)  | 
            ||
| 635 |     { | 
            ||
| 636 |         try { | 
            ||
| 637 | return include $filename;  | 
            ||
| 638 |         } catch (Throwable $exception) { | 
            ||
| 639 | $module_name = basename(dirname($filename));  | 
            ||
| 640 | $message = 'Fatal error in module: ' . $module_name;  | 
            ||
| 641 | $message .= '<br>' . $exception;  | 
            ||
| 642 | FlashMessages::addMessage($message, 'danger');  | 
            ||
| 643 | }  | 
            ||
| 644 | return null;  | 
            ||
| 645 | }  | 
            ||
| 646 | |||
| 647 | /**  | 
            ||
| 648 | * A function filter modules by enabled/disabled  | 
            ||
| 649 | *  | 
            ||
| 650 | * @param bool $include_disabled  | 
            ||
| 651 | *  | 
            ||
| 652 | * @return Closure  | 
            ||
| 653 | */  | 
            ||
| 654 | private function enabledFilter(bool $include_disabled): Closure  | 
            ||
| 655 |     { | 
            ||
| 656 |         return static function (ModuleInterface $module) use ($include_disabled): bool { | 
            ||
| 657 | return $include_disabled || $module->isEnabled();  | 
            ||
| 658 | };  | 
            ||
| 659 | }  | 
            ||
| 660 | |||
| 661 | /**  | 
            ||
| 662 | * A function filter modules by type  | 
            ||
| 663 | *  | 
            ||
| 664 | * @param string $interface  | 
            ||
| 665 | *  | 
            ||
| 666 | * @return Closure  | 
            ||
| 667 | */  | 
            ||
| 668 | private function interfaceFilter(string $interface): Closure  | 
            ||
| 669 |     { | 
            ||
| 670 |         return static function (ModuleInterface $module) use ($interface): bool { | 
            ||
| 671 | return $module instanceof $interface;  | 
            ||
| 672 | };  | 
            ||
| 673 | }  | 
            ||
| 674 | |||
| 675 | /**  | 
            ||
| 676 | * A function to sort footers  | 
            ||
| 677 | *  | 
            ||
| 678 | * @return Closure  | 
            ||
| 679 | */  | 
            ||
| 680 | private function footerSorter(): Closure  | 
            ||
| 681 |     { | 
            ||
| 682 |         return static function (ModuleFooterInterface $x, ModuleFooterInterface $y): int { | 
            ||
| 683 | return $x->getFooterOrder() <=> $y->getFooterOrder();  | 
            ||
| 684 | };  | 
            ||
| 685 | }  | 
            ||
| 686 | |||
| 687 | /**  | 
            ||
| 688 | * A function to sort menus  | 
            ||
| 689 | *  | 
            ||
| 690 | * @return Closure  | 
            ||
| 691 | */  | 
            ||
| 692 | private function menuSorter(): Closure  | 
            ||
| 693 |     { | 
            ||
| 694 |         return static function (ModuleMenuInterface $x, ModuleMenuInterface $y): int { | 
            ||
| 695 | return $x->getMenuOrder() <=> $y->getMenuOrder();  | 
            ||
| 696 | };  | 
            ||
| 697 | }  | 
            ||
| 698 | |||
| 699 | /**  | 
            ||
| 700 | * A function to sort menus  | 
            ||
| 701 | *  | 
            ||
| 702 | * @return Closure  | 
            ||
| 703 | */  | 
            ||
| 704 | private function sidebarSorter(): Closure  | 
            ||
| 705 |     { | 
            ||
| 706 |         return static function (ModuleSidebarInterface $x, ModuleSidebarInterface $y): int { | 
            ||
| 707 | return $x->getSidebarOrder() <=> $y->getSidebarOrder();  | 
            ||
| 708 | };  | 
            ||
| 709 | }  | 
            ||
| 710 | |||
| 711 | /**  | 
            ||
| 712 | * A function to sort menus  | 
            ||
| 713 | *  | 
            ||
| 714 | * @return Closure  | 
            ||
| 715 | */  | 
            ||
| 716 | private function tabSorter(): Closure  | 
            ||
| 720 | };  | 
            ||
| 721 | }  | 
            ||
| 722 | |||
| 723 | /**  | 
            ||
| 724 | * A function to sort modules by name.  | 
            ||
| 725 | *  | 
            ||
| 726 | * Languages have a "sortable" name, so that "British English" sorts as "English, British".  | 
            ||
| 727 | * This provides a more natural order in the language menu.  | 
            ||
| 728 | *  | 
            ||
| 729 | * @return Closure  | 
            ||
| 730 | */  | 
            ||
| 731 | private function moduleSorter(): Closure  | 
            ||
| 732 |     { | 
            ||
| 733 |         return static function (ModuleInterface $x, ModuleInterface $y): int { | 
            ||
| 734 | $title1 = $x instanceof ModuleLanguageInterface ? $x->locale()->endonymSortable() : $x->title();  | 
            ||
| 735 | $title2 = $y instanceof ModuleLanguageInterface ? $y->locale()->endonymSortable() : $y->title();  | 
            ||
| 736 | |||
| 737 | return I18N::strcasecmp($title1, $title2);  | 
            ||
| 738 | };  | 
            ||
| 739 | }  | 
            ||
| 740 | |||
| 741 | /**  | 
            ||
| 742 | * Find a specified module, if it is currently active.  | 
            ||
| 743 | *  | 
            ||
| 744 | * @param string $module_name  | 
            ||
| 745 | * @param bool $include_disabled  | 
            ||
| 746 | *  | 
            ||
| 747 | * @return ModuleInterface|null  | 
            ||
| 748 | */  | 
            ||
| 749 | public function findByName(string $module_name, bool $include_disabled = false): ?ModuleInterface  | 
            ||
| 750 |     { | 
            ||
| 751 | return $this->all($include_disabled)  | 
            ||
| 752 |             ->first(static function (ModuleInterface $module) use ($module_name): bool { | 
            ||
| 753 | return $module->name() === $module_name;  | 
            ||
| 754 | });  | 
            ||
| 755 | }  | 
            ||
| 756 | |||
| 757 | /**  | 
            ||
| 758 | * Configuration settings are available through the various "module component" pages.  | 
            ||
| 759 | * For modules that do not provide a component, we need to list them separately.  | 
            ||
| 760 | *  | 
            ||
| 761 | * @param bool $include_disabled  | 
            ||
| 762 | *  | 
            ||
| 763 | * @return Collection  | 
            ||
| 764 | */  | 
            ||
| 765 | public function otherModules(bool $include_disabled = false): Collection  | 
            ||
| 766 |     { | 
            ||
| 767 | return $this->findByInterface(ModuleInterface::class, $include_disabled, true)  | 
            ||
| 768 |             ->filter(static function (ModuleInterface $module): bool { | 
            ||
| 769 |                 foreach (self::COMPONENTS as $interface) { | 
            ||
| 770 |                     if ($module instanceof $interface) { | 
            ||
| 771 | return false;  | 
            ||
| 772 | }  | 
            ||
| 773 | }  | 
            ||
| 774 | |||
| 775 | return true;  | 
            ||
| 776 | });  | 
            ||
| 777 | }  | 
            ||
| 778 | |||
| 779 | /**  | 
            ||
| 780 | * Generate a list of module names which exist in the database but not on disk.  | 
            ||
| 781 | *  | 
            ||
| 782 | * @return Collection  | 
            ||
| 783 | */  | 
            ||
| 784 | public function deletedModules(): Collection  | 
            ||
| 794 | }  | 
            ||
| 795 | |||
| 796 | /**  | 
            ||
| 797 | * Boot all the modules.  | 
            ||
| 798 | *  | 
            ||
| 799 | * @param ModuleThemeInterface $current_theme  | 
            ||
| 800 | */  | 
            ||
| 801 | public function bootModules(ModuleThemeInterface $current_theme): void  | 
            ||
| 802 |     { | 
            ||
| 803 |         foreach ($this->all() as $module) { | 
            ||
| 804 | // Only bootstrap the current theme.  | 
            ||
| 805 |             if ($module instanceof ModuleThemeInterface && $module !== $current_theme) { | 
            ||
| 806 | continue;  | 
            ||
| 807 | }  | 
            ||
| 808 | |||
| 809 | $module->boot();  | 
            ||
| 810 | }  | 
            ||
| 811 | }  | 
            ||
| 812 | |||
| 813 | /**  | 
            ||
| 814 | * @return string[]  | 
            ||
| 815 | */  | 
            ||
| 816 | public function componentsWithAccess(): array  | 
            ||
| 819 | }  | 
            ||
| 820 | |||
| 821 | /**  | 
            ||
| 822 | * @return string[]  | 
            ||
| 823 | */  | 
            ||
| 824 | public function componentsWithOrder(): array  | 
            ||
| 825 |     { | 
            ||
| 826 | return self::COMPONENTS_WITH_SORT;  | 
            ||
| 827 | }  | 
            ||
| 828 | }  | 
            ||
| 829 |