Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / src / Command / Database / LogPollCommand.php
1 <?php
2
3 namespace Drupal\Console\Command\Database;
4
5 use Drupal\Console\Core\Style\DrupalStyle;
6 use Symfony\Component\Console\Input\InputArgument;
7 use Symfony\Component\Console\Input\InputInterface;
8 use Symfony\Component\Console\Output\OutputInterface;
9
10 /**
11  * Class LogPollCommand
12  *
13  * @package Drupal\Console\Command\Database
14  */
15 class LogPollCommand extends DatabaseLogBase
16 {
17     /**
18      * @var
19      */
20     protected $duration;
21
22     /**
23      * {@inheritdoc}
24      */
25     protected function execute(InputInterface $input, OutputInterface $output)
26     {
27         $io = new DrupalStyle($input, $output);
28
29         $io->note($this->trans('commands.database.log.poll.messages.warning'));
30
31         $this->getDefaultOptions($input);
32         $this->duration = $input->getArgument('duration');
33
34         $this->pollForEvents($io);
35     }
36
37     /**
38      * {@inheritdoc}
39      */
40     protected function configure()
41     {
42         $this
43             ->setName('database:log:poll')
44             ->setDescription($this->trans('commands.database.log.poll.description'));
45
46         $this->addDefaultLoggingOptions();
47
48         $this->addArgument(
49             'duration',
50             InputArgument::OPTIONAL,
51             $this->trans('commands.database.log.poll.arguments.duration'),
52             '10'
53         );
54     }
55
56     /**
57      * @param DrupalStyle $io
58      */
59     protected function pollForEvents(DrupalStyle $io)
60     {
61         $query = $this->makeQuery($io)->countQuery();
62         $results = $query->execute()->fetchAssoc();
63         $count = $results['expression'] - 1;//minus 1 so the newest message always prints
64
65         $tableHeader = $this->createTableHeader();
66
67         //Poll, force no wait on first loop
68         $lastExec = time() - $this->duration;
69         while (1) {
70             if (time() > $lastExec + $this->duration) {
71                 //Print out any new db logs
72                 $query = $this->makeQuery($io, $count);
73                 $results = $query->execute()->fetchAll();
74                 $count += count($results);
75                 $tableRows = [];
76                 foreach ($results as $r) {
77                     $tableRows[] = $this->createTableRow($r);
78                 }
79                 if (!empty($tableRows)) {
80                     $io->table($tableHeader, $tableRows);
81                 }
82                 //update the last exec time
83                 $lastExec = time();
84             }
85         }
86     }
87 }