xref: /webtrees/app/Http/Middleware/ReadConfigIni.php (revision 71378461661e7642e52abe7d41c9cfffb3e5369b)
1e16a1bfdSGreg Roach<?php
23976b470SGreg 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 Fisharebest\Webtrees\Http\Controllers\SetupController;
22e16a1bfdSGreg Roachuse Fisharebest\Webtrees\Webtrees;
23e16a1bfdSGreg Roachuse Psr\Http\Message\ResponseInterface;
24e16a1bfdSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
25e16a1bfdSGreg Roachuse Psr\Http\Server\MiddlewareInterface;
26e16a1bfdSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
273976b470SGreg Roach
28*71378461SGreg Roachuse function file_exists;
29e16a1bfdSGreg Roachuse function parse_ini_file;
30e16a1bfdSGreg Roach
31e16a1bfdSGreg Roach/**
32e16a1bfdSGreg Roach * Middleware to read (or create) the webtrees configuration file.
33e16a1bfdSGreg Roach */
34e16a1bfdSGreg Roachclass ReadConfigIni implements MiddlewareInterface
35e16a1bfdSGreg Roach{
36e16a1bfdSGreg Roach    /** @var SetupController $controller */
37e16a1bfdSGreg Roach    private $setup_controller;
38e16a1bfdSGreg Roach
39e16a1bfdSGreg Roach    /**
40e16a1bfdSGreg Roach     * @param SetupController $setup_controller
41e16a1bfdSGreg Roach     */
42e16a1bfdSGreg Roach    public function __construct(SetupController $setup_controller)
43e16a1bfdSGreg Roach    {
44e16a1bfdSGreg Roach        $this->setup_controller = $setup_controller;
45e16a1bfdSGreg Roach    }
46e16a1bfdSGreg Roach
47e16a1bfdSGreg Roach    /**
48e16a1bfdSGreg Roach     * @param ServerRequestInterface  $request
49e16a1bfdSGreg Roach     * @param RequestHandlerInterface $handler
50e16a1bfdSGreg Roach     *
51e16a1bfdSGreg Roach     * @return ResponseInterface
52e16a1bfdSGreg Roach     */
53e16a1bfdSGreg Roach    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
54e16a1bfdSGreg Roach    {
55e16a1bfdSGreg Roach        // Read the configuration settings.
56e16a1bfdSGreg Roach        if (file_exists(Webtrees::CONFIG_FILE)) {
57e16a1bfdSGreg Roach            $config = parse_ini_file(Webtrees::CONFIG_FILE);
58e16a1bfdSGreg Roach
59e16a1bfdSGreg Roach            // Store the configuration settings as request attributes.
60e16a1bfdSGreg Roach            foreach ($config as $key => $value) {
61e16a1bfdSGreg Roach                $request = $request->withAttribute($key, $value);
62e16a1bfdSGreg Roach            }
63e16a1bfdSGreg Roach
64e16a1bfdSGreg Roach            return $handler->handle($request);
65e16a1bfdSGreg Roach        }
66e16a1bfdSGreg Roach
67e16a1bfdSGreg Roach        // No configuration file? Run the setup wizard to create one.
68e16a1bfdSGreg Roach        return $this->setup_controller->setup($request);
69e16a1bfdSGreg Roach    }
70e16a1bfdSGreg Roach}
71