xref: /webtrees/app/Http/Middleware/UseTheme.php (revision d812eb6b3dd6c33a96d07cd964cb4c3b88cec447)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 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 <http://www.gnu.org/licenses/>.
16 */
17declare(strict_types=1);
18
19namespace Fisharebest\Webtrees\Http\Middleware;
20
21use Fisharebest\Webtrees\Module\ModuleThemeInterface;
22use Fisharebest\Webtrees\Module\WebtreesTheme;
23use Fisharebest\Webtrees\Services\ModuleService;
24use Fisharebest\Webtrees\Session;
25use Fisharebest\Webtrees\Site;
26use Fisharebest\Webtrees\Tree;
27use Generator;
28use Psr\Http\Message\ResponseInterface;
29use Psr\Http\Message\ServerRequestInterface;
30use Psr\Http\Server\MiddlewareInterface;
31use Psr\Http\Server\RequestHandlerInterface;
32
33/**
34 * Middleware to set a global theme.
35 */
36class UseTheme implements MiddlewareInterface
37{
38    /** @var ModuleService */
39    private $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($request) as $theme) {
60            if ($theme instanceof ModuleThemeInterface) {
61                // Bind this theme into the container
62                app()->instance(ModuleThemeInterface::class, $theme);
63
64                // Remember this setting
65                Session::put('theme', $theme->name());
66
67                break;
68            }
69        }
70
71        return $handler->handle($request);
72    }
73
74    /**
75     * The theme can be chosen in various ways.
76     *
77     * @param ServerRequestInterface $request
78     *
79     * @return Generator
80     */
81    private function themes(ServerRequestInterface $request): Generator
82    {
83        $themes = $this->module_service->findByInterface(ModuleThemeInterface::class);
84
85        // Last theme used
86        yield $themes->get(Session::get('theme', ''));
87
88        // Default for tree
89        $tree = $request->getAttribute('tree');
90
91        if ($tree instanceof Tree) {
92            yield $themes->get($tree->getPreference('THEME_DIR'));
93        }
94
95        // Default for site
96        yield $themes->get(Site::getPreference('THEME_DIR'));
97
98        // Default for application
99        yield app(WebtreesTheme::class);
100    }
101}
102