‪TYPO3CMS  ‪main
ReferenceIndexProgressListener.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Psr\Log\LogLevel;
21 use Symfony\Component\Console\Helper\ProgressBar;
22 use Symfony\Component\Console\Style\SymfonyStyle;
24 
30 {
31  protected SymfonyStyle ‪$io;
32  protected ?ProgressBar ‪$progressBar = null;
33  protected bool ‪$isEnabled = false;
34 
35  public function ‪initialize(SymfonyStyle ‪$io)
36  {
37  $this->io = ‪$io;
38  $this->isEnabled = ‪$io->isQuiet() === false;
39  }
40 
41  public function ‪start(int $maxSteps = 0, string $additionalMessage = null): void
42  {
43  if (!$this->isEnabled) {
44  return;
45  }
46  $tableName = $additionalMessage;
47  if ($maxSteps > 0) {
48  $this->io->section('Update index of table ' . $tableName);
49  $this->progressBar = $this->io->createProgressBar($maxSteps);
50  $this->progressBar->start($maxSteps);
51  } else {
52  $this->io->section('Nothing to update for empty table ' . $tableName);
53  $this->progressBar = null;
54  }
55  }
56 
57  public function ‪advance(int $step = 1, string $additionalMessage = null): void
58  {
59  if (!$this->isEnabled) {
60  return;
61  }
62  if ($additionalMessage) {
63  $this->‪showMessageWhileInProgress(function () use ($additionalMessage) {
64  $this->io->writeln($additionalMessage);
65  });
66  }
67  if ($this->progressBar !== null) {
68  $this->progressBar->advance($step);
69  }
70  }
71 
72  public function ‪finish(string $additionalMessage = null): void
73  {
74  if (!$this->isEnabled) {
75  return;
76  }
77  if ($this->progressBar !== null) {
78  $this->progressBar->finish();
79  $this->progressBar = null;
80  $this->io->writeln(PHP_EOL);
81  }
82  if ($additionalMessage) {
83  $this->io->writeln($additionalMessage);
84  }
85  }
86 
87  public function ‪log(string $message, string $logLevel = LogLevel::INFO): void
88  {
89  if (!$this->isEnabled) {
90  return;
91  }
92  $this->‪showMessageWhileInProgress(function () use ($message, $logLevel) {
93  switch ($logLevel) {
94  case LogLevel::ERROR:
95  $this->io->error($message);
96  break;
97  case LogLevel::WARNING:
98  $this->io->warning($message);
99  break;
100  default:
101  $this->io->writeln($message);
102  }
103  });
104  }
105 
106  protected function ‪showMessageWhileInProgress(callable $messageFunction): void
107  {
108  if ($this->progressBar !== null) {
109  $this->progressBar->clear();
110  $messageFunction();
111  $this->progressBar->display();
112  } else {
113  $messageFunction();
114  }
115  }
116 }
‪TYPO3\CMS\Backend\Command\ProgressListener\ReferenceIndexProgressListener\initialize
‪initialize(SymfonyStyle $io)
Definition: ReferenceIndexProgressListener.php:35
‪TYPO3\CMS\Backend\Command\ProgressListener\ReferenceIndexProgressListener
Definition: ReferenceIndexProgressListener.php:30
‪TYPO3\CMS\Backend\Command\ProgressListener\ReferenceIndexProgressListener\$io
‪SymfonyStyle $io
Definition: ReferenceIndexProgressListener.php:31
‪TYPO3\CMS\Backend\Command\ProgressListener
Definition: ReferenceIndexProgressListener.php:18
‪TYPO3\CMS\Backend\Command\ProgressListener\ReferenceIndexProgressListener\$isEnabled
‪bool $isEnabled
Definition: ReferenceIndexProgressListener.php:33
‪TYPO3\CMS\Backend\Command\ProgressListener\ReferenceIndexProgressListener\advance
‪advance(int $step=1, string $additionalMessage=null)
Definition: ReferenceIndexProgressListener.php:57
‪TYPO3\CMS\Backend\Command\ProgressListener\ReferenceIndexProgressListener\showMessageWhileInProgress
‪showMessageWhileInProgress(callable $messageFunction)
Definition: ReferenceIndexProgressListener.php:106
‪TYPO3\CMS\Backend\View\ProgressListenerInterface
Definition: ProgressListenerInterface.php:31
‪TYPO3\CMS\Backend\Command\ProgressListener\ReferenceIndexProgressListener\$progressBar
‪ProgressBar $progressBar
Definition: ReferenceIndexProgressListener.php:32
‪TYPO3\CMS\Backend\Command\ProgressListener\ReferenceIndexProgressListener\finish
‪finish(string $additionalMessage=null)
Definition: ReferenceIndexProgressListener.php:72
‪TYPO3\CMS\Backend\Command\ProgressListener\ReferenceIndexProgressListener\start
‪start(int $maxSteps=0, string $additionalMessage=null)
Definition: ReferenceIndexProgressListener.php:41
‪TYPO3\CMS\Backend\Command\ProgressListener\ReferenceIndexProgressListener\log
‪log(string $message, string $logLevel=LogLevel::INFO)
Definition: ReferenceIndexProgressListener.php:87