‪TYPO3CMS  10.4
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 {
34  protected ‪$io;
35 
39  protected ‪$progressBar;
40 
44  protected ‪$isEnabled = false;
45 
46  public function ‪initialize(SymfonyStyle ‪$io)
47  {
48  $this->io = ‪$io;
49  $this->isEnabled = ‪$io->isQuiet() === false;
50  }
51 
52  public function ‪start(int $maxSteps = 0, string $additionalMessage = null): void
53  {
54  if (!$this->isEnabled) {
55  return;
56  }
57  $tableName = $additionalMessage;
58  if ($maxSteps > 0) {
59  $this->io->section('Update index of table ' . $tableName);
60  $this->progressBar = $this->io->createProgressBar($maxSteps);
61  $this->progressBar->start($maxSteps);
62  } else {
63  $this->io->section('Nothing to update for table ' . $tableName);
64  $this->progressBar = null;
65  }
66  }
67 
68  public function ‪advance(int $step = 1, string $additionalMessage = null): void
69  {
70  if (!$this->isEnabled) {
71  return;
72  }
73  if ($additionalMessage) {
74  $this->‪showMessageWhileInProgress(function () use ($additionalMessage) {
75  $this->io->writeln($additionalMessage);
76  });
77  }
78  if ($this->progressBar !== null) {
79  $this->progressBar->advance();
80  }
81  }
82 
83  public function ‪finish(string $additionalMessage = null): void
84  {
85  if (!$this->isEnabled) {
86  return;
87  }
88  if ($this->progressBar !== null) {
89  $this->progressBar->finish();
90  $this->progressBar = null;
91  }
92  $this->io->writeln(PHP_EOL);
93  if ($additionalMessage) {
94  $this->io->writeln($additionalMessage);
95  }
96  }
97 
98  public function ‪log(string $message, string $logLevel = LogLevel::INFO): void
99  {
100  if (!$this->isEnabled) {
101  return;
102  }
103  $this->‪showMessageWhileInProgress(function () use ($message, $logLevel) {
104  switch ($logLevel) {
105  case LogLevel::ERROR:
106  $this->io->error($message);
107  break;
108  case LogLevel::WARNING:
109  $this->io->warning($message);
110  break;
111  default:
112  $this->io->writeln($message);
113  }
114  });
115  }
116 
117  protected function ‪showMessageWhileInProgress(callable $messageFunction): void
118  {
119  if ($this->progressBar !== null) {
120  $this->progressBar->clear();
121  $messageFunction();
122  $this->progressBar->display();
123  } else {
124  $messageFunction();
125  }
126  }
127 }
‪TYPO3\CMS\Backend\Command\ProgressListener\ReferenceIndexProgressListener\initialize
‪initialize(SymfonyStyle $io)
Definition: ReferenceIndexProgressListener.php:43
‪TYPO3\CMS\Backend\Command\ProgressListener\ReferenceIndexProgressListener
Definition: ReferenceIndexProgressListener.php:30
‪TYPO3\CMS\Backend\Command\ProgressListener\ReferenceIndexProgressListener\$io
‪SymfonyStyle $io
Definition: ReferenceIndexProgressListener.php:33
‪TYPO3\CMS\Backend\Command\ProgressListener
Definition: ReferenceIndexProgressListener.php:18
‪TYPO3\CMS\Backend\Command\ProgressListener\ReferenceIndexProgressListener\$isEnabled
‪bool $isEnabled
Definition: ReferenceIndexProgressListener.php:41
‪TYPO3\CMS\Backend\Command\ProgressListener\ReferenceIndexProgressListener\advance
‪advance(int $step=1, string $additionalMessage=null)
Definition: ReferenceIndexProgressListener.php:65
‪TYPO3\CMS\Backend\Command\ProgressListener\ReferenceIndexProgressListener\showMessageWhileInProgress
‪showMessageWhileInProgress(callable $messageFunction)
Definition: ReferenceIndexProgressListener.php:114
‪TYPO3\CMS\Backend\View\ProgressListenerInterface
Definition: ProgressListenerInterface.php:31
‪TYPO3\CMS\Backend\Command\ProgressListener\ReferenceIndexProgressListener\$progressBar
‪ProgressBar $progressBar
Definition: ReferenceIndexProgressListener.php:37
‪TYPO3\CMS\Backend\Command\ProgressListener\ReferenceIndexProgressListener\finish
‪finish(string $additionalMessage=null)
Definition: ReferenceIndexProgressListener.php:80
‪TYPO3\CMS\Backend\Command\ProgressListener\ReferenceIndexProgressListener\start
‪start(int $maxSteps=0, string $additionalMessage=null)
Definition: ReferenceIndexProgressListener.php:49
‪TYPO3\CMS\Backend\Command\ProgressListener\ReferenceIndexProgressListener\log
‪log(string $message, string $logLevel=LogLevel::INFO)
Definition: ReferenceIndexProgressListener.php:95