xref: /webtrees/app/Http/Middleware/ReadConfigIni.php (revision e16a1bfdc151cca06b727ccf08cd4f43ed7d6e55)
1*e16a1bfdSGreg Roach<?php
2*e16a1bfdSGreg Roach/**
3*e16a1bfdSGreg Roach * webtrees: online genealogy
4*e16a1bfdSGreg Roach * Copyright (C) 2019 webtrees development team
5*e16a1bfdSGreg Roach * This program is free software: you can redistribute it and/or modify
6*e16a1bfdSGreg Roach * it under the terms of the GNU General Public License as published by
7*e16a1bfdSGreg Roach * the Free Software Foundation, either version 3 of the License, or
8*e16a1bfdSGreg Roach * (at your option) any later version.
9*e16a1bfdSGreg Roach * This program is distributed in the hope that it will be useful,
10*e16a1bfdSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
11*e16a1bfdSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12*e16a1bfdSGreg Roach * GNU General Public License for more details.
13*e16a1bfdSGreg Roach * You should have received a copy of the GNU General Public License
14*e16a1bfdSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
15*e16a1bfdSGreg Roach */
16*e16a1bfdSGreg Roachdeclare(strict_types=1);
17*e16a1bfdSGreg Roach
18*e16a1bfdSGreg Roachnamespace Fisharebest\Webtrees\Http\Middleware;
19*e16a1bfdSGreg Roach
20*e16a1bfdSGreg Roachuse function file_exists;
21*e16a1bfdSGreg Roachuse Fisharebest\Webtrees\Http\Controllers\SetupController;
22*e16a1bfdSGreg Roachuse Fisharebest\Webtrees\Webtrees;
23*e16a1bfdSGreg Roachuse Psr\Http\Message\ResponseInterface;
24*e16a1bfdSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
25*e16a1bfdSGreg Roachuse Psr\Http\Server\MiddlewareInterface;
26*e16a1bfdSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
27*e16a1bfdSGreg Roachuse function parse_ini_file;
28*e16a1bfdSGreg Roach
29*e16a1bfdSGreg Roach/**
30*e16a1bfdSGreg Roach * Middleware to read (or create) the webtrees configuration file.
31*e16a1bfdSGreg Roach */
32*e16a1bfdSGreg Roachclass ReadConfigIni implements MiddlewareInterface
33*e16a1bfdSGreg Roach{
34*e16a1bfdSGreg Roach    /** @var SetupController $controller */
35*e16a1bfdSGreg Roach    private $setup_controller;
36*e16a1bfdSGreg Roach
37*e16a1bfdSGreg Roach    /**
38*e16a1bfdSGreg Roach     * @param SetupController $setup_controller
39*e16a1bfdSGreg Roach     */
40*e16a1bfdSGreg Roach    public function __construct(SetupController $setup_controller)
41*e16a1bfdSGreg Roach    {
42*e16a1bfdSGreg Roach        $this->setup_controller = $setup_controller;
43*e16a1bfdSGreg Roach    }
44*e16a1bfdSGreg Roach
45*e16a1bfdSGreg Roach    /**
46*e16a1bfdSGreg Roach     * @param ServerRequestInterface  $request
47*e16a1bfdSGreg Roach     * @param RequestHandlerInterface $handler
48*e16a1bfdSGreg Roach     *
49*e16a1bfdSGreg Roach     * @return ResponseInterface
50*e16a1bfdSGreg Roach     */
51*e16a1bfdSGreg Roach    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
52*e16a1bfdSGreg Roach    {
53*e16a1bfdSGreg Roach        // Read the configuration settings.
54*e16a1bfdSGreg Roach        if (file_exists(Webtrees::CONFIG_FILE)) {
55*e16a1bfdSGreg Roach            $config = parse_ini_file(Webtrees::CONFIG_FILE);
56*e16a1bfdSGreg Roach
57*e16a1bfdSGreg Roach            // Store the configuration settings as request attributes.
58*e16a1bfdSGreg Roach            foreach ($config as $key => $value) {
59*e16a1bfdSGreg Roach                $request = $request->withAttribute($key, $value);
60*e16a1bfdSGreg Roach            }
61*e16a1bfdSGreg Roach
62*e16a1bfdSGreg Roach            return $handler->handle($request);
63*e16a1bfdSGreg Roach        }
64*e16a1bfdSGreg Roach
65*e16a1bfdSGreg Roach        // No configuration file? Run the setup wizard to create one.
66*e16a1bfdSGreg Roach        return $this->setup_controller->setup($request);
67*e16a1bfdSGreg Roach    }
68*e16a1bfdSGreg Roach}
69