xref: /webtrees/app/Cli/Console.php (revision 9c2569b5b5f2aeac55b2e3f374c9b43ef6ad79ef)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Cli;
21
22use Fisharebest\Webtrees\DB;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Registry;
25use Fisharebest\Webtrees\Webtrees;
26use Symfony\Component\Console\Application;
27
28use function parse_ini_file;
29
30final class Console extends Application
31{
32    public function loadCommands(): self
33    {
34        $commands = glob(pattern: __DIR__ . '/Commands/*.php') ?: [];
35
36        foreach ($commands as $command) {
37            $class = __NAMESPACE__ . '\\Commands\\' . basename(path: $command, suffix: '.php');
38
39            $this->add(Registry::container()->get($class));
40        }
41
42        return $this;
43    }
44
45    public function bootstrap(): self
46    {
47        I18N::init(code: 'en-US', setup: true);
48
49        $config = parse_ini_file(filename: Webtrees::CONFIG_FILE);
50
51        if ($config === false) {
52            return $this;
53        }
54
55        DB::connect(
56            driver: $config['dbtype'] ?? DB::MYSQL,
57            host: $config['dbhost'],
58            port: $config['dbport'],
59            database: $config['dbname'],
60            username: $config['dbuser'],
61            password: $config['dbpass'],
62            prefix: $config['tblpfx'],
63            key: $config['dbkey'] ?? '',
64            certificate: $config['dbcert'] ?? '',
65            ca: $config['dbca'] ?? '',
66            verify_certificate: (bool) ($config['dbverify'] ?? ''),
67        );
68
69        DB::exec('START TRANSACTION');
70
71        return $this;
72    }
73}
74