xref: /webtrees/app/Http/Middleware/ReadConfigIni.php (revision c4943cff72f95a28fbb9404e3c20b169ff098e5c)
1e16a1bfdSGreg Roach<?php
23976b470SGreg Roach
3e16a1bfdSGreg Roach/**
4e16a1bfdSGreg Roach * webtrees: online genealogy
589f7189bSGreg Roach * Copyright (C) 2021 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
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16e16a1bfdSGreg Roach */
17fcfa147eSGreg Roach
18e16a1bfdSGreg Roachdeclare(strict_types=1);
19e16a1bfdSGreg Roach
20e16a1bfdSGreg Roachnamespace Fisharebest\Webtrees\Http\Middleware;
21e16a1bfdSGreg Roach
22430ed1cbSGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\SetupWizard;
23e16a1bfdSGreg Roachuse Fisharebest\Webtrees\Webtrees;
24e16a1bfdSGreg Roachuse Psr\Http\Message\ResponseInterface;
25e16a1bfdSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
26e16a1bfdSGreg Roachuse Psr\Http\Server\MiddlewareInterface;
27e16a1bfdSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
283976b470SGreg Roach
2971378461SGreg Roachuse function file_exists;
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{
37*c4943cffSGreg Roach    private SetupWizard $setup_wizard;
38e16a1bfdSGreg Roach
39e16a1bfdSGreg Roach    /**
40430ed1cbSGreg Roach     * @param SetupWizard $setup_wizard
41e16a1bfdSGreg Roach     */
42430ed1cbSGreg Roach    public function __construct(SetupWizard $setup_wizard)
43e16a1bfdSGreg Roach    {
44430ed1cbSGreg Roach        $this->setup_wizard = $setup_wizard;
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            }
63430ed1cbSGreg Roach        } else {
64430ed1cbSGreg Roach            // No configuration file? Run the setup wizard to create one.
65430ed1cbSGreg Roach            $handler = $this->setup_wizard;
66e16a1bfdSGreg Roach        }
67e16a1bfdSGreg Roach
68430ed1cbSGreg Roach        return $handler->handle($request);
69e16a1bfdSGreg Roach    }
70e16a1bfdSGreg Roach}
71