Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / src / Command / Database / LogDebugCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Database\LogDebugCommand.
6  */
7
8 namespace Drupal\Console\Command\Database;
9
10 use Symfony\Component\Console\Input\InputArgument;
11 use Symfony\Component\Console\Input\InputOption;
12 use Symfony\Component\Console\Input\InputInterface;
13 use Symfony\Component\Console\Output\OutputInterface;
14 use Drupal\Component\Serialization\Yaml;
15 use Drupal\Console\Core\Style\DrupalStyle;
16
17 /**
18  * Class LogDebugCommand
19  *
20  * @package Drupal\Console\Command\Database
21  */
22 class LogDebugCommand extends DatabaseLogBase
23 {
24     /**
25    * @var
26    */
27     protected $eventId;
28
29     /**
30    * @var
31    */
32     protected $asc;
33     /**
34    * @var
35    */
36     protected $limit;
37     /**
38    * @var
39    */
40     protected $offset;
41
42     /**
43    * Print in yml style if true
44    *
45    * @var bool
46    */
47     protected $ymlStyle;
48
49     /**
50    * {@inheritdoc}
51    */
52     protected function configure()
53     {
54         $this
55             ->setName('database:log:debug')
56             ->setDescription($this->trans('commands.database.log.debug.description'));
57
58         $this->addDefaultLoggingOptions();
59
60         $this
61             ->addArgument(
62                 'event-id',
63                 InputArgument::OPTIONAL,
64                 $this->trans('commands.database.log.debug.arguments.event-id')
65             )
66             ->addOption(
67                 'asc',
68                 null,
69                 InputOption::VALUE_NONE,
70                 $this->trans('commands.database.log.debug.options.asc')
71             )
72             ->addOption(
73                 'limit',
74                 null,
75                 InputOption::VALUE_OPTIONAL,
76                 $this->trans('commands.database.log.debug.options.limit')
77             )
78             ->addOption(
79                 'offset',
80                 null,
81                 InputOption::VALUE_OPTIONAL,
82                 $this->trans('commands.database.log.debug.options.offset'),
83                 0
84             )
85             ->addOption(
86                 'yml',
87                 null,
88                 InputOption::VALUE_NONE,
89                 $this->trans('commands.database.log.debug.options.yml'),
90                 null
91             );
92     }
93
94     /**
95    * {@inheritdoc}
96    */
97     protected function execute(InputInterface $input, OutputInterface $output)
98     {
99         $io = new DrupalStyle($input, $output);
100
101         $this->getDefaultOptions($input);
102         $this->eventId = $input->getArgument('event-id');
103         $this->asc = $input->getOption('asc');
104         $this->limit = $input->getOption('limit');
105         $this->offset = $input->getOption('offset');
106         $this->ymlStyle = $input->getOption('yml');
107
108
109         if ($this->eventId) {
110             return $this->getEventDetails($io);
111         } else {
112             return $this->getAllEvents($io);
113         }
114     }
115
116     /**
117    * @param $io
118    * @param $eventId
119    * @return bool
120    */
121     private function getEventDetails(DrupalStyle $io)
122     {
123         $dblog = $this->database
124             ->query(
125                 'SELECT w.*, u.uid FROM {watchdog} w LEFT JOIN {users} u ON u.uid = w.uid WHERE w.wid = :id',
126                 [':id' => $this->eventId]
127             )
128             ->fetchObject();
129
130         if (!$dblog) {
131             $io->error(
132                 sprintf(
133                     $this->trans('commands.database.log.debug.messages.not-found'),
134                     $this->eventId
135                 )
136             );
137             return 1;
138         }
139
140         if ($this->ymlStyle) {
141             $io->writeln(Yaml::encode($this->formatSingle($dblog)));
142         } else {
143             $io->table(
144                 $this->createTableHeader(),
145                 [$this->createTableRow($dblog)]
146             );
147         }
148     }
149
150     /**
151    * @param \Drupal\Console\Core\Style\DrupalStyle $io
152    * @return bool
153    */
154     private function getAllEvents(DrupalStyle $io)
155     {
156         $query = $this->makeQuery($io);
157
158         $result = $query->execute();
159
160         $tableRows = [];
161         foreach ($result as $dblog) {
162             $tableRows[] = $this->createTableRow($dblog);
163         }
164
165         $io->table(
166             $this->createTableHeader(),
167             $tableRows
168         );
169
170         return true;
171     }
172 }