Version 1
[yaffs-website] / vendor / drupal / console / src / Command / Config / DeleteCommand.php
1 <?php
2 /**
3  * @file
4  * Contains \Drupal\Console\Command\Config\DeleteCommand.
5  */
6
7 namespace Drupal\Console\Command\Config;
8
9 use Symfony\Component\Console\Input\InputArgument;
10 use Symfony\Component\Console\Input\InputInterface;
11 use Symfony\Component\Console\Output\OutputInterface;
12 use Symfony\Component\Yaml\Exception\RuntimeException;
13 use Symfony\Component\Console\Command\Command;
14 use Drupal\Core\Config\StorageInterface;
15 use Drupal\Core\Config\CachedStorage;
16 use Drupal\Core\Config\ConfigFactory;
17 use Drupal\Console\Core\Command\Shared\CommandTrait;
18 use Drupal\Console\Core\Style\DrupalStyle;
19
20 class DeleteCommand extends Command
21 {
22     use CommandTrait;
23
24     protected $allConfig = [];
25
26     /**
27      * @var ConfigFactory
28      */
29     protected $configFactory;
30
31     /**
32      * @var CachedStorage
33      */
34     protected $configStorage;
35
36     /**
37      * @var StorageInterface
38      */
39     protected $configStorageSync;
40
41     /**
42      * DeleteCommand constructor.
43      *
44      * @param ConfigFactory    $configFactory
45      * @param CachedStorage    $configStorage
46      * @param StorageInterface $configStorageSync
47      */
48     public function __construct(
49         ConfigFactory $configFactory,
50         CachedStorage $configStorage,
51         StorageInterface $configStorageSync
52     ) {
53         $this->configFactory = $configFactory;
54         $this->configStorage = $configStorage;
55         $this->configStorageSync = $configStorageSync;
56         parent::__construct();
57     }
58
59     /**
60      * {@inheritdoc}
61      */
62     protected function configure()
63     {
64         $this
65             ->setName('config:delete')
66             ->setDescription($this->trans('commands.config.delete.description'))
67             ->addArgument(
68                 'type',
69                 InputArgument::OPTIONAL,
70                 $this->trans('commands.config.delete.arguments.type')
71             )
72             ->addArgument(
73                 'name',
74                 InputArgument::OPTIONAL,
75                 $this->trans('commands.config.delete.arguments.name')
76             );
77     }
78
79     /**
80      * {@inheritdoc}
81      */
82     protected function interact(InputInterface $input, OutputInterface $output)
83     {
84         $io = new DrupalStyle($input, $output);
85
86         $type = $input->getArgument('type');
87         if (!$type) {
88             $type = $io->choiceNoList(
89                 $this->trans('commands.config.delete.arguments.type'),
90                 ['active', 'staging'],
91                 'active'
92             );
93             $input->setArgument('type', $type);
94         }
95
96         $name = $input->getArgument('name');
97         if (!$name) {
98             $name = $io->choiceNoList(
99                 $this->trans('commands.config.delete.arguments.name'),
100                 $this->getAllConfigNames(),
101                 'all'
102             );
103             $input->setArgument('name', $name);
104         }
105     }
106
107     /**
108      * {@inheritdoc}
109      */
110     protected function execute(InputInterface $input, OutputInterface $output)
111     {
112         $io = new DrupalStyle($input, $output);
113
114         $type = $input->getArgument('type');
115         if (!$type) {
116             $io->error($this->trans('commands.config.delete.errors.type'));
117             return 1;
118         }
119
120         $name = $input->getArgument('name');
121         if (!$name) {
122             $io->error($this->trans('commands.config.delete.errors.name'));
123             return 1;
124         }
125
126         $configStorage = ('active' === $type) ? $this->configStorage : $this->configStorageSync;
127
128         if (!$configStorage) {
129             $io->error($this->trans('commands.config.delete.errors.config-storage'));
130             return 1;
131         }
132
133         if ('all' === $name) {
134             $io->commentBlock($this->trans('commands.config.delete.warnings.undo'));
135             if ($io->confirm($this->trans('commands.config.delete.questions.sure'))) {
136                 if ($configStorage instanceof FileStorage) {
137                     $configStorage->deleteAll();
138                 } else {
139                     foreach ($this->yieldAllConfig() as $name) {
140                         $this->removeConfig($name);
141                     }
142                 }
143
144                 $io->success($this->trans('commands.config.delete.messages.all'));
145
146                 return 0;
147             }
148         }
149
150         if ($configStorage->exists($name)) {
151             if ($configStorage instanceof FileStorage) {
152                 $configStorage->delete($name);
153             } else {
154                 $this->removeConfig($name);
155             }
156
157             $io->success(
158                 sprintf(
159                     $this->trans('commands.config.delete.messages.deleted'),
160                     $name
161                 )
162             );
163             return 0;
164         }
165
166         $message = sprintf($this->trans('commands.config.delete.errors.not-exists'), $name);
167         $io->error($message);
168
169         return 1;
170     }
171
172     /**
173      * Retrieve configuration names form cache or service factory.
174      *
175      * @return array
176      *   All configuration names.
177      */
178     private function getAllConfigNames()
179     {
180         if ($this->allConfig) {
181             return $this->allConfig;
182         }
183
184         foreach ($this->configFactory->listAll() as $name) {
185             $this->allConfig[] = $name;
186         }
187
188         return $this->allConfig;
189     }
190
191     /**
192      * Yield configuration names.
193      *
194      * @return \Generator
195      *   Yield generator with config name.
196      */
197     private function yieldAllConfig()
198     {
199         $this->allConfig = $this->allConfig ?: $this->getAllConfigNames();
200         foreach ($this->allConfig as $name) {
201             yield $name;
202         }
203     }
204
205     /**
206      * Delete given config name.
207      *
208      * @param String $name Given config name.
209      */
210     private function removeConfig($name)
211     {
212         try {
213             $this->configFactory->getEditable($name)->delete();
214         } catch (\Exception $e) {
215             throw new RuntimeException($e->getMessage());
216         }
217     }
218 }