84e5346b3f0a68132ad195604147a9dee9bd06d3
[yaffs-website] / vendor / drupal / console / src / Command / DotenvInitCommand.php
1 <?php
2
3 namespace Drupal\Console\Command;
4
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;
13
14 /**
15  * Class InitCommand
16  *
17  * @package Drupal\Console\Command\Dotenv
18  */
19 class DotenvInitCommand extends GenerateCommand
20 {
21     /**
22      * @var DotenvInitGenerator
23      */
24     protected $generator;
25
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',
34         'host_port' => '80',
35         'drupal_root' => '/var/www/html',
36         'server_root' => '/var/www/html/web'
37     ];
38
39     /**
40      * InitCommand constructor.
41      *
42      * @param DotenvInitGenerator $generator
43      */
44     public function __construct(
45         DotenvInitGenerator $generator
46     ) {
47         $this->generator = $generator;
48         parent::__construct();
49     }
50
51     protected function configure()
52     {
53         $this->setName('dotenv:init')
54             ->setDescription($this->trans('commands.dotenv.init.description'))
55             ->addOption(
56                 'load-from-env',
57                 null,
58                 InputOption::VALUE_NONE,
59                 $this->trans('commands.dotenv.init.options.load-from-env')
60             )
61             ->addOption(
62                 'load-settings',
63                 null,
64                 InputOption::VALUE_NONE,
65                 $this->trans('commands.dotenv.init.options.load-settings')
66             );
67     }
68
69     /**
70      * {@inheritdoc}
71      */
72     protected function interact(InputInterface $input, OutputInterface $output)
73     {
74         foreach ($this->envParameters as $key => $value) {
75             $this->envParameters[$key] = $this->getIo()->ask(
76                 'Enter value for ' . strtoupper($key),
77                 $value
78             );
79         }
80     }
81
82     /**
83      * {@inheritdoc}
84      */
85     protected function execute(InputInterface $input, OutputInterface $output)
86     {
87         $fs = new Filesystem();
88         $loadFromEnv = $input->getOption('load-from-env');
89         $loadSettings = $input->getOption('load-settings');
90         if ($loadFromEnv) {
91             $this->envParameters['load_from_env'] = $loadFromEnv;
92         }
93         if ($loadSettings) {
94             $this->envParameters['load_settings'] = $loadSettings;
95         }
96         $this->copySettingsFile($fs);
97         $this->copyEnvFile($fs);
98
99         $this->generator->setIo($this->getIo());
100         $this->generator->generate($this->envParameters);
101     }
102
103     protected function copySettingsFile(Filesystem $fs)
104     {
105         $sourceFile = $this->drupalFinder
106                 ->getDrupalRoot() . '/sites/default/default.settings.php';
107         $destinationFile = $this->drupalFinder
108                 ->getDrupalRoot() . '/sites/default/settings.php';
109
110         $directory = dirname($sourceFile);
111         $permissions = fileperms($directory);
112         $fs->chmod($directory, 0755);
113
114         $this->validateFileExists($fs, $sourceFile);
115         $this->backUpFile($fs, $destinationFile);
116
117         $fs->copy(
118             $sourceFile,
119             $destinationFile
120         );
121
122         $this->validateFileExists($fs, $destinationFile);
123
124         include_once $this->drupalFinder->getDrupalRoot() . '/core/includes/bootstrap.inc';
125         include_once $this->drupalFinder->getDrupalRoot() . '/core/includes/install.inc';
126
127         $settings['config_directories'] = [
128             CONFIG_SYNC_DIRECTORY => (object) [
129                 'value' => Path::makeRelative(
130                     $this->drupalFinder->getComposerRoot() . '/config/sync',
131                     $this->drupalFinder->getDrupalRoot()
132                 ),
133                 'required' => true,
134             ],
135         ];
136
137         $settings['settings']['hash_salt'] = (object) [
138             'value'    => Crypt::randomBytesBase64(55),
139             'required' => true,
140         ];
141
142         drupal_rewrite_settings($settings, $destinationFile);
143
144         $this->showFileCreatedMessage($destinationFile);
145
146         $fs->chmod($directory, $permissions);
147     }
148
149     private function copyEnvFile(Filesystem $fs)
150     {
151         $sourceFiles = [
152             $this->drupalFinder->getComposerRoot() . '/example.gitignore',
153             $this->drupalFinder->getComposerRoot() . '/.gitignore'
154         ];
155
156         $sourceFile = $this->validateFileExists($fs, $sourceFiles);
157
158         $destinationFile = $this->drupalFinder
159                 ->getComposerRoot() . '/.gitignore';
160
161         if ($sourceFile !== $destinationFile) {
162             $this->backUpFile($fs, $destinationFile);
163         }
164
165         $fs->copy(
166             $sourceFile,
167             $destinationFile
168         );
169
170         $this->validateFileExists($fs, $destinationFile);
171
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);
177
178         if (strpos($gitIgnoreContent, '.env') === false) {
179             file_put_contents(
180                 $destinationFile,
181                 $gitIgnoreContent .
182                 $gitIgnoreDistContent
183             );
184         }
185
186         $this->showFileCreatedMessage($destinationFile);
187     }
188 }