xref: /webtrees/app/Http/Middleware/UseTheme.php (revision f1d4b4a2fcc65f3b3bd1d9fb6ff4b678a9e0aea4)
1b5979037SGreg Roach<?php
23976b470SGreg Roach
3b5979037SGreg Roach/**
4b5979037SGreg Roach * webtrees: online genealogy
5b5979037SGreg Roach * Copyright (C) 2019 webtrees development team
6b5979037SGreg Roach * This program is free software: you can redistribute it and/or modify
7b5979037SGreg Roach * it under the terms of the GNU General Public License as published by
8b5979037SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9b5979037SGreg Roach * (at your option) any later version.
10b5979037SGreg Roach * This program is distributed in the hope that it will be useful,
11b5979037SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12b5979037SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13b5979037SGreg Roach * GNU General Public License for more details.
14b5979037SGreg Roach * You should have received a copy of the GNU General Public License
15b5979037SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
16b5979037SGreg Roach */
17b5979037SGreg Roachdeclare(strict_types=1);
18b5979037SGreg Roach
19b5979037SGreg Roachnamespace Fisharebest\Webtrees\Http\Middleware;
20b5979037SGreg Roach
21b5979037SGreg Roachuse Fisharebest\Webtrees\Module\ModuleThemeInterface;
22b5979037SGreg Roachuse Fisharebest\Webtrees\Module\WebtreesTheme;
23b5979037SGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
24b5979037SGreg Roachuse Fisharebest\Webtrees\Session;
25b5979037SGreg Roachuse Fisharebest\Webtrees\Site;
26b5979037SGreg Roachuse Fisharebest\Webtrees\Tree;
27b5979037SGreg Roachuse Generator;
286ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
296ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
306ccdf4f0SGreg Roachuse Psr\Http\Server\MiddlewareInterface;
316ccdf4f0SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
32b5979037SGreg Roach
33b5979037SGreg Roach/**
34b5979037SGreg Roach * Middleware to set a global theme.
35b5979037SGreg Roach */
36b5979037SGreg Roachclass UseTheme implements MiddlewareInterface
37b5979037SGreg Roach{
38b5979037SGreg Roach    /** @var ModuleService */
39b5979037SGreg Roach    private $module_service;
40b5979037SGreg Roach
41b5979037SGreg Roach    /**
42b5979037SGreg Roach     * UseTheme constructor.
43b5979037SGreg Roach     *
44b5979037SGreg Roach     * @param ModuleService $module_service
45b5979037SGreg Roach     */
4617b3ba76SGreg Roach    public function __construct(ModuleService $module_service)
47b5979037SGreg Roach    {
48b5979037SGreg Roach        $this->module_service = $module_service;
49b5979037SGreg Roach    }
50b5979037SGreg Roach
51b5979037SGreg Roach    /**
526ccdf4f0SGreg Roach     * @param ServerRequestInterface  $request
536ccdf4f0SGreg Roach     * @param RequestHandlerInterface $handler
54b5979037SGreg Roach     *
556ccdf4f0SGreg Roach     * @return ResponseInterface
56b5979037SGreg Roach     */
576ccdf4f0SGreg Roach    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
58b5979037SGreg Roach    {
59*f1d4b4a2SGreg Roach        foreach ($this->themes($request) as $theme) {
60b5979037SGreg Roach            if ($theme instanceof ModuleThemeInterface) {
61b5979037SGreg Roach                // Bind this theme into the container
62b5979037SGreg Roach                app()->instance(ModuleThemeInterface::class, $theme);
63b5979037SGreg Roach
64b5979037SGreg Roach                // Remember this setting
65a0801ffbSGreg Roach                Session::put('theme', $theme->name());
66b5979037SGreg Roach
67b5979037SGreg Roach                break;
68b5979037SGreg Roach            }
69b5979037SGreg Roach        }
70b5979037SGreg Roach
716ccdf4f0SGreg Roach        return $handler->handle($request);
72b5979037SGreg Roach    }
73b5979037SGreg Roach
74b5979037SGreg Roach    /**
75b5979037SGreg Roach     * The theme can be chosen in various ways.
76b5979037SGreg Roach     *
77*f1d4b4a2SGreg Roach     * @param ServerRequestInterface $request
78*f1d4b4a2SGreg Roach     *
79b5979037SGreg Roach     * @return Generator
80b5979037SGreg Roach     */
81*f1d4b4a2SGreg Roach    private function themes(ServerRequestInterface $request): Generator
82b5979037SGreg Roach    {
83b5979037SGreg Roach        $themes = $this->module_service->findByInterface(ModuleThemeInterface::class);
84b5979037SGreg Roach
85b5979037SGreg Roach        // Last theme used
86a0801ffbSGreg Roach        yield $themes->get(Session::get('theme', ''));
87b5979037SGreg Roach
88b5979037SGreg Roach        // Default for tree
89*f1d4b4a2SGreg Roach        $tree = $request->getAttribute('tree');
9017b3ba76SGreg Roach
9117b3ba76SGreg Roach        if ($tree instanceof Tree) {
9217b3ba76SGreg Roach            yield $themes->get($tree->getPreference('THEME_DIR'));
93b5979037SGreg Roach        }
94b5979037SGreg Roach
95b5979037SGreg Roach        // Default for site
96b5979037SGreg Roach        yield $themes->get(Site::getPreference('THEME_DIR'));
97b5979037SGreg Roach
98b5979037SGreg Roach        // Default for application
99cab242e7SGreg Roach        yield app(WebtreesTheme::class);
100b5979037SGreg Roach    }
101b5979037SGreg Roach}
102