xref: /webtrees/app/Http/Middleware/ReadConfigIni.php (revision 89f7189b61a494347591c99bdb92afb7d8b66e1b)
1e16a1bfdSGreg Roach<?php
23976b470SGreg Roach
3e16a1bfdSGreg Roach/**
4e16a1bfdSGreg Roach * webtrees: online genealogy
5*89f7189bSGreg 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
15*89f7189bSGreg 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{
37430ed1cbSGreg Roach    /** @var SetupWizard */
38430ed1cbSGreg Roach    private $setup_wizard;
39e16a1bfdSGreg Roach
40e16a1bfdSGreg Roach    /**
41430ed1cbSGreg Roach     * @param SetupWizard $setup_wizard
42e16a1bfdSGreg Roach     */
43430ed1cbSGreg Roach    public function __construct(SetupWizard $setup_wizard)
44e16a1bfdSGreg Roach    {
45430ed1cbSGreg Roach        $this->setup_wizard = $setup_wizard;
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            }
64430ed1cbSGreg Roach        } else {
65430ed1cbSGreg Roach            // No configuration file? Run the setup wizard to create one.
66430ed1cbSGreg Roach            $handler = $this->setup_wizard;
67e16a1bfdSGreg Roach        }
68e16a1bfdSGreg Roach
69430ed1cbSGreg Roach        return $handler->handle($request);
70e16a1bfdSGreg Roach    }
71e16a1bfdSGreg Roach}
72