5 * Contains \Drupal\Console\Command\Site\ModeCommand.
7 namespace Drupal\Console\Command\Site;
9 use Symfony\Component\Console\Input\InputArgument;
10 use Symfony\Component\Console\Input\InputOption;
11 use Symfony\Component\Console\Input\InputInterface;
12 use Symfony\Component\Console\Output\OutputInterface;
13 use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
14 use Symfony\Component\Yaml\Yaml;
15 use Symfony\Component\Console\Command\Command;
16 use Symfony\Component\Filesystem\Filesystem;
17 use Novia713\Maginot\Maginot;
18 use Drupal\Console\Core\Command\Shared\ContainerAwareCommandTrait;
19 use Drupal\Console\Core\Style\DrupalStyle;
20 use Drupal\Console\Core\Utils\ConfigurationManager;
21 use Drupal\Core\Config\ConfigFactory;
22 use Drupal\Console\Core\Utils\ChainQueue;
24 class ModeCommand extends Command
26 use ContainerAwareCommandTrait;
31 protected $configFactory;
34 * @var ConfigurationManager
36 protected $configurationManager;
46 protected $chainQueue;
49 * DebugCommand constructor.
51 * @param ConfigFactory $configFactory
52 * @param ConfigurationManager $configurationManager
54 * @param ChainQueue $chainQueue,
56 public function __construct(
57 ConfigFactory $configFactory,
58 ConfigurationManager $configurationManager,
60 ChainQueue $chainQueue
62 $this->configFactory = $configFactory;
63 $this->configurationManager = $configurationManager;
64 $this->appRoot = $appRoot;
65 $this->chainQueue = $chainQueue;
69 $this->services_file =
70 $this->appRoot.'/sites/default/services.yml';
72 $this->local_services_file =
73 $this->appRoot.'/sites/development.services.yml';
75 $this->settings_file =
76 $this->appRoot.'/sites/default/settings.php';
78 $this->local_settings_file =
79 $this->appRoot.'/sites/default/settings.local.php';
81 $this->local_settings_file_original =
82 $this->appRoot.'/sites/example.settings.local.php';
84 $this->fs = new Filesystem();
85 $this->maginot = new Maginot();
86 $this->yaml = new Yaml();
88 $this->environment = null;
90 parent::__construct();
93 protected function configure()
96 ->setName('site:mode')
97 ->setDescription($this->trans('commands.site.mode.description'))
100 InputArgument::REQUIRED,
101 $this->trans('commands.site.mode.arguments.environment')
106 InputOption::VALUE_NONE,
107 $this->trans('commands.site.mode.options.local')
111 protected function execute(InputInterface $input, OutputInterface $output)
113 $io = new DrupalStyle($input, $output);
115 $this->environment = $input->getArgument('environment');
116 $this->local = $input->getOption('local');
118 $loadedConfigurations = [];
119 if (in_array($this->environment, ['dev', 'prod'])) {
120 $loadedConfigurations = $this->loadConfigurations($this->environment);
122 $io->error($this->trans('commands.site.mode.messages.invalid-env'));
125 $configurationOverrideResult = $this->overrideConfigurations(
126 $loadedConfigurations['configurations'],
130 foreach ($configurationOverrideResult as $configName => $result) {
132 $this->trans('commands.site.mode.messages.configuration').':',
135 $io->comment($configName);
138 $this->trans('commands.site.mode.messages.configuration-key'),
139 $this->trans('commands.site.mode.messages.original'),
140 $this->trans('commands.site.mode.messages.updated'),
143 $io->table($tableHeader, $result);
146 $servicesOverrideResult = $this->overrideServices(
147 $loadedConfigurations['services'],
151 if (!empty($servicesOverrideResult)) {
153 $this->trans('commands.site.mode.messages.new-services-settings')
157 $this->trans('commands.site.mode.messages.service'),
158 $this->trans('commands.site.mode.messages.service-parameter'),
159 $this->trans('commands.site.mode.messages.service-value'),
162 $io->table($tableHeaders, $servicesOverrideResult);
165 $this->chainQueue->addCommand('cache:rebuild', ['cache' => 'all']);
168 protected function overrideConfigurations($configurations, $io)
171 foreach ($configurations as $configName => $options) {
172 $config = $this->configFactory->getEditable($configName);
173 foreach ($options as $key => $value) {
174 $original = $config->get($key);
175 if (is_bool($original)) {
176 $original = $original ? 'true' : 'false';
179 if (is_bool($updated)) {
180 $updated = $updated ? 'true' : 'false';
183 $result[$configName][] = [
184 'configuration' => $key,
185 'original' => $original,
186 'updated' => $updated,
188 $config->set($key, $value);
193 $line_include_settings =
194 '<?php include __DIR__ . "/settings.local.php"; ?>';
196 if ($this->environment == 'dev') {
198 // copy sites/example.settings.local.php sites/default/settings.local.php
199 $this->fs->copy($this->local_settings_file_original, $this->local_settings_file, true);
201 // uncomment cache bins in settings.local.php
202 $this->maginot->unCommentLine(
203 '# $settings[\'cache\'][\'bins\'][\'render\'] = \'cache.backend.null\';',
204 $this->local_settings_file
207 $this->maginot->unCommentLine(
208 '// $settings[\'cache\'][\'bins\'][\'render\'] = \'cache.backend.null\';',
209 $this->local_settings_file
212 $this->maginot->unCommentLine(
213 '# $settings[\'cache\'][\'bins\'][\'dynamic_page_cache\'] = \'cache.backend.null\';',
214 $this->local_settings_file
217 $this->maginot->unCommentLine(
218 '// $settings[\'cache\'][\'bins\'][\'dynamic_page_cache\'] = \'cache.backend.null\';',
219 $this->local_settings_file
222 // include settings.local.php in settings.php
223 // -- check first line if it is already this
224 if ($this->maginot->getFirstLine($this->settings_file)!= $line_include_settings
226 chmod($this->settings_file, (int)0775);
227 $this->maginot->setFirstLine(
228 $line_include_settings,
236 $this->trans('commands.site.mode.messages.cachebins')
240 if ($this->environment == 'prod') {
243 // comment local.settings.php in settings.php
244 if ($this->maginot->getFirstLine($this->settings_file)==$line_include_settings
246 $this->maginot->deleteFirstLine(
254 $this->local_settings_file
256 //@TODO: msg user "local.settings.php deleted"
257 } catch (IOExceptionInterface $e) {
258 echo $e->getMessage();
262 // comment cache bins in local.settings.php,
263 // we still use local.settings.php for testing PROD
266 $this->maginot->CommentLine(
267 ' $settings[\'cache\'][\'bins\'][\'render\'] = \'cache.backend.null\';',
268 $this->local_settings_file
271 $this->maginot->CommentLine(
272 ' $settings[\'cache\'][\'bins\'][\'dynamic_page_cache\'] = \'cache.backend.null\';',
273 $this->local_settings_file
279 * would be better if this were replaced by $config->save?
281 //@TODO: 0444 should be a better permission for settings.php
282 chmod($this->settings_file, (int)0644);
283 //@TODO: 0555 should be a better permission for sites/default
284 chmod($this->appRoot.'/sites/default/', (int)0755);
289 protected function overrideServices($servicesSettings, DrupalStyle $io)
291 $directory = sprintf(
294 \Drupal::service('site.path')
297 $settingsServicesFile = $directory.'/services.yml';
298 if (!file_exists($settingsServicesFile)) {
299 // Copying default services
300 $defaultServicesFile = $this->appRoot.'/sites/default/default.services.yml';
301 if (!copy($defaultServicesFile, $settingsServicesFile)) {
304 '%s: %s/services.yml',
305 $this->trans('commands.site.mode.messages.error-copying-file'),
314 $services = $this->yaml->parse(file_get_contents($settingsServicesFile));
317 foreach ($servicesSettings as $service => $parameters) {
318 if (is_array($parameters)) {
319 foreach ($parameters as $parameter => $value) {
320 $services['parameters'][$service][$parameter] = $value;
321 // Set values for output
322 $result[$parameter]['service'] = $service;
323 $result[$parameter]['parameter'] = $parameter;
324 if (is_bool($value)) {
325 $value = $value ? 'true' : 'false';
327 $result[$parameter]['value'] = $value;
330 $services['parameters'][$service] = $parameters;
331 // Set values for output
332 $result[$service]['service'] = $service;
333 $result[$service]['parameter'] = '';
334 if (is_bool($parameters)) {
335 $value = $parameters ? 'true' : 'false';
337 $result[$service]['value'] = $value;
341 if (file_put_contents($settingsServicesFile, $this->yaml->dump($services))) {
344 $this->trans('commands.site.mode.messages.services-file-overwritten'),
345 $settingsServicesFile
351 '%s : %s/services.yml',
352 $this->trans('commands.site.mode.messages.error-writing-file'),
365 protected function loadConfigurations($env)
367 $configFile = sprintf(
368 '%s/.console/site.mode.yml',
369 $this->configurationManager->getHomeDirectory()
372 if (!file_exists($configFile)) {
373 $configFile = sprintf(
374 '%s/config/dist/site.mode.yml',
375 $this->configurationManager->getApplicationDirectory().DRUPAL_CONSOLE_CORE
379 $siteModeConfiguration = $this->yaml->parse(file_get_contents($configFile));
380 $configKeys = array_keys($siteModeConfiguration);
382 $configurationSettings = [];
383 foreach ($configKeys as $configKey) {
384 $siteModeConfigurationItem = $siteModeConfiguration[$configKey];
385 foreach ($siteModeConfigurationItem as $setting => $parameters) {
386 if (array_key_exists($env, $parameters)) {
387 $configurationSettings[$configKey][$setting] = $parameters[$env];
389 foreach ($parameters as $parameter => $value) {
390 $configurationSettings[$configKey][$setting][$parameter] = $value[$env];
396 return $configurationSettings;