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.
32 * @param NestedArray $nestedArray
34 public function __construct(NestedArray $nestedArray)
36 $this->nestedArray = $nestedArray;
37 parent::__construct();
40 protected function configure()
43 ->setName('yaml:unset:key')
44 ->setDescription($this->trans('commands.yaml.unset.key.description'))
47 InputArgument::REQUIRED,
48 $this->trans('commands.yaml.unset.value.arguments.yaml-file')
52 InputArgument::REQUIRED,
53 $this->trans('commands.yaml.unset.value.arguments.yaml-key')
57 protected function execute(InputInterface $input, OutputInterface $output)
59 $io = new DrupalStyle($input, $output);
62 $dumper = new Dumper();
64 $yaml_file = $input->getArgument('yaml-file');
65 $yaml_key = $input->getArgument('yaml-key');
68 $yaml_parsed = $yaml->parse(file_get_contents($yaml_file));
69 } catch (\Exception $e) {
70 $io->error($this->trans('commands.yaml.merge.messages.error-parsing').': '.$e->getMessage());
75 if (empty($yaml_parsed)) {
78 $this->trans('commands.yaml.merge.messages.wrong-parse'),
84 $parents = explode(".", $yaml_key);
85 $this->nestedArray->unsetValue($yaml_parsed, $parents);
88 $yaml = $dumper->dump($yaml_parsed, 10);
89 } catch (\Exception $e) {
90 $io->error($this->trans('commands.yaml.merge.messages.error-generating').': '.$e->getMessage());
96 file_put_contents($yaml_file, $yaml);
97 } catch (\Exception $e) {
98 $io->error($this->trans('commands.yaml.merge.messages.error-writing').': '.$e->getMessage());
105 $this->trans('commands.yaml.unset.value.messages.unset'),