Skip to content

Commit

Permalink
[dotenv] Relocate commands. (#3802)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmolivas authored Feb 21, 2018
1 parent 5f1d955 commit c7e848f
Show file tree
Hide file tree
Showing 7 changed files with 390 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/Command/Debug/DotenvCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Drupal\Console\Command\Debug;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Drupal\Console\Core\Command\Command;
use Symfony\Component\Filesystem\Filesystem;
use Drupal\Console\Core\Utils\DrupalFinder;

/**
* Class DebugCommand
*
* @package Drupal\Console\Command\Debug
*/
class DotenvCommand extends Command
{
/**
* @var DrupalFinder
*/
protected $drupalFinder;

/**
* InitCommand constructor.
*
* @param DrupalFinder $drupalFinder
*/
public function __construct(
DrupalFinder $drupalFinder
) {
$this->drupalFinder = $drupalFinder;
parent::__construct();
}

protected function configure()
{
$this->setName('debug:dotenv')
->setDescription('Debug Dotenv debug values.');
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$fs = new Filesystem();
$envFile = $this->drupalFinder->getComposerRoot() . '/.env';
if (!$fs->exists($envFile)) {
$this->getIo()->warning('File '. $envFile . ' not found.');

return 1;
}

$fileContent = file_get_contents($envFile);
$this->getIo()->writeln($fileContent);
}
}
198 changes: 198 additions & 0 deletions src/Command/Dotenv/InitCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
<?php

namespace Drupal\Console\Command\Dotenv;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Drupal\Console\Core\Command\Command;
use Symfony\Component\Filesystem\Filesystem;
use Drupal\Component\Utility\Crypt;
use Drupal\Console\Generator\DotenvInitGenerator;
use Webmozart\PathUtil\Path;
use Drupal\Console\Core\Utils\DrupalFinder;

/**
* Class InitCommand
*
* @package Drupal\Console\Command\Dotenv
*/
class InitCommand extends Command
{
/**
* @var DrupalFinder
*/
protected $drupalFinder;

/**
* InitCommand constructor.
*
* @param DrupalFinder $drupalFinder
*/

/**
* @var DotenvInitGenerator
*/
protected $generator;

private $envParameters = [
'environment' => 'local',
'database_name' => 'drupal',
'database_user' => 'drupal',
'database_password' => 'drupal',
'database_host' => '127.0.0.1',
'database_port' => '3306',
];

/**
* InitCommand constructor.
*
* @param DrupalFinder $drupalFinder
* @param DotenvInitGenerator $generator
*/
public function __construct(
DrupalFinder $drupalFinder,
DotenvInitGenerator $generator
) {
$this->drupalFinder = $drupalFinder;
$this->generator = $generator;
parent::__construct();
}

protected function configure()
{
$this->setName('dotenv:init')
->setDescription('Dotenv initializer.');
}

/**
* {@inheritdoc}
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
foreach ($this->envParameters as $key => $value) {
$this->envParameters[$key] = $this->getIo()->ask(
'Enter value for ' . strtoupper($key),
$value
);
}
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->copyFiles();

$this->generator->generate([
'io' => $this->getIo(),
'env_parameters' => $this->envParameters,
'console_root' => $this->drupalFinder->getComposerRoot(),
]);
}

private function copyFiles()
{
$fs = new Filesystem();
$defaultSettingsFile = $this->drupalFinder->getDrupalRoot() . '/sites/default/default.settings.php';
$settingsFile = $this->drupalFinder->getDrupalRoot() . '/sites/default/settings.php';

if (!$fs->exists($defaultSettingsFile)) {
$defaultSettingsFile = Path::makeRelative(
$defaultSettingsFile,
$this->drupalFinder->getComposerRoot()
);
$this->getIo()->error('File: ' . $defaultSettingsFile . 'not found.');

return 1;
}

if ($fs->exists($settingsFile)) {
$settingsFileOriginal = $settingsFile.'.original';
if (!$fs->exists($settingsFileOriginal)) {
$fs->rename(
$settingsFile,
$settingsFileOriginal,
true
);


$settingsOriginalFile = Path::makeRelative(
$settingsFile,
$this->drupalFinder->getComposerRoot()
);

$this->getIo()->success('File '.$settingsOriginalFile.'.original created.');
}
}

$fs->copy(
$defaultSettingsFile,
$settingsFile
);

include_once $this->drupalFinder->getDrupalRoot() . '/core/includes/bootstrap.inc';
include_once $this->drupalFinder->getDrupalRoot() . '/core/includes/install.inc';

$settings['config_directories'] = [
CONFIG_SYNC_DIRECTORY => (object) [
'value' => Path::makeRelative(
$this->drupalFinder->getComposerRoot() . '/config/sync',
$this->drupalFinder->getDrupalRoot()
),
'required' => true,
],
];

$settings['settings']['hash_salt'] = (object) [
'value' => Crypt::randomBytesBase64(55),
'required' => true,
];

drupal_rewrite_settings($settings, $settingsFile);

$settingsFileContent = file_get_contents($settingsFile);
file_put_contents(
$settingsFile,
$settingsFileContent .
file_get_contents(
$this->drupalFinder->getConsolePath() . 'templates/files/settings.dist'
)
);

$fs->chmod($settingsFile, 0666);

$settingsFile = Path::makeRelative(
$settingsFile,
$this->drupalFinder->getComposerRoot()
);

$this->getIo()->success('File '.$settingsFile.' created.');

$gitIgnoreFile = $this->drupalFinder->getComposerRoot() . '/.gitignore';
$gitIgnoreExampleFile = $this->drupalFinder->getComposerRoot() . '/example.gitignore';
if (!$fs->exists($gitIgnoreFile)) {
if (!$fs->exists($gitIgnoreExampleFile)) {
$fs->copy(
$gitIgnoreExampleFile,
$gitIgnoreFile
);
}
}

if ($fs->exists($gitIgnoreFile)) {
$gitIgnoreContent = file_get_contents($gitIgnoreFile);
if (strpos($gitIgnoreContent, '.env') === false) {
file_put_contents(
$gitIgnoreFile,
$gitIgnoreContent .
file_get_contents(
$this->drupalFinder->getConsolePath() . 'templates/files/.gitignore.dist'
)
);

$this->getIo()->success("File .gitignore updated.");
}
}
}
}
48 changes: 48 additions & 0 deletions src/Generator/DotenvInitGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/**
* @file
* Contains Drupal\Console\Generator\DotenvInitGenerator.
*/
namespace Drupal\Console\Generator;

use Symfony\Component\Filesystem\Filesystem;
use Drupal\Console\Core\Generator\Generator;

/**
* Class InitGenerator
*
* @package Drupal\Console\Generator
*/
class DotenvInitGenerator extends Generator
{
/**
* {@inheritdoc}
*/
public function generate(array $parameters)
{
$io = $parameters['io'];
$envParameters = $parameters['env_parameters'];
$consoleRoot = $parameters['console_root '];
$fs = new Filesystem();
$envFile = $consoleRoot . '/.env';

if ($fs->exists($envFile)) {
$fs->rename(
$envFile,
$envFile.'.original',
true
);

$io->success('File .env.original created.');
}

$this->renderFile(
'.env.dist.twig',
$consoleRoot . '/.env',
$envParameters
);

$io->success("File .env created.");
}
}
13 changes: 13 additions & 0 deletions templates/.env.dist.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# ENV
ENVIRONMENT={{ environment }}

# Database
DATABASE_NAME={{ database_name }}
DATABASE_USER={{ database_user }}
DATABASE_PASSWORD={{ database_password }}
DATABASE_HOST={{ database_host }}
DATABASE_PORT={{ database_port }}

# SETTINGS
#SETTINGS_BAR=baz
#SETTINGS_LOREM=ipsum
3 changes: 3 additions & 0 deletions templates/files/.gitignore.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

# dotenv file
.env
55 changes: 55 additions & 0 deletions templates/files/settings.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@

// Load .env file if exists
if (file_exists(dirname(DRUPAL_ROOT) . '/.env')) {
// Load environment
// make sure you add the vlucas/phpdotenv dependency using:
// composer require vlucas/phpdotenv
$dotenv = new \Dotenv\Dotenv(dirname(DRUPAL_ROOT));
$dotenv->load();
}

# Load environment
$env = getenv('ENVIRONMENT');

# Load key/value settings
$settings_drupal = array_filter(
$_SERVER,
function($key) {
return strpos($key, 'SETTINGS_') === 0;
},
ARRAY_FILTER_USE_KEY
);

# Set key/value settings
foreach ($settings_drupal as $name => $value) {
if (substr($name, 0, 9) === 'SETTINGS_') {
$key = strtolower(substr($name, 9));
$settings[$key] = $value;
}
}

$base_path = $app_root . '/' . $site_path;
$servicesFile = $base_path . '/services.'.$env.'.yml';
$settingsFile = $base_path . '/settings.'.$env.'.php';

// Load services definition file.
if (file_exists($servicesFile)) {
$settings['container_yamls'][] = $servicesFile;
}

// Load settings file.
if (file_exists($settingsFile)) {
include $settingsFile;
}

$databases['default']['default'] = array (
'database' => getenv('DATABASE_NAME'),
'username' => getenv('DATABASE_USER'),
'password' => getenv('DATABASE_PASSWORD'),
'prefix' => '',
'host' => getenv('DATABASE_HOST'),
'port' => getenv('DATABASE_PORT'),
'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
'driver' => 'mysql',
);

Loading

0 comments on commit c7e848f

Please sign in to comment.