| 1 |
<?php |
| 2 |
|
| 3 |
namespace Aws\S3\S3Transfer\Progress; |
| 4 |
|
| 5 |
use Aws\S3\S3Transfer\Exception\ProgressTrackerException; |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
final class SingleProgressTracker extends AbstractTransferListener |
| 11 |
implements ProgressTrackerInterface |
| 12 |
{ |
| 13 |
|
| 14 |
private ProgressBarInterface $progressBar; |
| 15 |
|
| 16 |
|
| 17 |
private mixed $output; |
| 18 |
|
| 19 |
|
| 20 |
private bool $clear; |
| 21 |
|
| 22 |
|
| 23 |
private ?TransferProgressSnapshot $currentSnapshot; |
| 24 |
|
| 25 |
|
| 26 |
private bool $showProgressOnUpdate; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public function __construct( |
| 36 |
ProgressBarInterface $progressBar = new ConsoleProgressBar(), |
| 37 |
mixed $output = STDOUT, |
| 38 |
bool $clear = true, |
| 39 |
?TransferProgressSnapshot $currentSnapshot = null, |
| 40 |
bool $showProgressOnUpdate = true |
| 41 |
) { |
| 42 |
$this->progressBar = $progressBar; |
| 43 |
if (get_resource_type($output) !== 'stream') { |
| 44 |
throw new \InvalidArgumentException("The type for $output must be a stream"); |
| 45 |
} |
| 46 |
$this->output = $output; |
| 47 |
$this->clear = $clear; |
| 48 |
$this->currentSnapshot = $currentSnapshot; |
| 49 |
$this->showProgressOnUpdate = $showProgressOnUpdate; |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public function getProgressBar(): ProgressBarInterface |
| 56 |
{ |
| 57 |
return $this->progressBar; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public function getOutput(): mixed |
| 64 |
{ |
| 65 |
return $this->output; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public function isClear(): bool |
| 72 |
{ |
| 73 |
return $this->clear; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public function getCurrentSnapshot(): ?TransferProgressSnapshot |
| 80 |
{ |
| 81 |
return $this->currentSnapshot; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public function isShowProgressOnUpdate(): bool |
| 88 |
{ |
| 89 |
return $this->showProgressOnUpdate; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public function transferInitiated(array $context): void |
| 98 |
{ |
| 99 |
$this->currentSnapshot = $context[AbstractTransferListener::PROGRESS_SNAPSHOT_KEY]; |
| 100 |
$progressFormat = $this->progressBar->getProgressBarFormat(); |
| 101 |
|
| 102 |
$progressFormat->setArg( |
| 103 |
'object_name', |
| 104 |
$this->currentSnapshot->getIdentifier() |
| 105 |
); |
| 106 |
|
| 107 |
$this->updateProgressBar(); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
public function bytesTransferred(array $context): bool |
| 114 |
{ |
| 115 |
$this->currentSnapshot = $context[AbstractTransferListener::PROGRESS_SNAPSHOT_KEY]; |
| 116 |
$progressFormat = $this->progressBar->getProgressBarFormat(); |
| 117 |
if ($progressFormat instanceof ColoredTransferProgressBarFormat) { |
| 118 |
$progressFormat->setArg( |
| 119 |
'color_code', |
| 120 |
ColoredTransferProgressBarFormat::BLUE_COLOR_CODE |
| 121 |
); |
| 122 |
} |
| 123 |
|
| 124 |
$this->updateProgressBar(); |
| 125 |
|
| 126 |
return true; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
public function transferComplete(array $context): void |
| 135 |
{ |
| 136 |
$this->currentSnapshot = $context[AbstractTransferListener::PROGRESS_SNAPSHOT_KEY]; |
| 137 |
$progressFormat = $this->progressBar->getProgressBarFormat(); |
| 138 |
if ($progressFormat instanceof ColoredTransferProgressBarFormat) { |
| 139 |
$progressFormat->setArg( |
| 140 |
'color_code', |
| 141 |
ColoredTransferProgressBarFormat::GREEN_COLOR_CODE |
| 142 |
); |
| 143 |
} |
| 144 |
|
| 145 |
$this->updateProgressBar( |
| 146 |
$this->currentSnapshot->getTotalBytes() === 0 |
| 147 |
); |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
public function transferFail(array $context): void |
| 156 |
{ |
| 157 |
$this->currentSnapshot = $context[AbstractTransferListener::PROGRESS_SNAPSHOT_KEY]; |
| 158 |
$progressFormat = $this->progressBar->getProgressBarFormat(); |
| 159 |
if ($progressFormat instanceof ColoredTransferProgressBarFormat) { |
| 160 |
$progressFormat->setArg( |
| 161 |
'color_code', |
| 162 |
ColoredTransferProgressBarFormat::RED_COLOR_CODE |
| 163 |
); |
| 164 |
$progressFormat->setArg( |
| 165 |
'message', |
| 166 |
$context[AbstractTransferListener::REASON_KEY] |
| 167 |
); |
| 168 |
} |
| 169 |
|
| 170 |
$this->updateProgressBar(); |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
private function updateProgressBar( |
| 185 |
bool $forceCompletion = false |
| 186 |
): void |
| 187 |
{ |
| 188 |
if (!$forceCompletion) { |
| 189 |
$this->progressBar->setPercentCompleted( |
| 190 |
((int)floor($this->currentSnapshot->ratioTransferred() * 100)) |
| 191 |
); |
| 192 |
} else { |
| 193 |
$this->progressBar->setPercentCompleted(100); |
| 194 |
} |
| 195 |
|
| 196 |
$this->progressBar->getProgressBarFormat()->setArgs([ |
| 197 |
'transferred' => min( |
| 198 |
$this->currentSnapshot->getTransferredBytes(), |
| 199 |
$this->currentSnapshot->getTotalBytes() |
| 200 |
), |
| 201 |
'to_be_transferred' => $this->currentSnapshot->getTotalBytes(), |
| 202 |
'unit' => 'B', |
| 203 |
]); |
| 204 |
|
| 205 |
if ($this->showProgressOnUpdate) { |
| 206 |
$this->showProgress(); |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
public function showProgress(): void |
| 216 |
{ |
| 217 |
if ($this->currentSnapshot === null) { |
| 218 |
throw new ProgressTrackerException( |
| 219 |
"There is not snapshot to show progress for." |
| 220 |
); |
| 221 |
} |
| 222 |
|
| 223 |
if ($this->clear) { |
| 224 |
fwrite($this->output, "\033[2J\033[H"); |
| 225 |
} |
| 226 |
|
| 227 |
fwrite($this->output, sprintf( |
| 228 |
"\r\n%s", |
| 229 |
$this->progressBar->render() |
| 230 |
)); |
| 231 |
fflush($this->output); |
| 232 |
} |
| 233 |
} |