Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 73 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| install | |
0.00% |
0 / 73 |
|
0.00% |
0 / 4 |
90 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| configure | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
| execute | |
0.00% |
0 / 28 |
|
0.00% |
0 / 1 |
42 | |||
| register_configuration | |
0.00% |
0 / 32 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * |
| 4 | * This file is part of the phpBB Forum Software package. |
| 5 | * |
| 6 | * @copyright (c) phpBB Limited <https://www.phpbb.com> |
| 7 | * @license GNU General Public License, version 2 (GPL-2.0) |
| 8 | * |
| 9 | * For full copyright and license information, please see |
| 10 | * the docs/CREDITS.txt file. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | namespace phpbb\install\console\command\install; |
| 15 | |
| 16 | use phpbb\install\exception\installer_exception; |
| 17 | use phpbb\install\helper\install_helper; |
| 18 | use phpbb\install\helper\iohandler\cli_iohandler; |
| 19 | use phpbb\install\helper\iohandler\exception\iohandler_not_implemented_exception; |
| 20 | use phpbb\install\helper\iohandler\factory; |
| 21 | use phpbb\install\installer; |
| 22 | use phpbb\install\installer_configuration; |
| 23 | use phpbb\language\language; |
| 24 | use Symfony\Component\Config\Definition\Exception\Exception; |
| 25 | use Symfony\Component\Config\Definition\Processor; |
| 26 | use Symfony\Component\Console\Command\Command; |
| 27 | use Symfony\Component\Console\Input\InputArgument; |
| 28 | use Symfony\Component\Console\Input\InputInterface; |
| 29 | use Symfony\Component\Console\Output\OutputInterface; |
| 30 | use Symfony\Component\Console\Style\SymfonyStyle; |
| 31 | use Symfony\Component\Yaml\Exception\ParseException; |
| 32 | use Symfony\Component\Yaml\Yaml; |
| 33 | |
| 34 | class install extends \phpbb\console\command\command |
| 35 | { |
| 36 | /** |
| 37 | * @var factory |
| 38 | */ |
| 39 | protected $iohandler_factory; |
| 40 | |
| 41 | /** |
| 42 | * @var installer |
| 43 | */ |
| 44 | protected $installer; |
| 45 | |
| 46 | /** |
| 47 | * @var install_helper |
| 48 | */ |
| 49 | protected $install_helper; |
| 50 | |
| 51 | /** |
| 52 | * @var language |
| 53 | */ |
| 54 | protected $language; |
| 55 | |
| 56 | /** |
| 57 | * Constructor |
| 58 | * |
| 59 | * @param language $language |
| 60 | * @param factory $factory |
| 61 | * @param installer $installer |
| 62 | * @param install_helper $install_helper |
| 63 | */ |
| 64 | public function __construct(language $language, factory $factory, installer $installer, install_helper $install_helper) |
| 65 | { |
| 66 | $this->iohandler_factory = $factory; |
| 67 | $this->installer = $installer; |
| 68 | $this->language = $language; |
| 69 | $this->install_helper = $install_helper; |
| 70 | |
| 71 | parent::__construct(new \phpbb\user($language, 'datetime')); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * {@inheritdoc} |
| 76 | */ |
| 77 | protected function configure() |
| 78 | { |
| 79 | $this |
| 80 | ->setName('install') |
| 81 | ->addArgument( |
| 82 | 'config-file', |
| 83 | InputArgument::REQUIRED, |
| 84 | $this->language->lang('CLI_CONFIG_FILE')) |
| 85 | ->setDescription($this->language->lang('CLI_INSTALL_BOARD')) |
| 86 | ; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Executes the command install. |
| 91 | * |
| 92 | * Install the board |
| 93 | * |
| 94 | * @param InputInterface $input An InputInterface instance |
| 95 | * @param OutputInterface $output An OutputInterface instance |
| 96 | * |
| 97 | * @return int 0 if everything went fine, or a non-zero exit code |
| 98 | * @throws iohandler_not_implemented_exception |
| 99 | */ |
| 100 | protected function execute(InputInterface $input, OutputInterface $output): int |
| 101 | { |
| 102 | $this->iohandler_factory->set_environment('cli'); |
| 103 | |
| 104 | /** @var cli_iohandler $iohandler */ |
| 105 | $iohandler = $this->iohandler_factory->get(); |
| 106 | $style = new SymfonyStyle($input, $output); |
| 107 | $iohandler->set_style($style, $output); |
| 108 | |
| 109 | $this->installer->set_iohandler($iohandler); |
| 110 | |
| 111 | $config_file = $input->getArgument('config-file'); |
| 112 | |
| 113 | if ($this->install_helper->is_phpbb_installed()) |
| 114 | { |
| 115 | $iohandler->add_error_message('INSTALL_PHPBB_INSTALLED'); |
| 116 | |
| 117 | return Command::FAILURE; |
| 118 | } |
| 119 | |
| 120 | if (!is_file($config_file)) |
| 121 | { |
| 122 | $iohandler->add_error_message(array('MISSING_FILE', $config_file)); |
| 123 | |
| 124 | return Command::FAILURE; |
| 125 | } |
| 126 | |
| 127 | try |
| 128 | { |
| 129 | $config = Yaml::parse(file_get_contents($config_file), Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE); |
| 130 | } |
| 131 | catch (ParseException $e) |
| 132 | { |
| 133 | $iohandler->add_error_message(array('INVALID_YAML_FILE', $config_file)); |
| 134 | |
| 135 | return Command::FAILURE; |
| 136 | } |
| 137 | |
| 138 | $processor = new Processor(); |
| 139 | $configuration = new installer_configuration(); |
| 140 | |
| 141 | try |
| 142 | { |
| 143 | $config = $processor->processConfiguration($configuration, $config); |
| 144 | } |
| 145 | catch (Exception $e) |
| 146 | { |
| 147 | $iohandler->add_error_message('INVALID_CONFIGURATION', $e->getMessage()); |
| 148 | |
| 149 | return Command::FAILURE; |
| 150 | } |
| 151 | |
| 152 | $this->register_configuration($iohandler, $config); |
| 153 | |
| 154 | try |
| 155 | { |
| 156 | $this->installer->run(); |
| 157 | return Command::SUCCESS; |
| 158 | } |
| 159 | catch (installer_exception $e) |
| 160 | { |
| 161 | $iohandler->add_error_message($e->getMessage()); |
| 162 | return Command::FAILURE; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Register the configuration to simulate the forms. |
| 168 | * |
| 169 | * @param cli_iohandler $iohandler |
| 170 | * @param array $config |
| 171 | */ |
| 172 | private function register_configuration(cli_iohandler $iohandler, $config) |
| 173 | { |
| 174 | $iohandler->set_input('admin_name', $config['admin']['name']); |
| 175 | $iohandler->set_input('admin_pass1', $config['admin']['password']); |
| 176 | $iohandler->set_input('admin_pass2', $config['admin']['password']); |
| 177 | $iohandler->set_input('board_email', $config['admin']['email']); |
| 178 | $iohandler->set_input('submit_admin', 'submit'); |
| 179 | |
| 180 | $iohandler->set_input('default_lang', $config['board']['lang']); |
| 181 | $iohandler->set_input('board_name', $config['board']['name']); |
| 182 | $iohandler->set_input('board_description', $config['board']['description']); |
| 183 | $iohandler->set_input('submit_board', 'submit'); |
| 184 | |
| 185 | $iohandler->set_input('dbms', $config['database']['dbms']); |
| 186 | $iohandler->set_input('dbhost', $config['database']['dbhost']); |
| 187 | $iohandler->set_input('dbport', $config['database']['dbport']); |
| 188 | $iohandler->set_input('dbuser', $config['database']['dbuser']); |
| 189 | $iohandler->set_input('dbpasswd', $config['database']['dbpasswd']); |
| 190 | $iohandler->set_input('dbname', $config['database']['dbname']); |
| 191 | $iohandler->set_input('table_prefix', $config['database']['table_prefix']); |
| 192 | $iohandler->set_input('submit_database', 'submit'); |
| 193 | |
| 194 | $iohandler->set_input('email_enable', $config['email']['enabled']); |
| 195 | $iohandler->set_input('smtp_delivery', $config['email']['smtp_delivery']); |
| 196 | $iohandler->set_input('smtp_host', $config['email']['smtp_host']); |
| 197 | $iohandler->set_input('smtp_port', $config['email']['smtp_port']); |
| 198 | $iohandler->set_input('smtp_user', $config['email']['smtp_user']); |
| 199 | $iohandler->set_input('smtp_pass', $config['email']['smtp_pass']); |
| 200 | $iohandler->set_input('submit_email', 'submit'); |
| 201 | |
| 202 | $iohandler->set_input('cookie_secure', $config['server']['cookie_secure']); |
| 203 | $iohandler->set_input('server_protocol', $config['server']['server_protocol']); |
| 204 | $iohandler->set_input('force_server_vars', $config['server']['force_server_vars']); |
| 205 | $iohandler->set_input('server_name', $config['server']['server_name']); |
| 206 | $iohandler->set_input('server_port', $config['server']['server_port']); |
| 207 | $iohandler->set_input('script_path', $config['server']['script_path']); |
| 208 | $iohandler->set_input('submit_server', 'submit'); |
| 209 | |
| 210 | $iohandler->set_input('install-extensions', $config['extensions']); |
| 211 | } |
| 212 | } |