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 Drupal\Console\Core\Command\Command;
14 use Drupal\Core\Config\StorageInterface;
15 use Drupal\Core\Config\CachedStorage;
16 use Drupal\Core\Config\ConfigFactory;
18 class DeleteCommand extends Command
20 protected $allConfig = [];
25 protected $configFactory;
30 protected $configStorage;
33 * @var StorageInterface
35 protected $configStorageSync;
38 * DeleteCommand constructor.
40 * @param ConfigFactory $configFactory
41 * @param CachedStorage $configStorage
42 * @param StorageInterface $configStorageSync
44 public function __construct(
45 ConfigFactory $configFactory,
46 CachedStorage $configStorage,
47 StorageInterface $configStorageSync
49 $this->configFactory = $configFactory;
50 $this->configStorage = $configStorage;
51 $this->configStorageSync = $configStorageSync;
52 parent::__construct();
58 protected function configure()
61 ->setName('config:delete')
62 ->setDescription($this->trans('commands.config.delete.description'))
65 InputArgument::OPTIONAL,
66 $this->trans('commands.config.delete.arguments.type')
70 InputArgument::OPTIONAL,
71 $this->trans('commands.config.delete.arguments.name')
72 )->setAliases(['cd']);
78 protected function interact(InputInterface $input, OutputInterface $output)
80 $type = $input->getArgument('type');
82 $type = $this->getIo()->choiceNoList(
83 $this->trans('commands.config.delete.arguments.type'),
84 ['active', 'staging'],
87 $input->setArgument('type', $type);
90 $name = $input->getArgument('name');
92 $name = $this->getIo()->choiceNoList(
93 $this->trans('commands.config.delete.arguments.name'),
94 $this->getAllConfigNames(),
97 $input->setArgument('name', $name);
104 protected function execute(InputInterface $input, OutputInterface $output)
106 $type = $input->getArgument('type');
108 $this->getIo()->error($this->trans('commands.config.delete.errors.type'));
112 $name = $input->getArgument('name');
114 $this->getIo()->error($this->trans('commands.config.delete.errors.name'));
118 $configStorage = ('active' === $type) ? $this->configStorage : $this->configStorageSync;
120 if (!$configStorage) {
121 $this->getIo()->error($this->trans('commands.config.delete.errors.config-storage'));
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();
131 foreach ($this->yieldAllConfig() as $name) {
132 $this->removeConfig($name);
136 $this->getIo()->success($this->trans('commands.config.delete.messages.all'));
142 if ($configStorage->exists($name)) {
143 if ($configStorage instanceof FileStorage) {
144 $configStorage->delete($name);
146 $this->removeConfig($name);
149 $this->getIo()->success(
151 $this->trans('commands.config.delete.messages.deleted'),
158 $message = sprintf($this->trans('commands.config.delete.errors.not-exists'), $name);
159 $this->getIo()->error($message);
165 * Retrieve configuration names form cache or service factory.
168 * All configuration names.
170 private function getAllConfigNames()
172 if ($this->allConfig) {
173 return $this->allConfig;
176 foreach ($this->configFactory->listAll() as $name) {
177 $this->allConfig[] = $name;
180 return $this->allConfig;
184 * Yield configuration names.
187 * Yield generator with config name.
189 private function yieldAllConfig()
191 $this->allConfig = $this->allConfig ?: $this->getAllConfigNames();
192 foreach ($this->allConfig as $name) {
198 * Delete given config name.
200 * @param String $name Given config name.
202 private function removeConfig($name)
205 $this->configFactory->getEditable($name)->delete();
206 } catch (\Exception $e) {
207 throw new RuntimeException($e->getMessage());