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