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