xref: /webtrees/app/Cli/Console.php (revision e873f434551745f888937263ff89e80db3b0f785)
1b9a4a6c6SGreg Roach<?php
2b9a4a6c6SGreg Roach
3b9a4a6c6SGreg Roach/**
4b9a4a6c6SGreg Roach * webtrees: online genealogy
5b9a4a6c6SGreg Roach * Copyright (C) 2023 webtrees development team
6b9a4a6c6SGreg Roach * This program is free software: you can redistribute it and/or modify
7b9a4a6c6SGreg Roach * it under the terms of the GNU General Public License as published by
8b9a4a6c6SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9b9a4a6c6SGreg Roach * (at your option) any later version.
10b9a4a6c6SGreg Roach * This program is distributed in the hope that it will be useful,
11b9a4a6c6SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12b9a4a6c6SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13b9a4a6c6SGreg Roach * GNU General Public License for more details.
14b9a4a6c6SGreg Roach * You should have received a copy of the GNU General Public License
15b9a4a6c6SGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16b9a4a6c6SGreg Roach */
17b9a4a6c6SGreg Roach
18b9a4a6c6SGreg Roachdeclare(strict_types=1);
19b9a4a6c6SGreg Roach
20b9a4a6c6SGreg Roachnamespace Fisharebest\Webtrees\Cli;
21b9a4a6c6SGreg Roach
22b9a4a6c6SGreg Roachuse Fisharebest\Webtrees\DB;
23b9a4a6c6SGreg Roachuse Fisharebest\Webtrees\I18N;
24b9a4a6c6SGreg Roachuse Fisharebest\Webtrees\Registry;
25b9a4a6c6SGreg Roachuse Fisharebest\Webtrees\Webtrees;
26b9a4a6c6SGreg Roachuse Symfony\Component\Console\Application;
27b9a4a6c6SGreg Roach
28b9a4a6c6SGreg Roachuse function parse_ini_file;
29b9a4a6c6SGreg Roach
30b9a4a6c6SGreg Roachfinal class Console extends Application
31b9a4a6c6SGreg Roach{
32*e873f434SGreg Roach    private const array COMMANDS = [
33857432a0SGreg Roach        Commands\CompilePoFiles::class,
34857432a0SGreg Roach        Commands\TreeCreate::class,
35857432a0SGreg Roach        Commands\TreeList::class,
36857432a0SGreg Roach        Commands\UserCreate::class,
37857432a0SGreg Roach        Commands\UserList::class,
38857432a0SGreg Roach    ];
39857432a0SGreg Roach
40bb87bb9bSGreg Roach    public function __construct()
41bb87bb9bSGreg Roach    {
42bb87bb9bSGreg Roach        parent::__construct(Webtrees::NAME, Webtrees::VERSION);
43bb87bb9bSGreg Roach    }
44bb87bb9bSGreg Roach
45b9a4a6c6SGreg Roach    public function loadCommands(): self
46b9a4a6c6SGreg Roach    {
47857432a0SGreg Roach        foreach (self::COMMANDS as $command) {
48857432a0SGreg Roach            $this->add(Registry::container()->get($command));
49b9a4a6c6SGreg Roach        }
50b9a4a6c6SGreg Roach
51b9a4a6c6SGreg Roach        return $this;
52b9a4a6c6SGreg Roach    }
53b9a4a6c6SGreg Roach
54b9a4a6c6SGreg Roach    public function bootstrap(): self
55b9a4a6c6SGreg Roach    {
56b9a4a6c6SGreg Roach        I18N::init(code: 'en-US', setup: true);
57b9a4a6c6SGreg Roach
58b9a4a6c6SGreg Roach        $config = parse_ini_file(filename: Webtrees::CONFIG_FILE);
59b9a4a6c6SGreg Roach
60b9a4a6c6SGreg Roach        if ($config === false) {
61b9a4a6c6SGreg Roach            return $this;
62b9a4a6c6SGreg Roach        }
63b9a4a6c6SGreg Roach
64b9a4a6c6SGreg Roach        DB::connect(
65b9a4a6c6SGreg Roach            driver: $config['dbtype'] ?? DB::MYSQL,
66b9a4a6c6SGreg Roach            host: $config['dbhost'],
67b9a4a6c6SGreg Roach            port: $config['dbport'],
68b9a4a6c6SGreg Roach            database: $config['dbname'],
69b9a4a6c6SGreg Roach            username: $config['dbuser'],
70b9a4a6c6SGreg Roach            password: $config['dbpass'],
71b9a4a6c6SGreg Roach            prefix: $config['tblpfx'],
72b9a4a6c6SGreg Roach            key: $config['dbkey'] ?? '',
73b9a4a6c6SGreg Roach            certificate: $config['dbcert'] ?? '',
74b9a4a6c6SGreg Roach            ca: $config['dbca'] ?? '',
75b9a4a6c6SGreg Roach            verify_certificate: (bool) ($config['dbverify'] ?? ''),
76b9a4a6c6SGreg Roach        );
77b9a4a6c6SGreg Roach
78b9a4a6c6SGreg Roach        DB::exec('START TRANSACTION');
79b9a4a6c6SGreg Roach
80b9a4a6c6SGreg Roach        return $this;
81b9a4a6c6SGreg Roach    }
82b9a4a6c6SGreg Roach}
83