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