xref: /webtrees/app/Http/Middleware/UseTheme.php (revision fcfa147e10aaa6c7ff580c29bd6e5b88666befc1)
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 */
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 Fisharebest\Webtrees\Tree;
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 set a global theme.
36 */
37class UseTheme implements MiddlewareInterface
38{
39    /** @var ModuleService */
40    private $module_service;
41
42    /**
43     * UseTheme constructor.
44     *
45     * @param ModuleService $module_service
46     */
47    public function __construct(ModuleService $module_service)
48    {
49        $this->module_service = $module_service;
50    }
51
52    /**
53     * @param ServerRequestInterface  $request
54     * @param RequestHandlerInterface $handler
55     *
56     * @return ResponseInterface
57     */
58    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
59    {
60        foreach ($this->themes($request) as $theme) {
61            if ($theme instanceof ModuleThemeInterface) {
62                // Bind this theme into the container
63                app()->instance(ModuleThemeInterface::class, $theme);
64
65                // Remember this setting
66                Session::put('theme', $theme->name());
67
68                break;
69            }
70        }
71
72        return $handler->handle($request);
73    }
74
75    /**
76     * The theme can be chosen in various ways.
77     *
78     * @param ServerRequestInterface $request
79     *
80     * @return Generator
81     */
82    private function themes(ServerRequestInterface $request): Generator
83    {
84        $themes = $this->module_service->findByInterface(ModuleThemeInterface::class);
85
86        // Last theme used
87        yield $themes->get(Session::get('theme', ''));
88
89        // Default for tree
90        $tree = $request->getAttribute('tree');
91
92        if ($tree instanceof Tree) {
93            yield $themes->get($tree->getPreference('THEME_DIR'));
94        }
95
96        // Default for site
97        yield $themes->get(Site::getPreference('THEME_DIR'));
98
99        // Default for application
100        yield app(WebtreesTheme::class);
101    }
102}
103