Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 113 additions & 39 deletions src/Command/Site/ModeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@
* @file
* Contains \Drupal\Console\Command\Site\ModeCommand.
*/

namespace Drupal\Console\Command\Site;

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Filesystem\Filesystem;
use Novia713\Maginot\Maginot;
use Drupal\Console\Command\Shared\ContainerAwareCommandTrait;
use Drupal\Console\Style\DrupalStyle;
use Drupal\Console\Utils\ConfigurationManager;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Console\Utils\ChainQueue;


class ModeCommand extends Command
{
use ContainerAwareCommandTrait;
Expand All @@ -44,9 +46,11 @@ class ModeCommand extends Command

/**
* DebugCommand constructor.
* @param ConfigFactory $configFactory
*
* @param ConfigFactory $configFactory
* @param ConfigurationManager $configurationManager
* @param ChainQueue $chainQueue,
* @param $appRoot
* @param ChainQueue $chainQueue,
*/
public function __construct(
ConfigFactory $configFactory,
Expand All @@ -58,6 +62,29 @@ public function __construct(
$this->configurationManager = $configurationManager;
$this->appRoot = $appRoot;
$this->chainQueue = $chainQueue;


$this->services_file =
$this->appRoot.'/sites/default/services.yml';

$this->local_services_file =
$this->appRoot.'/sites/development.services.yml';

$this->settings_file =
$this->appRoot.'/sites/default/settings.php';

$this->local_settings_file =
$this->appRoot.'/sites/default/settings.local.php';

$this->local_settings_file_original =
$this->appRoot.'/sites/example.settings.local.php';

$this->fs = new Filesystem();
$this->maginot = new Maginot();
$this->yaml = new Yaml();

$this->environment = null;

parent::__construct();
}

Expand All @@ -77,22 +104,23 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new DrupalStyle($input, $output);

$environment = $input->getArgument('environment');
$this->environment = $input->getArgument('environment');

$loadedConfigurations = [];
if (in_array($environment, array('dev', 'prod'))) {
$loadedConfigurations = $this->loadConfigurations($environment);
if (in_array($this->environment, array('dev', 'prod'))) {
$loadedConfigurations = $this->loadConfigurations($this->environment);
} else {
$io->error($this->trans('commands.site.mode.messages.invalid-env'));
}

$configurationOverrideResult = $this->overrideConfigurations(
$loadedConfigurations['configurations']
$loadedConfigurations['configurations'],
$io
);

foreach ($configurationOverrideResult as $configName => $result) {
$io->info(
$this->trans('commands.site.mode.messages.configuration') . ':',
$this->trans('commands.site.mode.messages.configuration').':',
false
);
$io->comment($configName);
Expand All @@ -107,7 +135,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

$servicesOverrideResult = $this->overrideServices(
$environment,
$loadedConfigurations['services'],
$io
);
Expand All @@ -129,19 +156,19 @@ protected function execute(InputInterface $input, OutputInterface $output)
$this->chainQueue->addCommand('cache:rebuild', ['cache' => 'all']);
}

protected function overrideConfigurations($configurations)
protected function overrideConfigurations($configurations, $io)
{
$result = [];
foreach ($configurations as $configName => $options) {
$config = $this->configFactory->getEditable($configName);
foreach ($options as $key => $value) {
$original = $config->get($key);
if (is_bool($original)) {
$original = $original? 'true' : 'false';
$original = $original ? 'true' : 'false';
}
$updated = $value;
if (is_bool($updated)) {
$updated = $updated? 'true' : 'false';
$updated = $updated ? 'true' : 'false';
}

$result[$configName][] = [
Expand All @@ -154,37 +181,86 @@ protected function overrideConfigurations($configurations)
$config->save();
}

// $this->getDrupalService('settings');die();
//
// $drupal = $this->getDrupalHelper();
// $fs = $this->getApplication()->getContainerHelper()->get('filesystem');
//
// $cache_render = '$settings = ["cache"]["bins"]["render"] = "cache.backend.null";';
// $cache_dynamic = '$settings =["cache"]["bins"]["dynamic_page_cache"] = "cache.backend.null";';
//
// $settings_file = $fs->exists($drupal->getRoot() . '/sites/default/local.settings.php')?:$drupal->getRoot() . '/sites/default/settings.php';
// chmod($drupal->getRoot() . '/sites/default/', 0775);
// chmod($settings_file, 0775);
// $settings_file = $fs->dumpFile($settings_file, file_get_contents($settings_file) . $cache_render . $cache_dynamic);
// chmod($drupal->getRoot() . '/sites/default/', 0644);
// chmod($settings_file, 0644);
// @TODO: $io->commentBlock()
$line_include_settings =
'<?php include __DIR__ . "/settings.local.php"; ?>';

if ($this->environment == 'dev') {

// copy sites/example.settings.local.php sites/default/settings.local.php
$this->fs->copy($this->local_settings_file_original, $this->local_settings_file, true);

// uncomment cache bins in local.settings
$this->maginot->unCommentLine(
'# $settings[\'cache\'][\'bins\'][\'render\'] = \'cache.backend.null\';',
$this->local_settings_file
);

$this->maginot->unCommentLine(
'# $settings[\'cache\'][\'bins\'][\'dynamic_page_cache\'] = \'cache.backend.null\';',
$this->local_settings_file
);

// include settings.local.php in settings.php
// -- check first line if it is already this
if (
$this->maginot->getFirstLine($this->settings_file)
!= $line_include_settings
) {
chmod($this->settings_file, (int)0775);
$this->maginot->setFirstLine(
$line_include_settings,
$this->settings_file
);
}

$io->commentBlock(
sprintf(
'%s',
$this->trans('commands.site.mode.messages.cachebins')
)
);
}
if ($this->environment == 'prod') {
// comment local.settings.php in settings.php
if (
$this->maginot->getFirstLine($this->settings_file)
==
$line_include_settings
) {
$this->maginot->deleteFirstLine(
$this->settings_file
);
}

//@TODO: comment cache bins lines in settings.local.php ??
}

/**
*
* would be better if this were replaced by $config->save?
*
*
*/
//@TODO: 0444 should be a better permission for settings.php
chmod($this->settings_file, (int)0644);
//@TODO: 0555 should be a better permission for sites/default
chmod($this->appRoot.'/sites/default/', (int)0755);

return $result;
}

protected function overrideServices($environment, $servicesSettings, DrupalStyle $io)
protected function overrideServices($servicesSettings, DrupalStyle $io)
{
$directory = sprintf(
'%s/%s',
$this->appRoot,
\Drupal::service('site.path')
);

$settingsServicesFile = $directory . '/services.yml';
$settingsServicesFile = $directory.'/services.yml';
if (!file_exists($settingsServicesFile)) {
// Copying default services
$defaultServicesFile = $this->appRoot . '/sites/default/default.services.yml';
$defaultServicesFile = $this->appRoot.'/sites/default/default.services.yml';
if (!copy($defaultServicesFile, $settingsServicesFile)) {
$io->error(
sprintf(
Expand All @@ -198,15 +274,12 @@ protected function overrideServices($environment, $servicesSettings, DrupalStyle
}
}

$yaml = new Yaml();

$services = $yaml->parse(file_get_contents($settingsServicesFile));
$services = $this->yaml->parse(file_get_contents($settingsServicesFile));

$result = [];
foreach ($servicesSettings as $service => $parameters) {
if(is_array($parameters)) {
if (is_array($parameters)) {
foreach ($parameters as $parameter => $value) {
print 'parameters: ' . $parameter . "\n";
$services['parameters'][$service][$parameter] = $value;
// Set values for output
$result[$parameter]['service'] = $service;
Expand All @@ -228,7 +301,7 @@ protected function overrideServices($environment, $servicesSettings, DrupalStyle
}
}

if (file_put_contents($settingsServicesFile, $yaml->dump($services))) {
if (file_put_contents($settingsServicesFile, $this->yaml->dump($services))) {
$io->commentBlock(
sprintf(
$this->trans('commands.site.mode.messages.services-file-overwritten'),
Expand All @@ -248,6 +321,7 @@ protected function overrideServices($environment, $servicesSettings, DrupalStyle
}

sort($result);

return $result;
}

Expand All @@ -261,18 +335,18 @@ protected function loadConfigurations($env)
if (!file_exists($configFile)) {
$configFile = sprintf(
'%s/config/dist/site.mode.yml',
$this->configurationManager->getApplicationDirectory() . DRUPAL_CONSOLE_CORE
$this->configurationManager->getApplicationDirectory().DRUPAL_CONSOLE_CORE
);
}

$siteModeConfiguration = Yaml::parse(file_get_contents($configFile));
$siteModeConfiguration = $this->yaml->parse(file_get_contents($configFile));
$configKeys = array_keys($siteModeConfiguration);

$configurationSettings = [];
foreach ($configKeys as $configKey) {
$siteModeConfigurationItem = $siteModeConfiguration[$configKey];
foreach ($siteModeConfigurationItem as $setting => $parameters) {
if(array_key_exists($env, $parameters)) {
if (array_key_exists($env, $parameters)) {
$configurationSettings[$configKey][$setting] = $parameters[$env];
} else {
foreach ($parameters as $parameter => $value) {
Expand Down