xref: /webtrees/app/Http/Middleware/UseTheme.php (revision 6ccdf4f0fd1b65a5d54259c969912382ce49629d)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees\Http\Middleware;
19
20use Fisharebest\Webtrees\Module\ModuleThemeInterface;
21use Fisharebest\Webtrees\Module\WebtreesTheme;
22use Fisharebest\Webtrees\Services\ModuleService;
23use Fisharebest\Webtrees\Session;
24use Fisharebest\Webtrees\Site;
25use Fisharebest\Webtrees\Tree;
26use Generator;
27use Psr\Http\Message\ResponseInterface;
28use Psr\Http\Message\ServerRequestInterface;
29use Psr\Http\Server\MiddlewareInterface;
30use Psr\Http\Server\RequestHandlerInterface;
31
32/**
33 * Middleware to set a global theme.
34 */
35class UseTheme implements MiddlewareInterface
36{
37    /** @var ModuleService */
38    private $module_service;
39
40    /**
41     * UseTheme constructor.
42     *
43     * @param ModuleService $module_service
44     */
45    public function __construct(ModuleService $module_service)
46    {
47        $this->module_service = $module_service;
48    }
49
50    /**
51     * @param ServerRequestInterface  $request
52     * @param RequestHandlerInterface $handler
53     *
54     * @return ResponseInterface
55     */
56    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
57    {
58        foreach ($this->themes() as $theme) {
59            if ($theme instanceof ModuleThemeInterface) {
60                // Bind this theme into the container
61                app()->instance(ModuleThemeInterface::class, $theme);
62
63                // Remember this setting
64                Session::put('theme_id', $theme->name());
65
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
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_id', ''));
84
85        // Default for tree
86        $tree = app(Tree::class);
87
88        if ($tree instanceof Tree) {
89            yield $themes->get($tree->getPreference('THEME_DIR'));
90        }
91
92        // Default for site
93        yield $themes->get(Site::getPreference('THEME_DIR'));
94
95        // Default for application
96        yield app(WebtreesTheme::class);
97    }
98}
99