|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\Console\Command\Dotenv; |
| 4 | + |
| 5 | +use Symfony\Component\Console\Input\InputInterface; |
| 6 | +use Symfony\Component\Console\Output\OutputInterface; |
| 7 | +use Drupal\Console\Core\Command\Command; |
| 8 | +use Symfony\Component\Filesystem\Filesystem; |
| 9 | +use Drupal\Component\Utility\Crypt; |
| 10 | +use Drupal\Console\Generator\DotenvInitGenerator; |
| 11 | +use Webmozart\PathUtil\Path; |
| 12 | +use Drupal\Console\Core\Utils\DrupalFinder; |
| 13 | + |
| 14 | +/** |
| 15 | + * Class InitCommand |
| 16 | + * |
| 17 | + * @package Drupal\Console\Command\Dotenv |
| 18 | + */ |
| 19 | +class InitCommand extends Command |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @var DrupalFinder |
| 23 | + */ |
| 24 | + protected $drupalFinder; |
| 25 | + |
| 26 | + /** |
| 27 | + * InitCommand constructor. |
| 28 | + * |
| 29 | + * @param DrupalFinder $drupalFinder |
| 30 | + */ |
| 31 | + |
| 32 | + /** |
| 33 | + * @var DotenvInitGenerator |
| 34 | + */ |
| 35 | + protected $generator; |
| 36 | + |
| 37 | + private $envParameters = [ |
| 38 | + 'environment' => 'local', |
| 39 | + 'database_name' => 'drupal', |
| 40 | + 'database_user' => 'drupal', |
| 41 | + 'database_password' => 'drupal', |
| 42 | + 'database_host' => '127.0.0.1', |
| 43 | + 'database_port' => '3306', |
| 44 | + ]; |
| 45 | + |
| 46 | + /** |
| 47 | + * InitCommand constructor. |
| 48 | + * |
| 49 | + * @param DrupalFinder $drupalFinder |
| 50 | + * @param DotenvInitGenerator $generator |
| 51 | + */ |
| 52 | + public function __construct( |
| 53 | + DrupalFinder $drupalFinder, |
| 54 | + DotenvInitGenerator $generator |
| 55 | + ) { |
| 56 | + $this->drupalFinder = $drupalFinder; |
| 57 | + $this->generator = $generator; |
| 58 | + parent::__construct(); |
| 59 | + } |
| 60 | + |
| 61 | + protected function configure() |
| 62 | + { |
| 63 | + $this->setName('dotenv:init') |
| 64 | + ->setDescription('Dotenv initializer.'); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * {@inheritdoc} |
| 69 | + */ |
| 70 | + protected function interact(InputInterface $input, OutputInterface $output) |
| 71 | + { |
| 72 | + foreach ($this->envParameters as $key => $value) { |
| 73 | + $this->envParameters[$key] = $this->getIo()->ask( |
| 74 | + 'Enter value for ' . strtoupper($key), |
| 75 | + $value |
| 76 | + ); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * {@inheritdoc} |
| 82 | + */ |
| 83 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 84 | + { |
| 85 | + $this->copyFiles(); |
| 86 | + |
| 87 | + $this->generator->generate([ |
| 88 | + 'io' => $this->getIo(), |
| 89 | + 'env_parameters' => $this->envParameters, |
| 90 | + 'console_root' => $this->drupalFinder->getComposerRoot(), |
| 91 | + ]); |
| 92 | + } |
| 93 | + |
| 94 | + private function copyFiles() |
| 95 | + { |
| 96 | + $fs = new Filesystem(); |
| 97 | + $defaultSettingsFile = $this->drupalFinder->getDrupalRoot() . '/sites/default/default.settings.php'; |
| 98 | + $settingsFile = $this->drupalFinder->getDrupalRoot() . '/sites/default/settings.php'; |
| 99 | + |
| 100 | + if (!$fs->exists($defaultSettingsFile)) { |
| 101 | + $defaultSettingsFile = Path::makeRelative( |
| 102 | + $defaultSettingsFile, |
| 103 | + $this->drupalFinder->getComposerRoot() |
| 104 | + ); |
| 105 | + $this->getIo()->error('File: ' . $defaultSettingsFile . 'not found.'); |
| 106 | + |
| 107 | + return 1; |
| 108 | + } |
| 109 | + |
| 110 | + if ($fs->exists($settingsFile)) { |
| 111 | + $settingsFileOriginal = $settingsFile.'.original'; |
| 112 | + if (!$fs->exists($settingsFileOriginal)) { |
| 113 | + $fs->rename( |
| 114 | + $settingsFile, |
| 115 | + $settingsFileOriginal, |
| 116 | + true |
| 117 | + ); |
| 118 | + |
| 119 | + |
| 120 | + $settingsOriginalFile = Path::makeRelative( |
| 121 | + $settingsFile, |
| 122 | + $this->drupalFinder->getComposerRoot() |
| 123 | + ); |
| 124 | + |
| 125 | + $this->getIo()->success('File '.$settingsOriginalFile.'.original created.'); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + $fs->copy( |
| 130 | + $defaultSettingsFile, |
| 131 | + $settingsFile |
| 132 | + ); |
| 133 | + |
| 134 | + include_once $this->drupalFinder->getDrupalRoot() . '/core/includes/bootstrap.inc'; |
| 135 | + include_once $this->drupalFinder->getDrupalRoot() . '/core/includes/install.inc'; |
| 136 | + |
| 137 | + $settings['config_directories'] = [ |
| 138 | + CONFIG_SYNC_DIRECTORY => (object) [ |
| 139 | + 'value' => Path::makeRelative( |
| 140 | + $this->drupalFinder->getComposerRoot() . '/config/sync', |
| 141 | + $this->drupalFinder->getDrupalRoot() |
| 142 | + ), |
| 143 | + 'required' => true, |
| 144 | + ], |
| 145 | + ]; |
| 146 | + |
| 147 | + $settings['settings']['hash_salt'] = (object) [ |
| 148 | + 'value' => Crypt::randomBytesBase64(55), |
| 149 | + 'required' => true, |
| 150 | + ]; |
| 151 | + |
| 152 | + drupal_rewrite_settings($settings, $settingsFile); |
| 153 | + |
| 154 | + $settingsFileContent = file_get_contents($settingsFile); |
| 155 | + file_put_contents( |
| 156 | + $settingsFile, |
| 157 | + $settingsFileContent . |
| 158 | + file_get_contents( |
| 159 | + $this->drupalFinder->getConsolePath() . 'templates/files/settings.dist' |
| 160 | + ) |
| 161 | + ); |
| 162 | + |
| 163 | + $fs->chmod($settingsFile, 0666); |
| 164 | + |
| 165 | + $settingsFile = Path::makeRelative( |
| 166 | + $settingsFile, |
| 167 | + $this->drupalFinder->getComposerRoot() |
| 168 | + ); |
| 169 | + |
| 170 | + $this->getIo()->success('File '.$settingsFile.' created.'); |
| 171 | + |
| 172 | + $gitIgnoreFile = $this->drupalFinder->getComposerRoot() . '/.gitignore'; |
| 173 | + $gitIgnoreExampleFile = $this->drupalFinder->getComposerRoot() . '/example.gitignore'; |
| 174 | + if (!$fs->exists($gitIgnoreFile)) { |
| 175 | + if (!$fs->exists($gitIgnoreExampleFile)) { |
| 176 | + $fs->copy( |
| 177 | + $gitIgnoreExampleFile, |
| 178 | + $gitIgnoreFile |
| 179 | + ); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + if ($fs->exists($gitIgnoreFile)) { |
| 184 | + $gitIgnoreContent = file_get_contents($gitIgnoreFile); |
| 185 | + if (strpos($gitIgnoreContent, '.env') === false) { |
| 186 | + file_put_contents( |
| 187 | + $gitIgnoreFile, |
| 188 | + $gitIgnoreContent . |
| 189 | + file_get_contents( |
| 190 | + $this->drupalFinder->getConsolePath() . 'templates/files/.gitignore.dist' |
| 191 | + ) |
| 192 | + ); |
| 193 | + |
| 194 | + $this->getIo()->success("File .gitignore updated."); |
| 195 | + } |
| 196 | + } |
| 197 | + } |
| 198 | +} |
0 commit comments