3 namespace Drupal\Console\Command;
5 use Symfony\Component\Console\Input\InputInterface;
6 use Symfony\Component\Console\Input\InputOption;
7 use Symfony\Component\Console\Output\OutputInterface;
8 use Drupal\Console\Core\Command\GenerateCommand;
9 use Symfony\Component\Filesystem\Filesystem;
10 use Drupal\Component\Utility\Crypt;
11 use Drupal\Console\Generator\DotenvInitGenerator;
12 use Webmozart\PathUtil\Path;
17 * @package Drupal\Console\Command\Dotenv
19 class DotenvInitCommand extends GenerateCommand
22 * @var DotenvInitGenerator
26 private $envParameters = [
27 'environment' => 'develop',
28 'database_name' => 'drupal',
29 'database_user' => 'drupal',
30 'database_password' => 'drupal',
31 'database_host' => 'mariadb',
32 'database_port' => '3306',
33 'host_name' => 'drupal.develop',
35 'drupal_root' => '/var/www/html',
36 'server_root' => '/var/www/html/web'
40 * InitCommand constructor.
42 * @param DotenvInitGenerator $generator
44 public function __construct(
45 DotenvInitGenerator $generator
47 $this->generator = $generator;
48 parent::__construct();
51 protected function configure()
53 $this->setName('dotenv:init')
54 ->setDescription($this->trans('commands.dotenv.init.description'))
58 InputOption::VALUE_NONE,
59 $this->trans('commands.dotenv.init.options.load-from-env')
64 InputOption::VALUE_NONE,
65 $this->trans('commands.dotenv.init.options.load-settings')
72 protected function interact(InputInterface $input, OutputInterface $output)
74 foreach ($this->envParameters as $key => $value) {
75 $this->envParameters[$key] = $this->getIo()->ask(
76 'Enter value for ' . strtoupper($key),
85 protected function execute(InputInterface $input, OutputInterface $output)
87 $fs = new Filesystem();
88 $loadFromEnv = $input->getOption('load-from-env');
89 $loadSettings = $input->getOption('load-settings');
91 $this->envParameters['load_from_env'] = $loadFromEnv;
94 $this->envParameters['load_settings'] = $loadSettings;
96 $this->copySettingsFile($fs);
97 $this->copyEnvFile($fs);
99 $this->generator->setIo($this->getIo());
100 $this->generator->generate($this->envParameters);
103 protected function copySettingsFile(Filesystem $fs)
105 $sourceFile = $this->drupalFinder
106 ->getDrupalRoot() . '/sites/default/default.settings.php';
107 $destinationFile = $this->drupalFinder
108 ->getDrupalRoot() . '/sites/default/settings.php';
110 $directory = dirname($sourceFile);
111 $permissions = fileperms($directory);
112 $fs->chmod($directory, 0755);
114 $this->validateFileExists($fs, $sourceFile);
115 $this->backUpFile($fs, $destinationFile);
122 $this->validateFileExists($fs, $destinationFile);
124 include_once $this->drupalFinder->getDrupalRoot() . '/core/includes/bootstrap.inc';
125 include_once $this->drupalFinder->getDrupalRoot() . '/core/includes/install.inc';
127 $settings['config_directories'] = [
128 CONFIG_SYNC_DIRECTORY => (object) [
129 'value' => Path::makeRelative(
130 $this->drupalFinder->getComposerRoot() . '/config/sync',
131 $this->drupalFinder->getDrupalRoot()
137 $settings['settings']['hash_salt'] = (object) [
138 'value' => Crypt::randomBytesBase64(55),
142 drupal_rewrite_settings($settings, $destinationFile);
144 $this->showFileCreatedMessage($destinationFile);
146 $fs->chmod($directory, $permissions);
149 private function copyEnvFile(Filesystem $fs)
152 $this->drupalFinder->getComposerRoot() . '/example.gitignore',
153 $this->drupalFinder->getComposerRoot() . '/.gitignore'
156 $sourceFile = $this->validateFileExists($fs, $sourceFiles);
158 $destinationFile = $this->drupalFinder
159 ->getComposerRoot() . '/.gitignore';
161 if ($sourceFile !== $destinationFile) {
162 $this->backUpFile($fs, $destinationFile);
170 $this->validateFileExists($fs, $destinationFile);
172 $gitIgnoreContent = file_get_contents($destinationFile);
173 $gitIgnoreDistFile = $this->drupalFinder->getComposerRoot() .
174 $this->drupalFinder->getConsolePath() .
175 'templates/files/.gitignore.dist';
176 $gitIgnoreDistContent = file_get_contents($gitIgnoreDistFile);
178 if (strpos($gitIgnoreContent, '.env') === false) {
182 $gitIgnoreDistContent
186 $this->showFileCreatedMessage($destinationFile);