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