xref: /webtrees/app/Services/ModuleService.php (revision a8fbc491176ca9810ef2991e9d3de0b320e31913)
14ca7e03cSGreg Roach<?php
24ca7e03cSGreg Roach/**
34ca7e03cSGreg Roach * webtrees: online genealogy
44ca7e03cSGreg Roach * Copyright (C) 2019 webtrees development team
54ca7e03cSGreg Roach * This program is free software: you can redistribute it and/or modify
64ca7e03cSGreg Roach * it under the terms of the GNU General Public License as published by
74ca7e03cSGreg Roach * the Free Software Foundation, either version 3 of the License, or
84ca7e03cSGreg Roach * (at your option) any later version.
94ca7e03cSGreg Roach * This program is distributed in the hope that it will be useful,
104ca7e03cSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
114ca7e03cSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
124ca7e03cSGreg Roach * GNU General Public License for more details.
134ca7e03cSGreg Roach * You should have received a copy of the GNU General Public License
144ca7e03cSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
154ca7e03cSGreg Roach */
164ca7e03cSGreg Roachdeclare(strict_types=1);
174ca7e03cSGreg Roach
184ca7e03cSGreg Roachnamespace Fisharebest\Webtrees\Services;
194ca7e03cSGreg Roach
204ca7e03cSGreg Roachuse Closure;
214ca7e03cSGreg Roachuse Fisharebest\Webtrees\Auth;
224ca7e03cSGreg Roachuse Fisharebest\Webtrees\FlashMessages;
234ca7e03cSGreg Roachuse Fisharebest\Webtrees\I18N;
244ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\AhnentafelReportModule;
254ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\AlbumModule;
264ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\AncestorsChartModule;
274ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\BatchUpdateModule;
284ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\BingWebmasterToolsModule;
294ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\BirthDeathMarriageReportModule;
304ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\BirthReportModule;
314ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\CalendarMenuModule;
324ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\CemeteryReportModule;
334ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\CensusAssistantModule;
344ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ChangeReportModule;
354ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ChartsBlockModule;
364ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ChartsMenuModule;
374ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\CkeditorModule;
384ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ClippingsCartModule;
394ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\CloudsTheme;
404ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ColorsTheme;
414ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\CompactTreeChartModule;
424ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ContactsFooterModule;
434ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\CookieWarningModule;
444ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\DeathReportModule;
454ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\DescendancyChartModule;
464ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\DescendancyModule;
474ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\DescendancyReportModule;
484ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ExtraInformationModule;
494ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\FabTheme;
504ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\FactSourcesReportModule;
514ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\FamilyBookChartModule;
524ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\FamilyGroupReportModule;
534ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\FamilyNavigatorModule;
544ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\FamilyTreeFavoritesModule;
554ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\FamilyTreeNewsModule;
564ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\FamilyTreeStatisticsModule;
574ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\FanChartModule;
584ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\FrequentlyAskedQuestionsModule;
594ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\GoogleAnalyticsModule;
604ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\GoogleWebmasterToolsModule;
614ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\HitCountFooterModule;
624ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\HourglassChartModule;
634ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\HtmlBlockModule;
644ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\IndividualFactsTabModule;
654ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\IndividualFamiliesReportModule;
664ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\IndividualReportModule;
674ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\InteractiveTreeModule;
684ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\LifespansChartModule;
694ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ListsMenuModule;
704ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\LoggedInUsersModule;
714ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\LoginBlockModule;
724ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\MarriageReportModule;
734ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\MatomoAnalyticsModule;
744ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\MediaTabModule;
754ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\MinimalTheme;
764ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\MissingFactsReportModule;
774ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleBlockInterface;
784ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleChartInterface;
794ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleCustomInterface;
804ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleFooterInterface;
814ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleInterface;
824ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleMenuInterface;
834ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleReportInterface;
844ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleSidebarInterface;
854ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleTabInterface;
864ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\NotesTabModule;
874ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\OccupationReportModule;
884ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\OnThisDayModule;
894ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\PedigreeChartModule;
904ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\PedigreeMapModule;
914ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\PedigreeReportModule;
924ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\PlacesModule;
934ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\PoweredByWebtreesModule;
944ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\RecentChangesModule;
954ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\RelatedIndividualsReportModule;
964ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\RelationshipsChartModule;
974ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\RelativesTabModule;
984ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ReportsMenuModule;
994ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ResearchTaskModule;
1004ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ReviewChangesModule;
1014ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\SearchMenuModule;
1024ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\SiteMapModule;
1034ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\SlideShowModule;
1044ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\SourcesTabModule;
1054ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\StatcounterModule;
1064ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\StatisticsChartModule;
1074ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\StoriesModule;
1084ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ThemeSelectModule;
1094ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\TimelineChartModule;
1104ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\TopGivenNamesModule;
1114ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\TopPageViewsModule;
1124ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\TopSurnamesModule;
1134ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\TreesMenuModule;
1144ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\UpcomingAnniversariesModule;
1154ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\UserFavoritesModule;
1164ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\UserJournalModule;
1174ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\UserMessagesModule;
1184ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\UserWelcomeModule;
1194ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\WebtreesTheme;
1204ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\WelcomeBlockModule;
1214ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\XeneaTheme;
1224ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\YahrzeitModule;
1234ca7e03cSGreg Roachuse Fisharebest\Webtrees\Tree;
1244ca7e03cSGreg Roachuse Fisharebest\Webtrees\User;
1254ca7e03cSGreg Roachuse Fisharebest\Webtrees\Webtrees;
1264ca7e03cSGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
1274ca7e03cSGreg Roachuse Illuminate\Support\Collection;
1284ca7e03cSGreg Roachuse Illuminate\Support\Str;
1294ca7e03cSGreg Roachuse stdClass;
1304ca7e03cSGreg Roachuse Throwable;
1314ca7e03cSGreg Roach
1324ca7e03cSGreg Roach/**
1334ca7e03cSGreg Roach * Functions for managing and maintaining modules.
1344ca7e03cSGreg Roach */
1354ca7e03cSGreg Roachclass ModuleService
1364ca7e03cSGreg Roach{
1374ca7e03cSGreg Roach    // Some types of module have different access levels in different trees.
1384ca7e03cSGreg Roach    private const COMPONENTS = [
1394ca7e03cSGreg Roach        'block'   => ModuleBlockInterface::class,
1404ca7e03cSGreg Roach        'chart'   => ModuleChartInterface::class,
1414ca7e03cSGreg Roach        'menu'    => ModuleMenuInterface::class,
1424ca7e03cSGreg Roach        'report'  => ModuleReportInterface::class,
1434ca7e03cSGreg Roach        'sidebar' => ModuleSidebarInterface::class,
1444ca7e03cSGreg Roach        'tab'     => ModuleTabInterface::class,
1454ca7e03cSGreg Roach    ];
1464ca7e03cSGreg Roach
1474ca7e03cSGreg Roach    // Array keys are module names, and should match module names from earlier versions of webtrees.
1484ca7e03cSGreg Roach    private const CORE_MODULES = [
1494ca7e03cSGreg Roach        'GEDFact_assistant'      => CensusAssistantModule::class,
1504ca7e03cSGreg Roach        'ahnentafel_report'      => AhnentafelReportModule::class,
1514ca7e03cSGreg Roach        'ancestors_chart'        => AncestorsChartModule::class,
1524ca7e03cSGreg Roach        'batch_update'           => BatchUpdateModule::class,
1534ca7e03cSGreg Roach        'bdm_report'             => BirthDeathMarriageReportModule::class,
1544ca7e03cSGreg Roach        'bing-webmaster-tools'   => BingWebmasterToolsModule::class,
1554ca7e03cSGreg Roach        'birth_report'           => BirthReportModule::class,
1564ca7e03cSGreg Roach        'calendar-menu'          => CalendarMenuModule::class,
1574ca7e03cSGreg Roach        'cemetery_report'        => CemeteryReportModule::class,
1584ca7e03cSGreg Roach        'change_report'          => ChangeReportModule::class,
1594ca7e03cSGreg Roach        'charts'                 => ChartsBlockModule::class,
1604ca7e03cSGreg Roach        'charts-menu'            => ChartsMenuModule::class,
1614ca7e03cSGreg Roach        'ckeditor'               => CkeditorModule::class,
1624ca7e03cSGreg Roach        'clippings'              => ClippingsCartModule::class,
1634ca7e03cSGreg Roach        'clouds'                 => CloudsTheme::class,
1644ca7e03cSGreg Roach        'colors'                 => ColorsTheme::class,
1654ca7e03cSGreg Roach        'compact-chart'          => CompactTreeChartModule::class,
1664ca7e03cSGreg Roach        'contact-links'          => ContactsFooterModule::class,
1674ca7e03cSGreg Roach        'cookie-warning'         => CookieWarningModule::class,
1684ca7e03cSGreg Roach        'death_report'           => DeathReportModule::class,
1694ca7e03cSGreg Roach        'descendancy'            => DescendancyModule::class,
1704ca7e03cSGreg Roach        'descendancy_chart'      => DescendancyChartModule::class,
1714ca7e03cSGreg Roach        'descendancy_report'     => DescendancyReportModule::class,
1724ca7e03cSGreg Roach        'extra_info'             => ExtraInformationModule::class,
1734ca7e03cSGreg Roach        'fab'                    => FabTheme::class,
1744ca7e03cSGreg Roach        'fact_sources'           => FactSourcesReportModule::class,
1754ca7e03cSGreg Roach        'family_book_chart'      => FamilyBookChartModule::class,
1764ca7e03cSGreg Roach        'family_group_report'    => FamilyGroupReportModule::class,
1774ca7e03cSGreg Roach        'family_nav'             => FamilyNavigatorModule::class,
1784ca7e03cSGreg Roach        'fan_chart'              => FanChartModule::class,
1794ca7e03cSGreg Roach        'faq'                    => FrequentlyAskedQuestionsModule::class,
1804ca7e03cSGreg Roach        'gedcom_block'           => WelcomeBlockModule::class,
1814ca7e03cSGreg Roach        'gedcom_favorites'       => FamilyTreeFavoritesModule::class,
1824ca7e03cSGreg Roach        'gedcom_news'            => FamilyTreeNewsModule::class,
1834ca7e03cSGreg Roach        'gedcom_stats'           => FamilyTreeStatisticsModule::class,
1844ca7e03cSGreg Roach        'google-analytics'       => GoogleAnalyticsModule::class,
1854ca7e03cSGreg Roach        'google-webmaster-tools' => GoogleWebmasterToolsModule::class,
1864ca7e03cSGreg Roach        'hit-counter'            => HitCountFooterModule::class,
1874ca7e03cSGreg Roach        'hourglass_chart'        => HourglassChartModule::class,
1884ca7e03cSGreg Roach        'html'                   => HtmlBlockModule::class,
1894ca7e03cSGreg Roach        'individual_ext_report'  => IndividualFamiliesReportModule::class,
1904ca7e03cSGreg Roach        'individual_report'      => IndividualReportModule::class,
1914ca7e03cSGreg Roach        'lifespans_chart'        => LifespansChartModule::class,
1924ca7e03cSGreg Roach        'lightbox'               => AlbumModule::class,
1934ca7e03cSGreg Roach        'lists-menu'             => ListsMenuModule::class,
1944ca7e03cSGreg Roach        'logged_in'              => LoggedInUsersModule::class,
1954ca7e03cSGreg Roach        'login_block'            => LoginBlockModule::class,
1964ca7e03cSGreg Roach        'marriage_report'        => MarriageReportModule::class,
1974ca7e03cSGreg Roach        'matomo-analytics'       => MatomoAnalyticsModule::class,
1984ca7e03cSGreg Roach        'media'                  => MediaTabModule::class,
1994ca7e03cSGreg Roach        'minimal'                => MinimalTheme::class,
2004ca7e03cSGreg Roach        'missing_facts_report'   => MissingFactsReportModule::class,
2014ca7e03cSGreg Roach        'notes'                  => NotesTabModule::class,
2024ca7e03cSGreg Roach        'occupation_report'      => OccupationReportModule::class,
2034ca7e03cSGreg Roach        'pedigree-map'           => PedigreeMapModule::class,
2044ca7e03cSGreg Roach        'pedigree_chart'         => PedigreeChartModule::class,
2054ca7e03cSGreg Roach        'pedigree_report'        => PedigreeReportModule::class,
2064ca7e03cSGreg Roach        'personal_facts'         => IndividualFactsTabModule::class,
2074ca7e03cSGreg Roach        'places'                 => PlacesModule::class,
2084ca7e03cSGreg Roach        'powered-by-webtrees'    => PoweredByWebtreesModule::class,
2094ca7e03cSGreg Roach        'random_media'           => SlideShowModule::class,
2104ca7e03cSGreg Roach        'recent_changes'         => RecentChangesModule::class,
2114ca7e03cSGreg Roach        'relationships_chart'    => RelationshipsChartModule::class,
2124ca7e03cSGreg Roach        'relative_ext_report'    => RelatedIndividualsReportModule::class,
2134ca7e03cSGreg Roach        'relatives'              => RelativesTabModule::class,
2144ca7e03cSGreg Roach        'reports-menu'           => ReportsMenuModule::class,
2154ca7e03cSGreg Roach        'review_changes'         => ReviewChangesModule::class,
2164ca7e03cSGreg Roach        'search-menu'            => SearchMenuModule::class,
2174ca7e03cSGreg Roach        'sitemap'                => SiteMapModule::class,
2184ca7e03cSGreg Roach        'sources_tab'            => SourcesTabModule::class,
2194ca7e03cSGreg Roach        'statcounter'            => StatcounterModule::class,
2204ca7e03cSGreg Roach        'statistics_chart'       => StatisticsChartModule::class,
2214ca7e03cSGreg Roach        'stories'                => StoriesModule::class,
2224ca7e03cSGreg Roach        'theme_select'           => ThemeSelectModule::class,
2234ca7e03cSGreg Roach        'timeline_chart'         => TimelineChartModule::class,
2244ca7e03cSGreg Roach        'todays_events'          => OnThisDayModule::class,
2254ca7e03cSGreg Roach        'todo'                   => ResearchTaskModule::class,
2264ca7e03cSGreg Roach        'top10_givnnames'        => TopGivenNamesModule::class,
2274ca7e03cSGreg Roach        'top10_pageviews'        => TopPageViewsModule::class,
2284ca7e03cSGreg Roach        'top10_surnames'         => TopSurnamesModule::class,
2294ca7e03cSGreg Roach        'tree'                   => InteractiveTreeModule::class,
2304ca7e03cSGreg Roach        'trees-menu'             => TreesMenuModule::class,
2314ca7e03cSGreg Roach        'upcoming_events'        => UpcomingAnniversariesModule::class,
2324ca7e03cSGreg Roach        'user_blog'              => UserJournalModule::class,
2334ca7e03cSGreg Roach        'user_favorites'         => UserFavoritesModule::class,
2344ca7e03cSGreg Roach        'user_messages'          => UserMessagesModule::class,
2354ca7e03cSGreg Roach        'user_welcome'           => UserWelcomeModule::class,
2364ca7e03cSGreg Roach        'webtrees'               => WebtreesTheme::class,
2374ca7e03cSGreg Roach        'xenea'                  => XeneaTheme::class,
2384ca7e03cSGreg Roach        'yahrzeit'               => YahrzeitModule::class,
2394ca7e03cSGreg Roach    ];
2404ca7e03cSGreg Roach
2414ca7e03cSGreg Roach    /**
2424ca7e03cSGreg Roach     * All core modules in the system.
2434ca7e03cSGreg Roach     *
2444ca7e03cSGreg Roach     * @return Collection
2454ca7e03cSGreg Roach     */
2464ca7e03cSGreg Roach    private function coreModules(): Collection
2474ca7e03cSGreg Roach    {
2484ca7e03cSGreg Roach        $modules = new Collection(self::CORE_MODULES);
2494ca7e03cSGreg Roach
2504ca7e03cSGreg Roach        return $modules->map(function (string $class, string $name): ModuleInterface {
2514ca7e03cSGreg Roach            $module = app()->make($class);
2524ca7e03cSGreg Roach
2534ca7e03cSGreg Roach            $module->setName($name);
2544ca7e03cSGreg Roach
2554ca7e03cSGreg Roach            return $module;
2564ca7e03cSGreg Roach        });
2574ca7e03cSGreg Roach    }
2584ca7e03cSGreg Roach
2594ca7e03cSGreg Roach    /**
2604ca7e03cSGreg Roach     * All custom modules in the system.  Custom modules are defined in modules_v4/
2614ca7e03cSGreg Roach     *
2624ca7e03cSGreg Roach     * @return Collection
2634ca7e03cSGreg Roach     */
2644ca7e03cSGreg Roach    private function customModules(): Collection
2654ca7e03cSGreg Roach    {
2664ca7e03cSGreg Roach        $pattern   = WT_ROOT . Webtrees::MODULES_PATH . '*/module.php';
2674ca7e03cSGreg Roach        $filenames = glob($pattern);
2684ca7e03cSGreg Roach
2694ca7e03cSGreg Roach        return (new Collection($filenames))
2704ca7e03cSGreg Roach            ->filter(function (string $filename): bool {
2714ca7e03cSGreg Roach                // Special characters will break PHP variable names.
2724ca7e03cSGreg Roach                // This also allows us to ignore modules called "foo.example" and "foo.disable"
2734ca7e03cSGreg Roach                $module_name = basename(dirname($filename));
2744ca7e03cSGreg Roach
2754ca7e03cSGreg Roach                return !Str::contains($module_name, ['.', ' ', '[', ']']) && Str::length($module_name) <= 30;
2764ca7e03cSGreg Roach            })
2774ca7e03cSGreg Roach            ->map(function (string $filename): ?ModuleCustomInterface {
2784ca7e03cSGreg Roach                try {
2794ca7e03cSGreg Roach                    $module = self::load($filename);
2804ca7e03cSGreg Roach
2814ca7e03cSGreg Roach                    if ($module instanceof ModuleCustomInterface) {
2824ca7e03cSGreg Roach                        $module_name = '_' . basename(dirname($filename)) . '_';
2834ca7e03cSGreg Roach
2844ca7e03cSGreg Roach                        $module->setName($module_name);
2854ca7e03cSGreg Roach                    } else {
2864ca7e03cSGreg Roach                        return null;
2874ca7e03cSGreg Roach                    }
2884ca7e03cSGreg Roach
2894ca7e03cSGreg Roach                    return $module;
2904ca7e03cSGreg Roach                } catch (Throwable $ex) {
2914ca7e03cSGreg Roach                    $message = '<pre>' . e($ex->getMessage()) . "\n" . e($ex->getTraceAsString()) . '</pre>';
2924ca7e03cSGreg Roach                    FlashMessages::addMessage($message, 'danger');
2934ca7e03cSGreg Roach
2944ca7e03cSGreg Roach                    return null;
2954ca7e03cSGreg Roach                }
2964ca7e03cSGreg Roach            })
2974ca7e03cSGreg Roach            ->filter();
2984ca7e03cSGreg Roach    }
2994ca7e03cSGreg Roach
3004ca7e03cSGreg Roach    /**
3014ca7e03cSGreg Roach     * All modules.
3024ca7e03cSGreg Roach     *
3034ca7e03cSGreg Roach     * @return Collection|ModuleInterface[]
3044ca7e03cSGreg Roach     */
3054ca7e03cSGreg Roach    public function all(): Collection
3064ca7e03cSGreg Roach    {
3074ca7e03cSGreg Roach        return app('cache.array')->rememberForever('all_modules', function (): Collection {
3084ca7e03cSGreg Roach            // Modules have a default status, order etc.
3094ca7e03cSGreg Roach            // We can override these from database settings.
3104ca7e03cSGreg Roach            $module_info = DB::table('module')
3114ca7e03cSGreg Roach                ->get()
3124ca7e03cSGreg Roach                ->mapWithKeys(function (stdClass $row): array {
3134ca7e03cSGreg Roach                    return [$row->module_name => $row];
3144ca7e03cSGreg Roach                });
3154ca7e03cSGreg Roach
3164ca7e03cSGreg Roach            return self::coreModules()
3174ca7e03cSGreg Roach                ->merge(self::customModules())
3184ca7e03cSGreg Roach                ->map(function (ModuleInterface $module) use ($module_info): ModuleInterface {
3194ca7e03cSGreg Roach                    $info = $module_info->get($module->name());
3204ca7e03cSGreg Roach
3214ca7e03cSGreg Roach                    if ($info instanceof stdClass) {
3224ca7e03cSGreg Roach                        $module->setEnabled($info->status === 'enabled');
3234ca7e03cSGreg Roach
3244ca7e03cSGreg Roach                        if ($module instanceof ModuleFooterInterface && $info->footer_order !== null) {
3254ca7e03cSGreg Roach                            $module->setFooterOrder((int) $info->footer_order);
3264ca7e03cSGreg Roach                        }
3274ca7e03cSGreg Roach
3284ca7e03cSGreg Roach                        if ($module instanceof ModuleMenuInterface && $info->menu_order !== null) {
3294ca7e03cSGreg Roach                            $module->setMenuOrder((int) $info->menu_order);
3304ca7e03cSGreg Roach                        }
3314ca7e03cSGreg Roach
3324ca7e03cSGreg Roach                        if ($module instanceof ModuleSidebarInterface && $info->sidebar_order !== null) {
3334ca7e03cSGreg Roach                            $module->setSidebarOrder((int) $info->sidebar_order);
3344ca7e03cSGreg Roach                        }
3354ca7e03cSGreg Roach
3364ca7e03cSGreg Roach                        if ($module instanceof ModuleTabInterface && $info->tab_order !== null) {
3374ca7e03cSGreg Roach                            $module->setTabOrder((int) $info->tab_order);
3384ca7e03cSGreg Roach                        }
3394ca7e03cSGreg Roach                    } else {
3404ca7e03cSGreg Roach                        DB::table('module')->insert(['module_name' => $module->name()]);
3414ca7e03cSGreg Roach                    }
3424ca7e03cSGreg Roach
3434ca7e03cSGreg Roach                    return $module;
3444ca7e03cSGreg Roach                })
3454ca7e03cSGreg Roach                ->sort(self::moduleSorter());
3464ca7e03cSGreg Roach        });
3474ca7e03cSGreg Roach    }
3484ca7e03cSGreg Roach
3494ca7e03cSGreg Roach    /**
350*a8fbc491SGreg Roach     * Load a module in a static scope, to prevent it from modifying local or object variables.
3514ca7e03cSGreg Roach     *
3524ca7e03cSGreg Roach     * @param string $filename
3534ca7e03cSGreg Roach     *
3544ca7e03cSGreg Roach     * @return mixed
3554ca7e03cSGreg Roach     */
356*a8fbc491SGreg Roach    private static function load(string $filename)
3574ca7e03cSGreg Roach    {
3584ca7e03cSGreg Roach        return include $filename;
3594ca7e03cSGreg Roach    }
3604ca7e03cSGreg Roach
3614ca7e03cSGreg Roach    /**
3624ca7e03cSGreg Roach     * A function to sort modules by name
3634ca7e03cSGreg Roach     *
3644ca7e03cSGreg Roach     * @return Closure
3654ca7e03cSGreg Roach     */
3664ca7e03cSGreg Roach    private function moduleSorter(): Closure
3674ca7e03cSGreg Roach    {
3684ca7e03cSGreg Roach        return function (ModuleInterface $x, ModuleInterface $y): int {
3694ca7e03cSGreg Roach            return I18N::strcasecmp($x->title(), $y->title());
3704ca7e03cSGreg Roach        };
3714ca7e03cSGreg Roach    }
3724ca7e03cSGreg Roach
3734ca7e03cSGreg Roach    /**
3744ca7e03cSGreg Roach     * A function to sort footers
3754ca7e03cSGreg Roach     *
3764ca7e03cSGreg Roach     * @return Closure
3774ca7e03cSGreg Roach     */
3784ca7e03cSGreg Roach    private function footerSorter(): Closure
3794ca7e03cSGreg Roach    {
3804ca7e03cSGreg Roach        return function (ModuleFooterInterface $x, ModuleFooterInterface $y): int {
3814ca7e03cSGreg Roach            return $x->getFooterOrder() <=> $y->getFooterOrder();
3824ca7e03cSGreg Roach        };
3834ca7e03cSGreg Roach    }
3844ca7e03cSGreg Roach
3854ca7e03cSGreg Roach    /**
3864ca7e03cSGreg Roach     * A function to sort menus
3874ca7e03cSGreg Roach     *
3884ca7e03cSGreg Roach     * @return Closure
3894ca7e03cSGreg Roach     */
3904ca7e03cSGreg Roach    private function menuSorter(): Closure
3914ca7e03cSGreg Roach    {
3924ca7e03cSGreg Roach        return function (ModuleMenuInterface $x, ModuleMenuInterface $y): int {
3934ca7e03cSGreg Roach            return $x->getMenuOrder() <=> $y->getMenuOrder();
3944ca7e03cSGreg Roach        };
3954ca7e03cSGreg Roach    }
3964ca7e03cSGreg Roach
3974ca7e03cSGreg Roach    /**
3984ca7e03cSGreg Roach     * A function to sort menus
3994ca7e03cSGreg Roach     *
4004ca7e03cSGreg Roach     * @return Closure
4014ca7e03cSGreg Roach     */
4024ca7e03cSGreg Roach    private function sidebarSorter(): Closure
4034ca7e03cSGreg Roach    {
4044ca7e03cSGreg Roach        return function (ModuleSidebarInterface $x, ModuleSidebarInterface $y): int {
4054ca7e03cSGreg Roach            return $x->getSidebarOrder() <=> $y->getSidebarOrder();
4064ca7e03cSGreg Roach        };
4074ca7e03cSGreg Roach    }
4084ca7e03cSGreg Roach
4094ca7e03cSGreg Roach    /**
4104ca7e03cSGreg Roach     * A function to sort menus
4114ca7e03cSGreg Roach     *
4124ca7e03cSGreg Roach     * @return Closure
4134ca7e03cSGreg Roach     */
4144ca7e03cSGreg Roach    private function tabSorter(): Closure
4154ca7e03cSGreg Roach    {
4164ca7e03cSGreg Roach        return function (ModuleTabInterface $x, ModuleTabInterface $y): int {
4174ca7e03cSGreg Roach            return $x->getTabOrder() <=> $y->getTabOrder();
4184ca7e03cSGreg Roach        };
4194ca7e03cSGreg Roach    }
4204ca7e03cSGreg Roach
4214ca7e03cSGreg Roach    /**
4224ca7e03cSGreg Roach     * Modules which (a) provide a specific function and (b) we have permission to see.
4234ca7e03cSGreg Roach     *
4244ca7e03cSGreg Roach     * @param string $component
4254ca7e03cSGreg Roach     * @param Tree   $tree
4264ca7e03cSGreg Roach     * @param User   $user
4274ca7e03cSGreg Roach     *
4284ca7e03cSGreg Roach     * @return Collection|ModuleInterface[]
4294ca7e03cSGreg Roach     */
4304ca7e03cSGreg Roach    public function findByComponent(string $component, Tree $tree, User $user): Collection
4314ca7e03cSGreg Roach    {
4324ca7e03cSGreg Roach        $interface = self::COMPONENTS[$component];
4334ca7e03cSGreg Roach
4344ca7e03cSGreg Roach        return self::findByInterface($interface)
4354ca7e03cSGreg Roach            ->filter(function (ModuleInterface $module) use ($component, $tree, $user): bool {
4364ca7e03cSGreg Roach                return $module->accessLevel($tree, $component) >= Auth::accessLevel($tree, $user);
4374ca7e03cSGreg Roach            });
4384ca7e03cSGreg Roach    }
4394ca7e03cSGreg Roach
4404ca7e03cSGreg Roach    /**
4414ca7e03cSGreg Roach     * All modules which provide a specific function.
4424ca7e03cSGreg Roach     *
4434ca7e03cSGreg Roach     * @param string $interface
4444ca7e03cSGreg Roach     * @param bool   $include_disabled
4454ca7e03cSGreg Roach     *
4464ca7e03cSGreg Roach     * @return Collection|ModuleInterface[]
4474ca7e03cSGreg Roach     */
4484ca7e03cSGreg Roach    public function findByInterface(string $interface, $include_disabled = false): Collection
4494ca7e03cSGreg Roach    {
4504ca7e03cSGreg Roach        $modules = self::all()
4514ca7e03cSGreg Roach            ->filter(function (ModuleInterface $module) use ($interface): bool {
4524ca7e03cSGreg Roach                return $module instanceof $interface;
4534ca7e03cSGreg Roach            })
4544ca7e03cSGreg Roach            ->filter(function (ModuleInterface $module) use ($include_disabled): bool {
4554ca7e03cSGreg Roach                return $include_disabled || $module->isEnabled();
4564ca7e03cSGreg Roach            });
4574ca7e03cSGreg Roach
4584ca7e03cSGreg Roach        switch ($interface) {
4594ca7e03cSGreg Roach            case ModuleFooterInterface::class:
4604ca7e03cSGreg Roach                return $modules->sort(self::footerSorter());
4614ca7e03cSGreg Roach
4624ca7e03cSGreg Roach            case ModuleMenuInterface::class:
4634ca7e03cSGreg Roach                return $modules->sort(self::menuSorter());
4644ca7e03cSGreg Roach
4654ca7e03cSGreg Roach            case ModuleSidebarInterface::class:
4664ca7e03cSGreg Roach                return $modules->sort(self::sidebarSorter());
4674ca7e03cSGreg Roach
4684ca7e03cSGreg Roach            case ModuleTabInterface::class:
4694ca7e03cSGreg Roach                return $modules->sort(self::tabSorter());
4704ca7e03cSGreg Roach        }
4714ca7e03cSGreg Roach
4724ca7e03cSGreg Roach        return $modules;
4734ca7e03cSGreg Roach    }
4744ca7e03cSGreg Roach
4754ca7e03cSGreg Roach    /**
4764ca7e03cSGreg Roach     * Find a specified module, if it is currently active.
4774ca7e03cSGreg Roach     *
4784ca7e03cSGreg Roach     * @param string $module_name
4794ca7e03cSGreg Roach     *
4804ca7e03cSGreg Roach     * @return ModuleInterface|null
4814ca7e03cSGreg Roach     */
4824ca7e03cSGreg Roach    public function findByName(string $module_name): ?ModuleInterface
4834ca7e03cSGreg Roach    {
4844ca7e03cSGreg Roach        return self::all()
4854ca7e03cSGreg Roach            ->filter(function (ModuleInterface $module) use ($module_name): bool {
4864ca7e03cSGreg Roach                return $module->isEnabled() && $module->name() === $module_name;
4874ca7e03cSGreg Roach            })
4884ca7e03cSGreg Roach            ->first();
4894ca7e03cSGreg Roach    }
4904ca7e03cSGreg Roach
4914ca7e03cSGreg Roach    /**
4924ca7e03cSGreg Roach     * Find a specified module, if it is currently active.
4934ca7e03cSGreg Roach     *
4944ca7e03cSGreg Roach     * @param string $class_name
4954ca7e03cSGreg Roach     *
4964ca7e03cSGreg Roach     * @return ModuleInterface|null
4974ca7e03cSGreg Roach     */
4984ca7e03cSGreg Roach    public function findByClass(string $class_name): ?ModuleInterface
4994ca7e03cSGreg Roach    {
5004ca7e03cSGreg Roach        return self::all()
5014ca7e03cSGreg Roach            ->filter(function (ModuleInterface $module) use ($class_name): bool {
5024ca7e03cSGreg Roach                return $module->isEnabled() && $module instanceof $class_name;
5034ca7e03cSGreg Roach            })
5044ca7e03cSGreg Roach            ->first();
5054ca7e03cSGreg Roach    }
5064ca7e03cSGreg Roach}
507