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