xref: /webtrees/app/Http/Middleware/BootModules.php (revision 3e4bf26f9f16d92152484c56e3629888fb6019d5)
10cfd6963SGreg Roach<?php
20cfd6963SGreg Roach/**
30cfd6963SGreg Roach * webtrees: online genealogy
40cfd6963SGreg Roach * Copyright (C) 2019 webtrees development team
50cfd6963SGreg Roach * This program is free software: you can redistribute it and/or modify
60cfd6963SGreg Roach * it under the terms of the GNU General Public License as published by
70cfd6963SGreg Roach * the Free Software Foundation, either version 3 of the License, or
80cfd6963SGreg Roach * (at your option) any later version.
90cfd6963SGreg Roach * This program is distributed in the hope that it will be useful,
100cfd6963SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
110cfd6963SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
120cfd6963SGreg Roach * GNU General Public License for more details.
130cfd6963SGreg Roach * You should have received a copy of the GNU General Public License
140cfd6963SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
150cfd6963SGreg Roach */
160cfd6963SGreg Roachdeclare(strict_types=1);
170cfd6963SGreg Roach
180cfd6963SGreg Roachnamespace Fisharebest\Webtrees\Http\Middleware;
190cfd6963SGreg Roach
200cfd6963SGreg Roachuse Closure;
210cfd6963SGreg Roachuse Fisharebest\Webtrees\Module\ModuleInterface;
220cfd6963SGreg Roachuse Fisharebest\Webtrees\Module\ModuleThemeInterface;
230cfd6963SGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
240cfd6963SGreg Roachuse Symfony\Component\HttpFoundation\Request;
250cfd6963SGreg Roachuse Symfony\Component\HttpFoundation\Response;
260cfd6963SGreg Roachuse Throwable;
270cfd6963SGreg Roachuse function method_exists;
280cfd6963SGreg Roach
290cfd6963SGreg Roach/**
300cfd6963SGreg Roach * Middleware to bootstrap the modules.
310cfd6963SGreg Roach */
320cfd6963SGreg Roachclass BootModules implements MiddlewareInterface
330cfd6963SGreg Roach{
340cfd6963SGreg Roach    /**
350cfd6963SGreg Roach     * @param Request $request
360cfd6963SGreg Roach     * @param Closure $next
370cfd6963SGreg Roach     *
380cfd6963SGreg Roach     * @return Response
390cfd6963SGreg Roach     * @throws Throwable
400cfd6963SGreg Roach     */
410cfd6963SGreg Roach    public function handle(Request $request, Closure $next): Response
420cfd6963SGreg Roach    {
430cfd6963SGreg Roach        $module_service = app(ModuleService::class);
440cfd6963SGreg Roach        $theme          = app(ModuleThemeInterface::class);
450cfd6963SGreg Roach
460cfd6963SGreg Roach        $bootable_modules = $module_service->all()->filter(function (ModuleInterface $module) {
470cfd6963SGreg Roach            return method_exists($module, 'boot');
480cfd6963SGreg Roach        });
490cfd6963SGreg Roach
500cfd6963SGreg Roach        foreach ($bootable_modules as $module) {
510cfd6963SGreg Roach            // Only bootstrap the current theme.
520cfd6963SGreg Roach            if ($module instanceof ModuleThemeInterface && $module !== $theme) {
530cfd6963SGreg Roach                continue;
540cfd6963SGreg Roach            }
550cfd6963SGreg Roach
56*3e4bf26fSGreg Roach            app()->dispatch($module, 'boot');
570cfd6963SGreg Roach        }
580cfd6963SGreg Roach
590cfd6963SGreg Roach        return $next($request);
600cfd6963SGreg Roach    }
610cfd6963SGreg Roach}
62