xref: /webtrees/app/Http/Middleware/UseTheme.php (revision 455a30fe529581f1d53045f9c38a0674472d498f)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 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\Services\ModuleService;
25use Fisharebest\Webtrees\Session;
26use Fisharebest\Webtrees\Site;
27use Generator;
28use Psr\Http\Message\ResponseInterface;
29use Psr\Http\Message\ServerRequestInterface;
30use Psr\Http\Server\MiddlewareInterface;
31use Psr\Http\Server\RequestHandlerInterface;
32
33use function app;
34
35/**
36 * Middleware to select a theme.
37 */
38class UseTheme implements MiddlewareInterface
39{
40    /** @var ModuleService */
41    private $module_service;
42
43    /**
44     * UseTheme constructor.
45     *
46     * @param ModuleService $module_service
47     */
48    public function __construct(ModuleService $module_service)
49    {
50        $this->module_service = $module_service;
51    }
52
53    /**
54     * @param ServerRequestInterface  $request
55     * @param RequestHandlerInterface $handler
56     *
57     * @return ResponseInterface
58     */
59    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
60    {
61        foreach ($this->themes() as $theme) {
62            if ($theme instanceof ModuleThemeInterface) {
63                app()->instance(ModuleThemeInterface::class, $theme);
64                $request = $request->withAttribute('theme', $theme);
65                Session::put('theme', $theme->name());
66                break;
67            }
68        }
69
70        return $handler->handle($request);
71    }
72
73    /**
74     * The theme can be chosen in various ways.
75     *
76     * @return Generator<ModuleThemeInterface|null>
77     */
78    private function themes(): Generator
79    {
80        $themes = $this->module_service->findByInterface(ModuleThemeInterface::class);
81
82        // Last theme used
83        yield $themes->get(Session::get('theme'));
84
85        // Default for site
86        yield $themes->get(Site::getPreference('THEME_DIR'));
87
88        // Default for application
89        yield app(WebtreesTheme::class);
90    }
91}
92