View source
<?php
namespace Drupal\Core\Installer\Form;
use Drupal\Component\Utility\Crypt;
use Drupal\Core\Database\Database;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Site\Settings;
use Symfony\Component\DependencyInjection\ContainerInterface;
class SiteSettingsForm extends FormBase {
protected $sitePath;
protected $renderer;
public function __construct($site_path, RendererInterface $renderer) {
$this->sitePath = $site_path;
$this->renderer = $renderer;
}
public static function create(ContainerInterface $container) {
return new static($container
->getParameter('site.path'), $container
->get('renderer'));
}
public function getFormId() {
return 'install_settings_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$settings_file = './' . $this->sitePath . '/settings.php';
$form['#title'] = $this
->t('Database configuration');
$drivers = drupal_get_database_types();
$drivers_keys = array_keys($drivers);
$input =& $form_state
->getUserInput();
if (!isset($input['driver']) && ($database = Database::getConnectionInfo())) {
$input['driver'] = $database['default']['driver'];
$input[$database['default']['driver']] = $database['default'];
}
if (isset($input['driver'])) {
$default_driver = $input['driver'];
$prefix =& $input[$default_driver]['prefix'];
if (isset($prefix) && is_array($prefix)) {
$prefix = $prefix['default'];
}
$default_options = $input[$default_driver];
}
else {
$default_driver = current($drivers_keys);
$default_options = [];
}
$form['driver'] = [
'#type' => 'radios',
'#title' => $this
->t('Database type'),
'#required' => TRUE,
'#default_value' => $default_driver,
];
if (count($drivers) == 1) {
$form['driver']['#disabled'] = TRUE;
}
foreach ($drivers as $key => $driver) {
$form['driver']['#options'][$key] = $driver
->name();
$form['settings'][$key] = $driver
->getFormOptions($default_options);
$form['settings'][$key]['#prefix'] = '<h2 class="js-hide">' . $this
->t('@driver_name settings', [
'@driver_name' => $driver
->name(),
]) . '</h2>';
$form['settings'][$key]['#type'] = 'container';
$form['settings'][$key]['#tree'] = TRUE;
$form['settings'][$key]['advanced_options']['#parents'] = [
$key,
];
$form['settings'][$key]['#states'] = [
'visible' => [
':input[name=driver]' => [
'value' => $key,
],
],
];
}
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['save'] = [
'#type' => 'submit',
'#value' => $this
->t('Save and continue'),
'#button_type' => 'primary',
'#limit_validation_errors' => [
[
'driver',
],
[
$default_driver,
],
],
'#submit' => [
'::submitForm',
],
];
$form['errors'] = [];
$form['settings_file'] = [
'#type' => 'value',
'#value' => $settings_file,
];
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$driver = $form_state
->getValue('driver');
$database = $form_state
->getValue($driver);
$drivers = drupal_get_database_types();
$reflection = new \ReflectionClass($drivers[$driver]);
$install_namespace = $reflection
->getNamespaceName();
$database['namespace'] = substr($install_namespace, 0, strrpos($install_namespace, '\\'));
$database['driver'] = $driver;
if ($autoload = Database::findDriverAutoloadDirectory($database['namespace'], DRUPAL_ROOT)) {
$database['autoload'] = $autoload;
}
$form_state
->set('database', $database);
foreach ($this
->getDatabaseErrors($database, $form_state
->getValue('settings_file')) as $name => $message) {
$form_state
->setErrorByName($name, $message);
}
}
protected function getDatabaseErrors(array $database, $settings_file) {
$errors = install_database_errors($database, $settings_file);
$form_errors = array_filter($errors, function ($value) {
return is_int($value);
});
$errors = array_diff_key($errors, $form_errors);
if (count($errors)) {
$error_message = static::getDatabaseErrorsTemplate($errors);
$form_errors[$database['driver'] . '][0'] = $this->renderer
->renderPlain($error_message);
}
return $form_errors;
}
public static function getDatabaseErrorsTemplate(array $errors) {
return [
'#type' => 'inline_template',
'#template' => '{% trans %}Resolve all issues below to continue the installation. For help configuring your database server, see the <a href="https://www.drupal.org/docs/8/install">installation handbook</a>, or contact your hosting provider.{% endtrans %}{{ errors }}',
'#context' => [
'errors' => [
'#theme' => 'item_list',
'#items' => $errors,
],
],
];
}
public function submitForm(array &$form, FormStateInterface $form_state) {
global $install_state;
$settings = [];
$database = $form_state
->get('database');
$settings['databases']['default']['default'] = (object) [
'value' => $database,
'required' => TRUE,
];
$settings['settings']['hash_salt'] = (object) [
'value' => Crypt::randomBytesBase64(55),
'required' => TRUE,
];
if (empty(Settings::get('config_sync_directory'))) {
if (empty($install_state['config_install_path'])) {
$config_sync_directory = $this
->createRandomConfigDirectory();
}
else {
$config_sync_directory = $install_state['config_install_path'];
}
$settings['settings']['config_sync_directory'] = (object) [
'value' => $config_sync_directory,
'required' => TRUE,
];
}
drupal_rewrite_settings($settings);
$install_state['settings_verified'] = TRUE;
$install_state['config_verified'] = TRUE;
$install_state['database_verified'] = TRUE;
$install_state['completed_task'] = install_verify_completed_task();
}
protected function createRandomConfigDirectory() {
$config_sync_directory = $this->sitePath . '/files/config_' . Crypt::randomBytesBase64(55) . '/sync';
if (\Drupal::service('file_system')
->prepareDirectory($config_sync_directory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS)) {
$text = 'This directory contains configuration to be imported into your Drupal site. To make this configuration active, visit admin/config/development/configuration/sync.' . ' For information about deploying configuration between servers, see https://www.drupal.org/documentation/administer/config';
file_put_contents($config_sync_directory . '/README.txt', $text);
}
return $config_sync_directory;
}
}