5 * Contains \Drupal\Console\Command\Site\ModeCommand.
8 namespace Drupal\Console\Command\Site;
10 use Symfony\Component\Console\Input\InputArgument;
11 use Symfony\Component\Console\Input\InputInterface;
12 use Symfony\Component\Console\Output\OutputInterface;
13 use Symfony\Component\Yaml\Yaml;
14 use Symfony\Component\Console\Command\Command;
15 use Drupal\Console\Core\Command\Shared\ContainerAwareCommandTrait;
16 use Drupal\Console\Core\Style\DrupalStyle;
17 use Drupal\Console\Core\Utils\ConfigurationManager;
18 use Drupal\Core\Config\ConfigFactory;
19 use Drupal\Console\Core\Utils\ChainQueue;
21 class ModeCommand extends Command
23 use ContainerAwareCommandTrait;
28 protected $configFactory;
31 * @var ConfigurationManager
33 protected $configurationManager;
43 protected $chainQueue;
46 * DebugCommand constructor.
48 * @param ConfigFactory $configFactory
49 * @param ConfigurationManager $configurationManager
51 * @param ChainQueue $chainQueue,
53 public function __construct(
54 ConfigFactory $configFactory,
55 ConfigurationManager $configurationManager,
57 ChainQueue $chainQueue
59 $this->configFactory = $configFactory;
60 $this->configurationManager = $configurationManager;
61 $this->appRoot = $appRoot;
62 $this->chainQueue = $chainQueue;
63 parent::__construct();
66 protected function configure()
69 ->setName('site:mode')
70 ->setDescription($this->trans('commands.site.mode.description'))
73 InputArgument::REQUIRED,
74 $this->trans('commands.site.mode.arguments.environment')
78 protected function execute(InputInterface $input, OutputInterface $output)
80 $io = new DrupalStyle($input, $output);
82 $environment = $input->getArgument('environment');
84 if (!in_array($environment, ['dev', 'prod'])) {
85 $io->error($this->trans('commands.site.mode.messages.invalid-env'));
89 $loadedConfigurations = $this->loadConfigurations($environment);
91 $configurationOverrideResult = $this->overrideConfigurations(
92 $loadedConfigurations['configurations']
95 foreach ($configurationOverrideResult as $configName => $result) {
97 $this->trans('commands.site.mode.messages.configuration') . ':',
100 $io->comment($configName);
103 $this->trans('commands.site.mode.messages.configuration-key'),
104 $this->trans('commands.site.mode.messages.original'),
105 $this->trans('commands.site.mode.messages.updated'),
108 $io->table($tableHeader, $result);
111 $servicesOverrideResult = $this->processServicesFile(
113 $loadedConfigurations['services'],
117 if (!empty($servicesOverrideResult)) {
119 $this->trans('commands.site.mode.messages.new-services-settings')
123 $this->trans('commands.site.mode.messages.service'),
124 $this->trans('commands.site.mode.messages.service-parameter'),
125 $this->trans('commands.site.mode.messages.service-value'),
128 $io->table($tableHeaders, $servicesOverrideResult);
131 $this->chainQueue->addCommand('cache:rebuild', ['cache' => 'all']);
134 protected function overrideConfigurations($configurations)
137 foreach ($configurations as $configName => $options) {
138 $config = $this->configFactory->getEditable($configName);
139 foreach ($options as $key => $value) {
140 $original = $config->get($key);
141 if (is_bool($original)) {
142 $original = $original? 'true' : 'false';
145 if (is_bool($updated)) {
146 $updated = $updated? 'true' : 'false';
149 $result[$configName][] = [
150 'configuration' => $key,
151 'original' => $original,
152 'updated' => $updated,
154 $config->set($key, $value);
162 protected function processServicesFile($environment, $servicesSettings, DrupalStyle $io)
164 $directory = sprintf(
167 \Drupal::service('site.path')
170 $settingsServicesFile = $directory . '/services.yml';
172 if (!file_exists($settingsServicesFile)) {
173 // Copying default services
174 $defaultServicesFile = $this->appRoot . '/sites/default/default.services.yml';
175 if (!copy($defaultServicesFile, $settingsServicesFile)) {
178 '%s: %s/services.yml',
179 $this->trans('commands.site.mode.messages.error-copying-file'),
190 $services = $yaml->parse(file_get_contents($settingsServicesFile));
193 foreach ($servicesSettings as $service => $parameters) {
194 if (is_array($parameters)) {
195 foreach ($parameters as $parameter => $value) {
196 $services['parameters'][$service][$parameter] = $value;
197 // Set values for output
198 $result[$parameter]['service'] = $service;
199 $result[$parameter]['parameter'] = $parameter;
200 if (is_bool($value)) {
201 $value = $value ? 'true' : 'false';
203 $result[$parameter]['value'] = $value;
206 $services['parameters'][$service] = $parameters;
207 // Set values for output
208 $result[$service]['service'] = $service;
209 $result[$service]['parameter'] = '';
210 if (is_bool($parameters)) {
211 $value = $parameters ? 'true' : 'false';
213 $result[$service]['value'] = $value;
217 if (file_put_contents($settingsServicesFile, $yaml->dump($services))) {
220 $this->trans('commands.site.mode.messages.services-file-overwritten'),
221 $settingsServicesFile
227 '%s : %s/services.yml',
228 $this->trans('commands.site.mode.messages.error-writing-file'),
240 protected function loadConfigurations($env)
242 $configFile = sprintf(
243 '%s/.console/site.mode.yml',
244 $this->configurationManager->getHomeDirectory()
247 if (!file_exists($configFile)) {
248 $configFile = sprintf(
249 '%s/config/dist/site.mode.yml',
250 $this->configurationManager->getApplicationDirectory() . DRUPAL_CONSOLE_CORE
254 $siteModeConfiguration = Yaml::parse(file_get_contents($configFile));
255 $configKeys = array_keys($siteModeConfiguration);
257 $configurationSettings = [];
258 foreach ($configKeys as $configKey) {
259 $siteModeConfigurationItem = $siteModeConfiguration[$configKey];
260 foreach ($siteModeConfigurationItem as $setting => $parameters) {
261 if (array_key_exists($env, $parameters)) {
262 $configurationSettings[$configKey][$setting] = $parameters[$env];
264 foreach ($parameters as $parameter => $value) {
265 $configurationSettings[$configKey][$setting][$parameter] = $value[$env];
271 return $configurationSettings;