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