Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / src / Command / Site / ModeCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Site\ModeCommand.
6  */
7
8 namespace Drupal\Console\Command\Site;
9
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;
20
21 class ModeCommand extends Command
22 {
23     use ContainerAwareCommandTrait;
24
25     /**
26      * @var ConfigFactory
27      */
28     protected $configFactory;
29
30     /**
31      * @var ConfigurationManager
32      */
33     protected $configurationManager;
34
35     /**
36      * @var string
37      */
38     protected $appRoot;
39
40     /**
41      * @var ChainQueue
42      */
43     protected $chainQueue;
44
45     /**
46      * DebugCommand constructor.
47      *
48      * @param ConfigFactory        $configFactory
49      * @param ConfigurationManager $configurationManager
50      * @param $appRoot,
51      * @param ChainQueue           $chainQueue,
52      */
53     public function __construct(
54         ConfigFactory $configFactory,
55         ConfigurationManager $configurationManager,
56         $appRoot,
57         ChainQueue $chainQueue
58     ) {
59         $this->configFactory = $configFactory;
60         $this->configurationManager = $configurationManager;
61         $this->appRoot = $appRoot;
62         $this->chainQueue = $chainQueue;
63         parent::__construct();
64     }
65
66     protected function configure()
67     {
68         $this
69             ->setName('site:mode')
70             ->setDescription($this->trans('commands.site.mode.description'))
71             ->addArgument(
72                 'environment',
73                 InputArgument::REQUIRED,
74                 $this->trans('commands.site.mode.arguments.environment')
75             );
76     }
77
78     protected function execute(InputInterface $input, OutputInterface $output)
79     {
80         $io = new DrupalStyle($input, $output);
81
82         $environment = $input->getArgument('environment');
83
84         if (!in_array($environment, ['dev', 'prod'])) {
85             $io->error($this->trans('commands.site.mode.messages.invalid-env'));
86             return 1;
87         }
88
89         $loadedConfigurations = $this->loadConfigurations($environment);
90
91         $configurationOverrideResult = $this->overrideConfigurations(
92             $loadedConfigurations['configurations']
93         );
94
95         foreach ($configurationOverrideResult as $configName => $result) {
96             $io->info(
97                 $this->trans('commands.site.mode.messages.configuration') . ':',
98                 false
99             );
100             $io->comment($configName);
101
102             $tableHeader = [
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'),
106             ];
107
108             $io->table($tableHeader, $result);
109         }
110
111         $servicesOverrideResult = $this->processServicesFile(
112             $environment,
113             $loadedConfigurations['services'],
114             $io
115         );
116
117         if (!empty($servicesOverrideResult)) {
118             $io->info(
119                 $this->trans('commands.site.mode.messages.new-services-settings')
120             );
121
122             $tableHeaders = [
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'),
126             ];
127
128             $io->table($tableHeaders, $servicesOverrideResult);
129         }
130
131         $this->chainQueue->addCommand('cache:rebuild', ['cache' => 'all']);
132     }
133
134     protected function overrideConfigurations($configurations)
135     {
136         $result = [];
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';
143                 }
144                 $updated = $value;
145                 if (is_bool($updated)) {
146                     $updated = $updated? 'true' : 'false';
147                 }
148
149                 $result[$configName][] = [
150                     'configuration' => $key,
151                     'original' => $original,
152                     'updated' => $updated,
153                 ];
154                 $config->set($key, $value);
155             }
156             $config->save();
157         }
158
159         return $result;
160     }
161
162     protected function processServicesFile($environment, $servicesSettings, DrupalStyle $io)
163     {
164         $directory = sprintf(
165             '%s/%s',
166             $this->appRoot,
167             \Drupal::service('site.path')
168         );
169
170         $settingsServicesFile = $directory . '/services.yml';
171
172         if (!file_exists($settingsServicesFile)) {
173             // Copying default services
174             $defaultServicesFile = $this->appRoot . '/sites/default/default.services.yml';
175             if (!copy($defaultServicesFile, $settingsServicesFile)) {
176                 $io->error(
177                     sprintf(
178                         '%s: %s/services.yml',
179                         $this->trans('commands.site.mode.messages.error-copying-file'),
180                         $directory
181                     )
182                 );
183
184                 return [];
185             }
186         }
187
188         $yaml = new Yaml();
189
190         $services = $yaml->parse(file_get_contents($settingsServicesFile));
191
192         $result = [];
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';
202                     }
203                     $result[$parameter]['value'] = $value;
204                 }
205             } else {
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';
212                 }
213                 $result[$service]['value'] = $value;
214             }
215         }
216
217         if (file_put_contents($settingsServicesFile, $yaml->dump($services))) {
218             $io->commentBlock(
219                 sprintf(
220                     $this->trans('commands.site.mode.messages.services-file-overwritten'),
221                     $settingsServicesFile
222                 )
223             );
224         } else {
225             $io->error(
226                 sprintf(
227                     '%s : %s/services.yml',
228                     $this->trans('commands.site.mode.messages.error-writing-file'),
229                     $directory
230                 )
231             );
232
233             return [];
234         }
235
236         sort($result);
237         return $result;
238     }
239
240     protected function loadConfigurations($env)
241     {
242         $configFile = sprintf(
243             '%s/.console/site.mode.yml',
244             $this->configurationManager->getHomeDirectory()
245         );
246
247         if (!file_exists($configFile)) {
248             $configFile = sprintf(
249                 '%s/config/dist/site.mode.yml',
250                 $this->configurationManager->getApplicationDirectory() . DRUPAL_CONSOLE_CORE
251             );
252         }
253
254         $siteModeConfiguration = Yaml::parse(file_get_contents($configFile));
255         $configKeys = array_keys($siteModeConfiguration);
256
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];
263                 } else {
264                     foreach ($parameters as $parameter => $value) {
265                         $configurationSettings[$configKey][$setting][$parameter] = $value[$env];
266                     }
267                 }
268             }
269         }
270
271         return $configurationSettings;
272     }
273 }