4 * Contains \Drupal\Console\Command\Config\DeleteCommand.
7 namespace Drupal\Console\Command\Config;
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;
20 class DeleteCommand extends Command
24 protected $allConfig = [];
29 protected $configFactory;
34 protected $configStorage;
37 * @var StorageInterface
39 protected $configStorageSync;
42 * DeleteCommand constructor.
44 * @param ConfigFactory $configFactory
45 * @param CachedStorage $configStorage
46 * @param StorageInterface $configStorageSync
48 public function __construct(
49 ConfigFactory $configFactory,
50 CachedStorage $configStorage,
51 StorageInterface $configStorageSync
53 $this->configFactory = $configFactory;
54 $this->configStorage = $configStorage;
55 $this->configStorageSync = $configStorageSync;
56 parent::__construct();
62 protected function configure()
65 ->setName('config:delete')
66 ->setDescription($this->trans('commands.config.delete.description'))
69 InputArgument::OPTIONAL,
70 $this->trans('commands.config.delete.arguments.type')
74 InputArgument::OPTIONAL,
75 $this->trans('commands.config.delete.arguments.name')
82 protected function interact(InputInterface $input, OutputInterface $output)
84 $io = new DrupalStyle($input, $output);
86 $type = $input->getArgument('type');
88 $type = $io->choiceNoList(
89 $this->trans('commands.config.delete.arguments.type'),
90 ['active', 'staging'],
93 $input->setArgument('type', $type);
96 $name = $input->getArgument('name');
98 $name = $io->choiceNoList(
99 $this->trans('commands.config.delete.arguments.name'),
100 $this->getAllConfigNames(),
103 $input->setArgument('name', $name);
110 protected function execute(InputInterface $input, OutputInterface $output)
112 $io = new DrupalStyle($input, $output);
114 $type = $input->getArgument('type');
116 $io->error($this->trans('commands.config.delete.errors.type'));
120 $name = $input->getArgument('name');
122 $io->error($this->trans('commands.config.delete.errors.name'));
126 $configStorage = ('active' === $type) ? $this->configStorage : $this->configStorageSync;
128 if (!$configStorage) {
129 $io->error($this->trans('commands.config.delete.errors.config-storage'));
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();
139 foreach ($this->yieldAllConfig() as $name) {
140 $this->removeConfig($name);
144 $io->success($this->trans('commands.config.delete.messages.all'));
150 if ($configStorage->exists($name)) {
151 if ($configStorage instanceof FileStorage) {
152 $configStorage->delete($name);
154 $this->removeConfig($name);
159 $this->trans('commands.config.delete.messages.deleted'),
166 $message = sprintf($this->trans('commands.config.delete.errors.not-exists'), $name);
167 $io->error($message);
173 * Retrieve configuration names form cache or service factory.
176 * All configuration names.
178 private function getAllConfigNames()
180 if ($this->allConfig) {
181 return $this->allConfig;
184 foreach ($this->configFactory->listAll() as $name) {
185 $this->allConfig[] = $name;
188 return $this->allConfig;
192 * Yield configuration names.
195 * Yield generator with config name.
197 private function yieldAllConfig()
199 $this->allConfig = $this->allConfig ?: $this->getAllConfigNames();
200 foreach ($this->allConfig as $name) {
206 * Delete given config name.
208 * @param String $name Given config name.
210 private function removeConfig($name)
213 $this->configFactory->getEditable($name)->delete();
214 } catch (\Exception $e) {
215 throw new RuntimeException($e->getMessage());