Completed
Push — develop ( fcf138...6e2abb )
by Greg
09:51 queued 04:00
created

UseLocale::handle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 11
rs 10
c 0
b 0
f 0
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
 */
16
declare(strict_types=1);
17
18
namespace Fisharebest\Webtrees\Http\Middleware;
19
20
use Closure;
21
use Fisharebest\Localization\Locale as WebtreesLocale;
22
use Fisharebest\Localization\Locale\LocaleInterface;
23
use Fisharebest\Webtrees\I18N;
24
use Fisharebest\Webtrees\Session;
25
use Fisharebest\Webtrees\Tree;
26
use Symfony\Component\HttpFoundation\Request;
27
use Symfony\Component\HttpFoundation\Response;
28
use Throwable;
29
30
/**
31
 * Middleware to set a global theme.
32
 */
33
class UseLocale implements MiddlewareInterface
34
{
35
    /**
36
     * @param Request $request
37
     * @param Closure $next
38
     *
39
     * @return Response
40
     * @throws Throwable
41
     */
42
    public function handle(Request $request, Closure $next): Response
43
    {
44
        $tree = app()->make(Tree::class);
45
46
        // Select a locale
47
        define('WT_LOCALE', I18N::init('', $tree));
48
        Session::put('locale', WT_LOCALE);
49
50
        app()->instance(LocaleInterface::class, WebtreesLocale::create(WT_LOCALE));
51
52
        return $next($request);
53
    }
54
}
55