Part-DB /
Part-DB-symfony
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony). |
||
| 4 | * |
||
| 5 | * Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics) |
||
| 6 | * |
||
| 7 | * This program is free software: you can redistribute it and/or modify |
||
| 8 | * it under the terms of the GNU Affero General Public License as published |
||
| 9 | * by the Free Software Foundation, either version 3 of the License, or |
||
| 10 | * (at your option) any later version. |
||
| 11 | * |
||
| 12 | * This program is distributed in the hope that it will be useful, |
||
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
| 15 | * GNU Affero General Public License for more details. |
||
| 16 | * |
||
| 17 | * You should have received a copy of the GNU Affero General Public License |
||
| 18 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
||
| 19 | */ |
||
| 20 | |||
| 21 | declare(strict_types=1); |
||
| 22 | |||
| 23 | namespace App\Command\Logs; |
||
| 24 | |||
| 25 | use App\Entity\Base\AbstractNamedDBElement; |
||
| 26 | use App\Entity\LogSystem\AbstractLogEntry; |
||
| 27 | use App\Repository\LogEntryRepository; |
||
| 28 | use App\Services\ElementTypeNameGenerator; |
||
| 29 | use App\Services\LogSystem\LogEntryExtraFormatter; |
||
| 30 | use Doctrine\ORM\EntityManagerInterface; |
||
| 31 | use Symfony\Component\Console\Command\Command; |
||
| 32 | use Symfony\Component\Console\Helper\Table; |
||
| 33 | use Symfony\Component\Console\Input\InputInterface; |
||
| 34 | use Symfony\Component\Console\Input\InputOption; |
||
| 35 | use Symfony\Component\Console\Output\OutputInterface; |
||
| 36 | use Symfony\Component\Console\Style\SymfonyStyle; |
||
| 37 | use Symfony\Contracts\Translation\TranslatorInterface; |
||
| 38 | |||
| 39 | class ShowEventLogCommand extends Command |
||
| 40 | { |
||
| 41 | protected static $defaultName = 'partdb:logs:show|app:show-logs'; |
||
| 42 | protected EntityManagerInterface $entityManager; |
||
| 43 | protected TranslatorInterface $translator; |
||
| 44 | protected ElementTypeNameGenerator $elementTypeNameGenerator; |
||
| 45 | protected LogEntryRepository $repo; |
||
| 46 | protected LogEntryExtraFormatter $formatter; |
||
| 47 | |||
| 48 | public function __construct(EntityManagerInterface $entityManager, |
||
| 49 | TranslatorInterface $translator, ElementTypeNameGenerator $elementTypeNameGenerator, LogEntryExtraFormatter $formatter) |
||
| 50 | { |
||
| 51 | $this->entityManager = $entityManager; |
||
| 52 | $this->translator = $translator; |
||
| 53 | $this->elementTypeNameGenerator = $elementTypeNameGenerator; |
||
| 54 | $this->formatter = $formatter; |
||
| 55 | |||
| 56 | $this->repo = $this->entityManager->getRepository(AbstractLogEntry::class); |
||
| 57 | parent::__construct(); |
||
| 58 | } |
||
| 59 | |||
| 60 | public function execute(InputInterface $input, OutputInterface $output): int |
||
| 61 | { |
||
| 62 | $io = new SymfonyStyle($input, $output); |
||
| 63 | |||
| 64 | $onePage = $input->getOption('onePage'); |
||
| 65 | |||
| 66 | $desc = (bool) $input->getOption('oldest_first'); |
||
| 67 | $limit = (int) $input->getOption('count'); |
||
| 68 | $page = (int) $input->getOption('page'); |
||
| 69 | $showExtra = $input->getOption('showExtra'); |
||
| 70 | |||
| 71 | $total_count = $this->repo->count([]); |
||
| 72 | $max_page = (int) ceil($total_count / $limit); |
||
| 73 | |||
| 74 | if ($page > $max_page && $max_page > 0) { |
||
| 75 | $io->error("There is no page ${page}! The maximum page is ${max_page}."); |
||
| 76 | |||
| 77 | return 1; |
||
| 78 | } |
||
| 79 | |||
| 80 | $io->note("There are a total of ${total_count} log entries in the DB."); |
||
| 81 | |||
| 82 | $continue = true; |
||
| 83 | while ($continue && $page <= $max_page) { |
||
| 84 | $this->showPage($output, $desc, $limit, $page, $max_page, $showExtra); |
||
| 85 | |||
| 86 | if ($onePage) { |
||
| 87 | return 0; |
||
| 88 | } |
||
| 89 | |||
| 90 | $continue = $io->confirm('Do you want to show the next page?'); |
||
| 91 | ++$page; |
||
| 92 | } |
||
| 93 | |||
| 94 | return 0; |
||
| 95 | } |
||
| 96 | |||
| 97 | protected function configure(): void |
||
| 98 | { |
||
| 99 | $this |
||
| 100 | ->setDescription('List the last event log entries.') |
||
| 101 | ->addOption('count', 'c', InputOption::VALUE_REQUIRED, 'How many log entries should be shown per page.', 50) |
||
| 102 | ->addOption('oldest_first', null, InputOption::VALUE_NONE, 'Show older entries first.') |
||
| 103 | ->addOption('page', 'p', InputOption::VALUE_REQUIRED, 'Which page should be shown?', 1) |
||
| 104 | ->addOption('onePage', null, InputOption::VALUE_NONE, 'Show only one page (dont ask to go to next).') |
||
| 105 | ->addOption('showExtra', 'x', InputOption::VALUE_NONE, 'Show a column with the extra data.'); |
||
| 106 | } |
||
| 107 | |||
| 108 | protected function showPage(OutputInterface $output, bool $desc, int $limit, int $page, int $max_page, bool $showExtra): void |
||
| 109 | { |
||
| 110 | $sorting = $desc ? 'ASC' : 'DESC'; |
||
| 111 | $offset = ($page - 1) * $limit; |
||
| 112 | |||
| 113 | /** @var AbstractLogEntry[] $entries */ |
||
| 114 | $entries = $this->repo->getLogsOrderedByTimestamp($sorting, $limit, $offset); |
||
| 115 | |||
| 116 | $table = new Table($output); |
||
| 117 | $table->setHeaderTitle("Page ${page} / ${max_page}"); |
||
| 118 | $headers = ['ID', 'Timestamp', 'Type', 'User', 'Target Type', 'Target']; |
||
| 119 | if ($showExtra) { |
||
| 120 | $headers[] = 'Extra data'; |
||
| 121 | $table->setColumnMaxWidth(6, 50); |
||
| 122 | } |
||
| 123 | $table->setHeaders($headers); |
||
| 124 | |||
| 125 | foreach ($entries as $entry) { |
||
| 126 | $this->addTableRow($table, $entry, $showExtra); |
||
| 127 | } |
||
| 128 | |||
| 129 | $table->setColumnMaxWidth(3, 20); |
||
| 130 | $table->setColumnMaxWidth(5, 30); |
||
| 131 | |||
| 132 | $table->render(); |
||
| 133 | } |
||
| 134 | |||
| 135 | protected function addTableRow(Table $table, AbstractLogEntry $entry, bool $showExtra): void |
||
| 136 | { |
||
| 137 | $target = $this->repo->getTargetElement($entry); |
||
| 138 | $target_name = ''; |
||
| 139 | if ($target instanceof AbstractNamedDBElement) { |
||
| 140 | $target_name = $target->getName().' <info>('.$target->getID().')</info>'; |
||
| 141 | } elseif ($entry->getTargetID()) { |
||
|
0 ignored issues
–
show
|
|||
| 142 | $target_name = '<info>('.$entry->getTargetID().')</info>'; |
||
| 143 | } |
||
| 144 | |||
| 145 | $target_class = ''; |
||
| 146 | if (null !== $entry->getTargetClass()) { |
||
| 147 | $target_class = $this->elementTypeNameGenerator->getLocalizedTypeLabel($entry->getTargetClass()); |
||
| 148 | } |
||
| 149 | |||
| 150 | $row = [ |
||
| 151 | $entry->getID(), |
||
| 152 | $entry->getTimestamp()->format('Y-m-d H:i:s'), |
||
| 153 | $entry->getType(), |
||
| 154 | $entry->getUser()->getFullName(true), |
||
| 155 | $target_class, |
||
| 156 | $target_name, |
||
| 157 | ]; |
||
| 158 | |||
| 159 | if ($showExtra) { |
||
| 160 | $row[] = $this->formatter->formatConsole($entry); |
||
| 161 | } |
||
| 162 | |||
| 163 | $table->addRow($row); |
||
| 164 | } |
||
| 165 | } |
||
| 166 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: