| 1 |
<?php |
| 2 |
|
| 3 |
namespace Aws\S3\S3Transfer\Progress; |
| 4 |
|
| 5 |
use Aws\S3\S3Transfer\Exception\ProgressTrackerException; |
| 6 |
use Aws\S3\S3Transfer\Progress\ConsoleProgressBar; |
| 7 |
use Aws\S3\S3Transfer\Progress\DirectoryTransferProgressSnapshot; |
| 8 |
use Aws\S3\S3Transfer\Progress\ProgressBarInterface; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
final class DirectoryProgressTracker extends AbstractTransferListener implements ProgressTrackerInterface |
| 14 |
{ |
| 15 |
|
| 16 |
private ProgressBarInterface $progressBar; |
| 17 |
|
| 18 |
|
| 19 |
private mixed $output; |
| 20 |
|
| 21 |
|
| 22 |
private bool $clear; |
| 23 |
|
| 24 |
|
| 25 |
private ?DirectoryTransferProgressSnapshot $currentSnapshot; |
| 26 |
|
| 27 |
|
| 28 |
private bool $showProgressOnUpdate; |
| 29 |
|
| 30 |
public function __construct( |
| 31 |
ProgressBarInterface $progressBar = new ConsoleProgressBar(), |
| 32 |
mixed $output = STDOUT, |
| 33 |
bool $clear = true, |
| 34 |
?DirectoryTransferProgressSnapshot $currentSnapshot = null, |
| 35 |
bool $showProgressOnUpdate = true |
| 36 |
) { |
| 37 |
$this->progressBar = $progressBar; |
| 38 |
if (get_resource_type($output) !== 'stream') { |
| 39 |
throw new \InvalidArgumentException("The type for $output must be a stream"); |
| 40 |
} |
| 41 |
$this->output = $output; |
| 42 |
$this->clear = $clear; |
| 43 |
$this->currentSnapshot = $currentSnapshot; |
| 44 |
$this->showProgressOnUpdate = $showProgressOnUpdate; |
| 45 |
} |
| 46 |
|
| 47 |
public function getProgressBar(): ProgressBarInterface |
| 48 |
{ |
| 49 |
return $this->progressBar; |
| 50 |
} |
| 51 |
|
| 52 |
public function transferInitiated(array $context): void |
| 53 |
{ |
| 54 |
$this->currentSnapshot = $context[self::PROGRESS_SNAPSHOT_KEY]; |
| 55 |
$progressFormat = $this->progressBar->getProgressBarFormat(); |
| 56 |
|
| 57 |
$progressFormat->setArg( |
| 58 |
'object_name', |
| 59 |
$this->currentSnapshot->getIdentifier() |
| 60 |
); |
| 61 |
$this->updateProgressBar(); |
| 62 |
} |
| 63 |
|
| 64 |
public function bytesTransferred(array $context): bool |
| 65 |
{ |
| 66 |
$this->currentSnapshot = $context[self::PROGRESS_SNAPSHOT_KEY]; |
| 67 |
$this->updateProgressBar(); |
| 68 |
|
| 69 |
return true; |
| 70 |
} |
| 71 |
|
| 72 |
public function transferComplete(array $context): void |
| 73 |
{ |
| 74 |
$this->currentSnapshot = $context[self::PROGRESS_SNAPSHOT_KEY]; |
| 75 |
$this->updateProgressBar(true); |
| 76 |
} |
| 77 |
|
| 78 |
public function transferFail(array $context): void |
| 79 |
{ |
| 80 |
$this->currentSnapshot = $context[self::PROGRESS_SNAPSHOT_KEY]; |
| 81 |
$this->updateProgressBar(); |
| 82 |
} |
| 83 |
|
| 84 |
public function showProgress(): void |
| 85 |
{ |
| 86 |
if ($this->currentSnapshot === null) { |
| 87 |
throw new ProgressTrackerException("There is not snapshot to show progress for."); |
| 88 |
} |
| 89 |
|
| 90 |
if ($this->clear) { |
| 91 |
fwrite($this->output, "\033[2J\033[H"); |
| 92 |
} |
| 93 |
|
| 94 |
fwrite($this->output, sprintf( |
| 95 |
"\r\n%s", |
| 96 |
$this->progressBar->render() |
| 97 |
)); |
| 98 |
fflush($this->output); |
| 99 |
} |
| 100 |
|
| 101 |
private function updateProgressBar(bool $forceCompletion = false): void |
| 102 |
{ |
| 103 |
if ($this->currentSnapshot === null) { |
| 104 |
return; |
| 105 |
} |
| 106 |
|
| 107 |
if (!$forceCompletion) { |
| 108 |
$percent = (int) floor($this->currentSnapshot->ratioTransferred() * 100); |
| 109 |
$this->progressBar->setPercentCompleted($percent); |
| 110 |
} else { |
| 111 |
$this->progressBar->setPercentCompleted(100); |
| 112 |
} |
| 113 |
|
| 114 |
$this->progressBar->getProgressBarFormat()->setArgs([ |
| 115 |
'transferred' => min( |
| 116 |
$this->currentSnapshot->getTransferredBytes(), |
| 117 |
$this->currentSnapshot->getTotalBytes() |
| 118 |
), |
| 119 |
'to_be_transferred' => $this->currentSnapshot->getTotalBytes(), |
| 120 |
'unit' => 'B', |
| 121 |
]); |
| 122 |
|
| 123 |
if ($this->showProgressOnUpdate) { |
| 124 |
$this->showProgress(); |
| 125 |
} |
| 126 |
} |
| 127 |
} |