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