xref: /webtrees/app/Http/Middleware/UseTheme.php (revision d239cb0bd52ae30e36592e8b68f180897a644140)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\Middleware;
21
22use Fisharebest\Webtrees\Module\ModuleThemeInterface;
23use Fisharebest\Webtrees\Module\WebtreesTheme;
24use Fisharebest\Webtrees\Registry;
25use Fisharebest\Webtrees\Services\ModuleService;
26use Fisharebest\Webtrees\Session;
27use Fisharebest\Webtrees\Site;
28use Generator;
29use Psr\Http\Message\ResponseInterface;
30use Psr\Http\Message\ServerRequestInterface;
31use Psr\Http\Server\MiddlewareInterface;
32use Psr\Http\Server\RequestHandlerInterface;
33
34/**
35 * Middleware to select a theme.
36 */
37class UseTheme implements MiddlewareInterface
38{
39    private ModuleService $module_service;
40
41    /**
42     * UseTheme constructor.
43     *
44     * @param ModuleService $module_service
45     */
46    public function __construct(ModuleService $module_service)
47    {
48        $this->module_service = $module_service;
49    }
50
51    /**
52     * @param ServerRequestInterface  $request
53     * @param RequestHandlerInterface $handler
54     *
55     * @return ResponseInterface
56     */
57    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
58    {
59        foreach ($this->themes() as $theme) {
60            if ($theme instanceof ModuleThemeInterface) {
61                Registry::container()->set(ModuleThemeInterface::class, $theme);
62                $request = $request->withAttribute('theme', $theme);
63                Session::put('theme', $theme->name());
64                break;
65            }
66        }
67
68        return $handler->handle($request);
69    }
70
71    /**
72     * The theme can be chosen in various ways.
73     *
74     * @return Generator<ModuleThemeInterface|null>
75     */
76    private function themes(): Generator
77    {
78        $themes = $this->module_service->findByInterface(ModuleThemeInterface::class);
79
80        // Last theme used
81        yield $themes
82            ->first(static fn (ModuleThemeInterface $module): bool => $module->name() === Session::get('theme'));
83
84        // Default for site
85        yield $themes
86            ->first(static fn (ModuleThemeInterface $module): bool => $module->name() === Site::getPreference('THEME_DIR'));
87
88        // Default for application
89        yield new WebtreesTheme();
90    }
91}
92