Skip to content

Commit c7e848f

Browse files
authored
[dotenv] Relocate commands. (#3802)
1 parent 5f1d955 commit c7e848f

7 files changed

Lines changed: 390 additions & 0 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace Drupal\Console\Command\Debug;
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\Console\Core\Utils\DrupalFinder;
10+
11+
/**
12+
* Class DebugCommand
13+
*
14+
* @package Drupal\Console\Command\Debug
15+
*/
16+
class DotenvCommand extends Command
17+
{
18+
/**
19+
* @var DrupalFinder
20+
*/
21+
protected $drupalFinder;
22+
23+
/**
24+
* InitCommand constructor.
25+
*
26+
* @param DrupalFinder $drupalFinder
27+
*/
28+
public function __construct(
29+
DrupalFinder $drupalFinder
30+
) {
31+
$this->drupalFinder = $drupalFinder;
32+
parent::__construct();
33+
}
34+
35+
protected function configure()
36+
{
37+
$this->setName('debug:dotenv')
38+
->setDescription('Debug Dotenv debug values.');
39+
}
40+
41+
/**
42+
* {@inheritdoc}
43+
*/
44+
protected function execute(InputInterface $input, OutputInterface $output)
45+
{
46+
$fs = new Filesystem();
47+
$envFile = $this->drupalFinder->getComposerRoot() . '/.env';
48+
if (!$fs->exists($envFile)) {
49+
$this->getIo()->warning('File '. $envFile . ' not found.');
50+
51+
return 1;
52+
}
53+
54+
$fileContent = file_get_contents($envFile);
55+
$this->getIo()->writeln($fileContent);
56+
}
57+
}

src/Command/Dotenv/InitCommand.php

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
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+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Contains Drupal\Console\Generator\DotenvInitGenerator.
6+
*/
7+
namespace Drupal\Console\Generator;
8+
9+
use Symfony\Component\Filesystem\Filesystem;
10+
use Drupal\Console\Core\Generator\Generator;
11+
12+
/**
13+
* Class InitGenerator
14+
*
15+
* @package Drupal\Console\Generator
16+
*/
17+
class DotenvInitGenerator extends Generator
18+
{
19+
/**
20+
* {@inheritdoc}
21+
*/
22+
public function generate(array $parameters)
23+
{
24+
$io = $parameters['io'];
25+
$envParameters = $parameters['env_parameters'];
26+
$consoleRoot = $parameters['console_root '];
27+
$fs = new Filesystem();
28+
$envFile = $consoleRoot . '/.env';
29+
30+
if ($fs->exists($envFile)) {
31+
$fs->rename(
32+
$envFile,
33+
$envFile.'.original',
34+
true
35+
);
36+
37+
$io->success('File .env.original created.');
38+
}
39+
40+
$this->renderFile(
41+
'.env.dist.twig',
42+
$consoleRoot . '/.env',
43+
$envParameters
44+
);
45+
46+
$io->success("File .env created.");
47+
}
48+
}

templates/.env.dist.twig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# ENV
2+
ENVIRONMENT={{ environment }}
3+
4+
# Database
5+
DATABASE_NAME={{ database_name }}
6+
DATABASE_USER={{ database_user }}
7+
DATABASE_PASSWORD={{ database_password }}
8+
DATABASE_HOST={{ database_host }}
9+
DATABASE_PORT={{ database_port }}
10+
11+
# SETTINGS
12+
#SETTINGS_BAR=baz
13+
#SETTINGS_LOREM=ipsum

templates/files/.gitignore.dist

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
# dotenv file
3+
.env

templates/files/settings.dist

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
2+
// Load .env file if exists
3+
if (file_exists(dirname(DRUPAL_ROOT) . '/.env')) {
4+
// Load environment
5+
// make sure you add the vlucas/phpdotenv dependency using:
6+
// composer require vlucas/phpdotenv
7+
$dotenv = new \Dotenv\Dotenv(dirname(DRUPAL_ROOT));
8+
$dotenv->load();
9+
}
10+
11+
# Load environment
12+
$env = getenv('ENVIRONMENT');
13+
14+
# Load key/value settings
15+
$settings_drupal = array_filter(
16+
$_SERVER,
17+
function($key) {
18+
return strpos($key, 'SETTINGS_') === 0;
19+
},
20+
ARRAY_FILTER_USE_KEY
21+
);
22+
23+
# Set key/value settings
24+
foreach ($settings_drupal as $name => $value) {
25+
if (substr($name, 0, 9) === 'SETTINGS_') {
26+
$key = strtolower(substr($name, 9));
27+
$settings[$key] = $value;
28+
}
29+
}
30+
31+
$base_path = $app_root . '/' . $site_path;
32+
$servicesFile = $base_path . '/services.'.$env.'.yml';
33+
$settingsFile = $base_path . '/settings.'.$env.'.php';
34+
35+
// Load services definition file.
36+
if (file_exists($servicesFile)) {
37+
$settings['container_yamls'][] = $servicesFile;
38+
}
39+
40+
// Load settings file.
41+
if (file_exists($settingsFile)) {
42+
include $settingsFile;
43+
}
44+
45+
$databases['default']['default'] = array (
46+
'database' => getenv('DATABASE_NAME'),
47+
'username' => getenv('DATABASE_USER'),
48+
'password' => getenv('DATABASE_PASSWORD'),
49+
'prefix' => '',
50+
'host' => getenv('DATABASE_HOST'),
51+
'port' => getenv('DATABASE_PORT'),
52+
'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
53+
'driver' => 'mysql',
54+
);
55+

0 commit comments

Comments
 (0)