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