Version 1
[yaffs-website] / vendor / drupal / console / src / Command / State / DebugCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\State\DebugCommand.
6  */
7
8 namespace Drupal\Console\Command\State;
9
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\Console\Command\Command;
14 use Drupal\Core\KeyValueStore\KeyValueFactoryInterface;
15 use Drupal\Core\State\StateInterface;
16 use Drupal\Console\Core\Command\Shared\CommandTrait;
17 use Drupal\Console\Core\Style\DrupalStyle;
18 use Drupal\Component\Serialization\Yaml;
19
20 /**
21  * Class DebugCommand
22  *
23  * @package Drupal\Console\Command\State
24  */
25 class DebugCommand extends Command
26 {
27     use CommandTrait;
28
29     /**
30      * @var StateInterface
31      */
32     protected $state;
33
34     /**
35      * @var KeyValueFactoryInterface
36      */
37     protected $keyValue;
38
39     /**
40      * DebugCommand constructor.
41      *
42      * @param StateInterface           $state
43      * @param KeyValueFactoryInterface $keyValue
44      */
45     public function __construct(
46         StateInterface $state,
47         KeyValueFactoryInterface $keyValue
48     ) {
49         $this->state = $state;
50         $this->keyValue = $keyValue;
51         parent::__construct();
52     }
53
54     /**
55      * {@inheritdoc}
56      */
57     protected function configure()
58     {
59         $this
60             ->setName('state:debug')
61             ->setDescription($this->trans('commands.state.debug.description'))
62             ->setHelp($this->trans('commands.state.debug.help'))
63             ->addArgument(
64                 'key',
65                 InputArgument::OPTIONAL,
66                 $this->trans('commands.state.debug.arguments.key')
67             );
68     }
69
70     /**
71      * {@inheritdoc}
72      */
73     protected function execute(InputInterface $input, OutputInterface $output)
74     {
75         $io = new DrupalStyle($input, $output);
76         $key = $input->getArgument('key');
77
78         if ($key) {
79             $io->info($key);
80             $io->writeln(Yaml::encode($this->state->get($key)));
81
82             return 0;
83         }
84
85         $tableHeader = [$this->trans('commands.state.debug.messages.key')];
86         $keyStoreStates = array_keys($this->keyValue->get('state')->getAll());
87         $io->table($tableHeader, $keyStoreStates);
88
89         return 0;
90     }
91 }