xref: /webtrees/app/Http/Middleware/BootModules.php (revision 8fb0f35ec023dbdcc3de06551bf957cb6f7f6faf)
10cfd6963SGreg Roach<?php
23976b470SGreg Roach
30cfd6963SGreg Roach/**
40cfd6963SGreg Roach * webtrees: online genealogy
50cfd6963SGreg Roach * Copyright (C) 2019 webtrees development team
60cfd6963SGreg Roach * This program is free software: you can redistribute it and/or modify
70cfd6963SGreg Roach * it under the terms of the GNU General Public License as published by
80cfd6963SGreg Roach * the Free Software Foundation, either version 3 of the License, or
90cfd6963SGreg Roach * (at your option) any later version.
100cfd6963SGreg Roach * This program is distributed in the hope that it will be useful,
110cfd6963SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
120cfd6963SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
130cfd6963SGreg Roach * GNU General Public License for more details.
140cfd6963SGreg Roach * You should have received a copy of the GNU General Public License
150cfd6963SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
160cfd6963SGreg Roach */
170cfd6963SGreg Roachdeclare(strict_types=1);
180cfd6963SGreg Roach
190cfd6963SGreg Roachnamespace Fisharebest\Webtrees\Http\Middleware;
200cfd6963SGreg Roach
210cfd6963SGreg Roachuse Fisharebest\Webtrees\Module\ModuleInterface;
220cfd6963SGreg Roachuse Fisharebest\Webtrees\Module\ModuleThemeInterface;
230cfd6963SGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
246ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
256ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
266ccdf4f0SGreg Roachuse Psr\Http\Server\MiddlewareInterface;
276ccdf4f0SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
280cfd6963SGreg Roachuse function method_exists;
290cfd6963SGreg Roach
300cfd6963SGreg Roach/**
310cfd6963SGreg Roach * Middleware to bootstrap the modules.
320cfd6963SGreg Roach */
330cfd6963SGreg Roachclass BootModules implements MiddlewareInterface
340cfd6963SGreg Roach{
35*8fb0f35eSGreg Roach    /** @var ModuleService */
36*8fb0f35eSGreg Roach    private $module_service;
37*8fb0f35eSGreg Roach
38*8fb0f35eSGreg Roach    /** @var ModuleThemeInterface */
39*8fb0f35eSGreg Roach    private $theme;
40*8fb0f35eSGreg Roach
41*8fb0f35eSGreg Roach    /**
42*8fb0f35eSGreg Roach     * BootModules constructor.
43*8fb0f35eSGreg Roach     *
44*8fb0f35eSGreg Roach     * @param ModuleService        $module_service
45*8fb0f35eSGreg Roach     * @param ModuleThemeInterface $theme
46*8fb0f35eSGreg Roach     */
47*8fb0f35eSGreg Roach    public function __construct(ModuleService $module_service, ModuleThemeInterface $theme)
48*8fb0f35eSGreg Roach    {
49*8fb0f35eSGreg Roach
50*8fb0f35eSGreg Roach        $this->module_service = $module_service;
51*8fb0f35eSGreg Roach        $this->theme          = $theme;
52*8fb0f35eSGreg Roach    }
53*8fb0f35eSGreg Roach
540cfd6963SGreg Roach    /**
556ccdf4f0SGreg Roach     * @param ServerRequestInterface  $request
566ccdf4f0SGreg Roach     * @param RequestHandlerInterface $handler
570cfd6963SGreg Roach     *
586ccdf4f0SGreg Roach     * @return ResponseInterface
590cfd6963SGreg Roach     */
606ccdf4f0SGreg Roach    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
610cfd6963SGreg Roach    {
62*8fb0f35eSGreg Roach        $bootable_modules = $this->module_service->all()->filter(static function (ModuleInterface $module) {
630cfd6963SGreg Roach            return method_exists($module, 'boot');
640cfd6963SGreg Roach        });
650cfd6963SGreg Roach
660cfd6963SGreg Roach        foreach ($bootable_modules as $module) {
670cfd6963SGreg Roach            // Only bootstrap the current theme.
68*8fb0f35eSGreg Roach            if ($module instanceof ModuleThemeInterface && $module !== $this->theme) {
690cfd6963SGreg Roach                continue;
700cfd6963SGreg Roach            }
710cfd6963SGreg Roach
723e4bf26fSGreg Roach            app()->dispatch($module, 'boot');
730cfd6963SGreg Roach        }
740cfd6963SGreg Roach
756ccdf4f0SGreg Roach        return $handler->handle($request);
760cfd6963SGreg Roach    }
770cfd6963SGreg Roach}
78