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