xref: /webtrees/app/Http/Middleware/UseTheme.php (revision d35568b467207589ea9059739da0bf1f7e785a0d)
1b5979037SGreg Roach<?php
23976b470SGreg Roach
3b5979037SGreg Roach/**
4b5979037SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
6b5979037SGreg Roach * This program is free software: you can redistribute it and/or modify
7b5979037SGreg Roach * it under the terms of the GNU General Public License as published by
8b5979037SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9b5979037SGreg Roach * (at your option) any later version.
10b5979037SGreg Roach * This program is distributed in the hope that it will be useful,
11b5979037SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12b5979037SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13b5979037SGreg Roach * GNU General Public License for more details.
14b5979037SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16b5979037SGreg Roach */
17fcfa147eSGreg Roach
18b5979037SGreg Roachdeclare(strict_types=1);
19b5979037SGreg Roach
20b5979037SGreg Roachnamespace Fisharebest\Webtrees\Http\Middleware;
21b5979037SGreg Roach
22b5979037SGreg Roachuse Fisharebest\Webtrees\Module\ModuleThemeInterface;
23b5979037SGreg Roachuse Fisharebest\Webtrees\Module\WebtreesTheme;
24*d35568b4SGreg Roachuse Fisharebest\Webtrees\Registry;
25b5979037SGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
26b5979037SGreg Roachuse Fisharebest\Webtrees\Session;
27b5979037SGreg Roachuse Fisharebest\Webtrees\Site;
28b5979037SGreg Roachuse Generator;
296ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
306ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
316ccdf4f0SGreg Roachuse Psr\Http\Server\MiddlewareInterface;
326ccdf4f0SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
33b5979037SGreg Roach
34b5979037SGreg Roach/**
35150f35adSGreg Roach * Middleware to select a theme.
36b5979037SGreg Roach */
37b5979037SGreg Roachclass UseTheme implements MiddlewareInterface
38b5979037SGreg Roach{
39c4943cffSGreg Roach    private ModuleService $module_service;
40b5979037SGreg Roach
41b5979037SGreg Roach    /**
42b5979037SGreg Roach     * UseTheme constructor.
43b5979037SGreg Roach     *
44b5979037SGreg Roach     * @param ModuleService $module_service
45b5979037SGreg Roach     */
4617b3ba76SGreg Roach    public function __construct(ModuleService $module_service)
47b5979037SGreg Roach    {
48b5979037SGreg Roach        $this->module_service = $module_service;
49b5979037SGreg Roach    }
50b5979037SGreg Roach
51b5979037SGreg Roach    /**
526ccdf4f0SGreg Roach     * @param ServerRequestInterface  $request
536ccdf4f0SGreg Roach     * @param RequestHandlerInterface $handler
54b5979037SGreg Roach     *
556ccdf4f0SGreg Roach     * @return ResponseInterface
56b5979037SGreg Roach     */
576ccdf4f0SGreg Roach    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
58b5979037SGreg Roach    {
59d77915f8SGreg Roach        foreach ($this->themes() as $theme) {
60b5979037SGreg Roach            if ($theme instanceof ModuleThemeInterface) {
61*d35568b4SGreg Roach                Registry::container()->set(ModuleThemeInterface::class, $theme);
62150f35adSGreg Roach                $request = $request->withAttribute('theme', $theme);
63a0801ffbSGreg Roach                Session::put('theme', $theme->name());
64b5979037SGreg Roach                break;
65b5979037SGreg Roach            }
66b5979037SGreg Roach        }
67b5979037SGreg Roach
686ccdf4f0SGreg Roach        return $handler->handle($request);
69b5979037SGreg Roach    }
70b5979037SGreg Roach
71b5979037SGreg Roach    /**
72b5979037SGreg Roach     * The theme can be chosen in various ways.
73b5979037SGreg Roach     *
74fc26b4f6SGreg Roach     * @return Generator<ModuleThemeInterface|null>
75b5979037SGreg Roach     */
76d77915f8SGreg Roach    private function themes(): Generator
77b5979037SGreg Roach    {
78b5979037SGreg Roach        $themes = $this->module_service->findByInterface(ModuleThemeInterface::class);
79b5979037SGreg Roach
80b5979037SGreg Roach        // Last theme used
817b9aa048SGreg Roach        yield $themes
8271e7c06eSGreg Roach            ->first(static fn (ModuleThemeInterface $module): bool => $module->name() === Session::get('theme'));
83b5979037SGreg Roach
84b5979037SGreg Roach        // Default for site
857b9aa048SGreg Roach        yield $themes
8671e7c06eSGreg Roach            ->first(static fn (ModuleThemeInterface $module): bool => $module->name() === Site::getPreference('THEME_DIR'));
87b5979037SGreg Roach
88b5979037SGreg Roach        // Default for application
897b9aa048SGreg Roach        yield new WebtreesTheme();
90b5979037SGreg Roach    }
91b5979037SGreg Roach}
92