Version 1
[yaffs-website] / vendor / drupal / console-core / src / Command / Settings / SetCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Core\Command\Settings\SetCommand.
6  */
7
8 namespace Drupal\Console\Core\Command\Settings;
9
10 use Symfony\Component\Console\Input\InputInterface;
11 use Symfony\Component\Console\Input\InputArgument;
12 use Symfony\Component\Console\Output\OutputInterface;
13 use Symfony\Component\Yaml\Dumper;
14 use Symfony\Component\Yaml\Parser;
15 use Symfony\Component\Console\Command\Command;
16 use Drupal\Console\Core\Command\Shared\CommandTrait;
17 use Drupal\Console\Core\Utils\ConfigurationManager;
18 use Drupal\Console\Core\Utils\NestedArray;
19 use Drupal\Console\Core\Style\DrupalStyle;
20
21 /**
22  * Class SetCommand
23  * @package Drupal\Console\Core\Command\Settings
24  */
25 class SetCommand extends Command
26 {
27     use CommandTrait;
28
29     /**
30      * @var ConfigurationManager
31      */
32     protected $configurationManager;
33
34     /**
35      * @var NestedArray
36      */
37     protected $nestedArray;
38
39     /**
40      * CheckCommand constructor.
41      * @param ConfigurationManager $configurationManager
42      * @param NestedArray          $nestedArray
43      */
44     public function __construct(
45         ConfigurationManager $configurationManager,
46         NestedArray $nestedArray
47     ) {
48         $this->configurationManager = $configurationManager;
49         $this->nestedArray = $nestedArray;
50
51         parent::__construct();
52     }
53
54     /**
55      * {@inheritdoc}
56      */
57     protected function configure()
58     {
59         $this
60             ->setName('settings:set')
61             ->addArgument(
62                 'name',
63                 InputArgument::REQUIRED,
64                 $this->trans('commands.settings.set.arguments.name'),
65                 null
66             )
67             ->addArgument(
68                 'value',
69                 InputArgument::REQUIRED,
70                 $this->trans('commands.settings.set.arguments.value'),
71                 null
72             )
73             ->setDescription($this->trans('commands.settings.set.description'));
74     }
75
76     /**
77      * {@inheritdoc}
78      */
79     protected function execute(InputInterface $input, OutputInterface $output)
80     {
81         $io = new DrupalStyle($input, $output);
82         $parser = new Parser();
83         $dumper = new Dumper();
84
85         $settingName = $input->getArgument('name');
86         $settingValue = $input->getArgument('value');
87
88         $userConfigFile = sprintf(
89             '%s/.console/config.yml',
90             $this->configurationManager->getHomeDirectory()
91         );
92
93         if (!file_exists($userConfigFile)) {
94             $io->error(
95                 sprintf(
96                     $this->trans('commands.settings.set.messages.missing-file'),
97                     $userConfigFile
98                 )
99             );
100             return 1;
101         }
102
103         try {
104             $userConfigFileParsed = $parser->parse(
105                 file_get_contents($userConfigFile)
106             );
107         } catch (\Exception $e) {
108             $io->error(
109                 $this->trans(
110                     'commands.settings.set.messages.error-parsing'
111                 ) . ': ' . $e->getMessage()
112             );
113             return 1;
114         }
115
116         $parents = array_merge(['application'], explode(".", $settingName));
117
118         $this->nestedArray->setValue(
119             $userConfigFileParsed,
120             $parents,
121             $settingValue,
122             true
123         );
124
125         try {
126             $userConfigFileDump = $dumper->dump($userConfigFileParsed, 10);
127         } catch (\Exception $e) {
128             $io->error(
129                 [
130                     $this->trans('commands.settings.set.messages.error-generating'),
131                     $e->getMessage()
132                 ]
133             );
134
135             return 1;
136         }
137
138         if ($settingName == 'language') {
139             $this->getApplication()
140                 ->getTranslator()
141                 ->changeCoreLanguage($settingValue);
142
143             $translatorLanguage = $this->getApplication()->getTranslator()->getLanguage();
144             if ($translatorLanguage != $settingValue) {
145                 $io->error(
146                     sprintf(
147                         $this->trans('commands.settings.set.messages.missing-language'),
148                         $settingValue
149                     )
150                 );
151
152                 return 1;
153             }
154         }
155
156         try {
157             file_put_contents($userConfigFile, $userConfigFileDump);
158         } catch (\Exception $e) {
159             $io->error(
160                 [
161                     $this->trans('commands.settings.set.messages.error-writing'),
162                     $e->getMessage()
163                 ]
164             );
165
166             return 1;
167         }
168
169         $io->success(
170             sprintf(
171                 $this->trans('commands.settings.set.messages.success'),
172                 $settingName,
173                 $settingValue
174             )
175         );
176
177         return 0;
178     }
179 }