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