5 * Contains \Drupal\Console\Core\Command\Yaml\UnsetKeyCommand.
8 namespace Drupal\Console\Core\Command\Yaml;
10 use Symfony\Component\Console\Input\InputArgument;
11 use Symfony\Component\Console\Input\InputInterface;
12 use Symfony\Component\Console\Output\OutputInterface;
13 use Symfony\Component\Yaml\Dumper;
14 use Symfony\Component\Yaml\Parser;
15 use Symfony\Component\Console\Command\Command;
16 use Drupal\Console\Core\Command\Shared\CommandTrait;
17 use Drupal\Console\Core\Style\DrupalStyle;
18 use Drupal\Console\Core\Utils\NestedArray;
20 class UnsetKeyCommand extends Command
27 protected $nestedArray;
30 * UnsetKeyCommand constructor.
31 * @param NestedArray $nestedArray
33 public function __construct(NestedArray $nestedArray)
35 $this->nestedArray = $nestedArray;
36 parent::__construct();
39 protected function configure()
42 ->setName('yaml:unset:key')
43 ->setDescription($this->trans('commands.yaml.unset.key.description'))
46 InputArgument::REQUIRED,
47 $this->trans('commands.yaml.unset.value.arguments.yaml-file')
51 InputArgument::REQUIRED,
52 $this->trans('commands.yaml.unset.value.arguments.yaml-key')
56 protected function execute(InputInterface $input, OutputInterface $output)
58 $io = new DrupalStyle($input, $output);
61 $dumper = new Dumper();
63 $yaml_file = $input->getArgument('yaml-file');
64 $yaml_key = $input->getArgument('yaml-key');
67 $yaml_parsed = $yaml->parse(file_get_contents($yaml_file));
68 } catch (\Exception $e) {
69 $io->error($this->trans('commands.yaml.merge.messages.error-parsing').': '.$e->getMessage());
74 if (empty($yaml_parsed)) {
77 $this->trans('commands.yaml.merge.messages.wrong-parse'),
83 $parents = explode(".", $yaml_key);
84 $this->nestedArray->unsetValue($yaml_parsed, $parents);
87 $yaml = $dumper->dump($yaml_parsed, 10);
88 } catch (\Exception $e) {
89 $io->error($this->trans('commands.yaml.merge.messages.error-generating').': '.$e->getMessage());
95 file_put_contents($yaml_file, $yaml);
96 } catch (\Exception $e) {
97 $io->error($this->trans('commands.yaml.merge.messages.error-writing').': '.$e->getMessage());
104 $this->trans('commands.yaml.unset.value.messages.unset'),