Debian Codegraph @

lib/3rdParty/composer/aws/aws-sdk-php/src/S3/S3Transfer/Progress/MultiProgressTracker.php

raw symbols SCIP
1 <?php
2
3 namespace Aws\S3\S3Transfer\Progress;
4
5 use Closure;
6
7 final class MultiProgressTracker extends AbstractTransferListener implements ProgressTrackerInterface
8 {
9 private const CLEAR_ASCII_CODE = "\033[2J\033[H";
10
11 /** @var array */
12 private array $singleProgressTrackers;
13
14 /** @var resource */
15 private mixed $output;
16
17 /** @var int */
18 private int $transferCount;
19
20 /** @var int */
21 private int $completed;
22
23 /** @var int */
24 private int $failed;
25
26 /** @var ProgressBarFactoryInterface|Closure|null */
27 private readonly ProgressBarFactoryInterface|Closure|null $progressBarFactory;
28
29 /**
30 * @param array $singleProgressTrackers
31 * @param mixed|false|resource $output
32 * @param int $transferCount
33 * @param int $completed
34 * @param int $failed
35 * @param ProgressBarFactoryInterface|Closure|null $progressBarFactory
36 */
37 public function __construct(
38 array $singleProgressTrackers = [],
39 mixed $output = STDOUT,
40 int $transferCount = 0,
41 int $completed = 0,
42 int $failed = 0,
43 ProgressBarFactoryInterface|Closure|null $progressBarFactory = null
44 )
45 {
46 $this->singleProgressTrackers = $singleProgressTrackers;
47 $this->output = $output;
48 $this->transferCount = $transferCount;
49 $this->completed = $completed;
50 $this->failed = $failed;
51 $this->progressBarFactory = $progressBarFactory;
52 }
53
54 /**
55 * @return array
56 */
57 public function getSingleProgressTrackers(): array
58 {
59 return $this->singleProgressTrackers;
60 }
61
62 /**
63 * @return mixed
64 */
65 public function getOutput(): mixed
66 {
67 return $this->output;
68 }
69
70 /**
71 * @return int
72 */
73 public function getTransferCount(): int
74 {
75 return $this->transferCount;
76 }
77
78 /**
79 * @return int
80 */
81 public function getCompleted(): int
82 {
83 return $this->completed;
84 }
85
86 /**
87 * @return int
88 */
89 public function getFailed(): int
90 {
91 return $this->failed;
92 }
93
94 /**
95 * @return ProgressBarFactoryInterface|Closure|null
96 */
97 public function getProgressBarFactory(): ProgressBarFactoryInterface|Closure|null
98 {
99 return $this->progressBarFactory;
100 }
101
102 /**
103 * @inheritDoc
104 */
105 public function transferInitiated(array $context): void
106 {
107 $this->transferCount++;
108 $snapshot = $context[AbstractTransferListener::PROGRESS_SNAPSHOT_KEY];
109 if (isset($this->singleProgressTrackers[$snapshot->getIdentifier()])) {
110 $progressTracker = $this->singleProgressTrackers[$snapshot->getIdentifier()];
111 } else {
112 if ($this->progressBarFactory === null) {
113 $progressTracker = new SingleProgressTracker(
114 output: $this->output,
115 clear: false,
116 showProgressOnUpdate: false,
117 );
118 } else {
119 $progressBarFactoryFn = $this->progressBarFactory;
120 $progressTracker = new SingleProgressTracker(
121 progressBar: $progressBarFactoryFn(),
122 output: $this->output,
123 clear: false,
124 showProgressOnUpdate: false,
125 );
126 }
127
128 $this->singleProgressTrackers[$snapshot->getIdentifier()] = $progressTracker;
129 }
130
131 $progressTracker->transferInitiated($context);
132 $this->showProgress();
133 }
134
135 /**
136 * @inheritDoc
137 */
138 public function bytesTransferred(array $context): bool
139 {
140 $snapshot = $context[AbstractTransferListener::PROGRESS_SNAPSHOT_KEY];
141 $progressTracker = $this->singleProgressTrackers[$snapshot->getIdentifier()];
142 $progressTracker->bytesTransferred($context);
143 $this->showProgress();
144
145 return true;
146 }
147
148 /**
149 * @inheritDoc
150 */
151 public function transferComplete(array $context): void
152 {
153 $this->completed++;
154 $snapshot = $context[AbstractTransferListener::PROGRESS_SNAPSHOT_KEY];
155 $progressTracker = $this->singleProgressTrackers[$snapshot->getIdentifier()];
156 $progressTracker->transferComplete($context);
157 $this->showProgress();
158 }
159
160 /**
161 * @inheritDoc
162 */
163 public function transferFail(array $context): void
164 {
165 $this->failed++;
166 $snapshot = $context[AbstractTransferListener::PROGRESS_SNAPSHOT_KEY];
167 $progressTracker = $this->singleProgressTrackers[$snapshot->getIdentifier()];
168 $progressTracker->transferFail($context);
169 $this->showProgress();
170 }
171
172 /**
173 * @inheritDoc
174 */
175 public function showProgress(): void
176 {
177 fwrite($this->output, self::CLEAR_ASCII_CODE);
178 $percentsSum = 0;
179 foreach ($this->singleProgressTrackers as $_ => $progressTracker) {
180 $progressTracker->showProgress();
181 $percentsSum += $progressTracker->getProgressBar()->getPercentCompleted();
182 }
183
184 $allProgressBarWidth = ConsoleProgressBar::DEFAULT_PROGRESS_BAR_WIDTH;
185 if (count($this->singleProgressTrackers) !== 0) {
186 $firstKey = array_key_first($this->singleProgressTrackers);
187 $allProgressBarWidth = $this->singleProgressTrackers[$firstKey]
188 ->getProgressBar()->getProgressBarWidth();
189 }
190
191 $percent = (int) floor($percentsSum / $this->transferCount);
192 $multiProgressBarFormat = new MultiProgressBarFormat();
193 $multiProgressBarFormat->setArgs([
194 'completed' => $this->completed,
195 'failed' => $this->failed,
196 'total' => $this->transferCount,
197 ]);
198 $allTransferProgressBar = new ConsoleProgressBar(
199 progressBarWidth: $allProgressBarWidth,
200 percentCompleted: $percent,
201 progressBarFormat: $multiProgressBarFormat
202 );
203 fwrite(
204 $this->output,
205 sprintf(
206 "\n%s\n",
207 str_repeat(
208 '-',
209 $allTransferProgressBar->getProgressBarWidth()
210 )
211 )
212 );
213 fwrite(
214 $this->output,
215 sprintf(
216 "%s\n",
217 $allTransferProgressBar->render(),
218 )
219 );
220 }
221 }