xref: /webtrees/app/Http/Middleware/BootModules.php (revision 0cfd6963ac65bd7fe86283b801b4f23d665c6004)
1*0cfd6963SGreg Roach<?php
2*0cfd6963SGreg Roach/**
3*0cfd6963SGreg Roach * webtrees: online genealogy
4*0cfd6963SGreg Roach * Copyright (C) 2019 webtrees development team
5*0cfd6963SGreg Roach * This program is free software: you can redistribute it and/or modify
6*0cfd6963SGreg Roach * it under the terms of the GNU General Public License as published by
7*0cfd6963SGreg Roach * the Free Software Foundation, either version 3 of the License, or
8*0cfd6963SGreg Roach * (at your option) any later version.
9*0cfd6963SGreg Roach * This program is distributed in the hope that it will be useful,
10*0cfd6963SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
11*0cfd6963SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12*0cfd6963SGreg Roach * GNU General Public License for more details.
13*0cfd6963SGreg Roach * You should have received a copy of the GNU General Public License
14*0cfd6963SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
15*0cfd6963SGreg Roach */
16*0cfd6963SGreg Roachdeclare(strict_types=1);
17*0cfd6963SGreg Roach
18*0cfd6963SGreg Roachnamespace Fisharebest\Webtrees\Http\Middleware;
19*0cfd6963SGreg Roach
20*0cfd6963SGreg Roachuse Closure;
21*0cfd6963SGreg Roachuse Fisharebest\Webtrees\Module\ModuleInterface;
22*0cfd6963SGreg Roachuse Fisharebest\Webtrees\Module\ModuleThemeInterface;
23*0cfd6963SGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
24*0cfd6963SGreg Roachuse Symfony\Component\HttpFoundation\Request;
25*0cfd6963SGreg Roachuse Symfony\Component\HttpFoundation\Response;
26*0cfd6963SGreg Roachuse Throwable;
27*0cfd6963SGreg Roachuse function method_exists;
28*0cfd6963SGreg Roach
29*0cfd6963SGreg Roach/**
30*0cfd6963SGreg Roach * Middleware to bootstrap the modules.
31*0cfd6963SGreg Roach */
32*0cfd6963SGreg Roachclass BootModules implements MiddlewareInterface
33*0cfd6963SGreg Roach{
34*0cfd6963SGreg Roach    /**
35*0cfd6963SGreg Roach     * @param Request $request
36*0cfd6963SGreg Roach     * @param Closure $next
37*0cfd6963SGreg Roach     *
38*0cfd6963SGreg Roach     * @return Response
39*0cfd6963SGreg Roach     * @throws Throwable
40*0cfd6963SGreg Roach     */
41*0cfd6963SGreg Roach    public function handle(Request $request, Closure $next): Response
42*0cfd6963SGreg Roach    {
43*0cfd6963SGreg Roach        $module_service = app(ModuleService::class);
44*0cfd6963SGreg Roach        $theme          = app(ModuleThemeInterface::class);
45*0cfd6963SGreg Roach
46*0cfd6963SGreg Roach        $bootable_modules = $module_service->all()->filter(function (ModuleInterface $module) {
47*0cfd6963SGreg Roach            return method_exists($module, 'boot');
48*0cfd6963SGreg Roach        });
49*0cfd6963SGreg Roach
50*0cfd6963SGreg Roach        foreach ($bootable_modules as $module) {
51*0cfd6963SGreg Roach            // Only bootstrap the current theme.
52*0cfd6963SGreg Roach            if ($module instanceof ModuleThemeInterface && $module !== $theme) {
53*0cfd6963SGreg Roach                continue;
54*0cfd6963SGreg Roach            }
55*0cfd6963SGreg Roach
56*0cfd6963SGreg Roach            return app()->dispatch($module, 'boot');
57*0cfd6963SGreg Roach        }
58*0cfd6963SGreg Roach
59*0cfd6963SGreg Roach        return $next($request);
60*0cfd6963SGreg Roach    }
61*0cfd6963SGreg Roach}
62