| @@ -4,8 +4,9 @@ | ||
| 4 | 4 | |
| 5 | 5 | use Dudlewebs\WPMCS\s3\Aws; |
| 6 | 6 | use Dudlewebs\WPMCS\s3\Aws\CommandInterface; |
| 7 | 7 | use Dudlewebs\WPMCS\s3\Aws\Exception\AwsException; |
| 8 | +use Dudlewebs\WPMCS\s3\Aws\MetricsBuilder; | |
| 8 | 9 | use Dudlewebs\WPMCS\s3\GuzzleHttp\Promise; |
| 9 | 10 | use Dudlewebs\WPMCS\s3\GuzzleHttp\Promise\PromiseInterface; |
| 10 | 11 | use Dudlewebs\WPMCS\s3\GuzzleHttp\Promise\PromisorInterface; |
| 11 | 12 | use Iterator; |
| @@ -25,8 +26,9 @@ | ||
| 25 | 26 | private $destination; |
| 26 | 27 | private $concurrency; |
| 27 | 28 | private $mupThreshold; |
| 28 | 29 | private $before; |
| 30 | + private $after; | |
| 29 | 31 | private $s3Args = []; |
| 30 | 32 | private $addContentMD5; |
| 31 | 33 | /** |
| 32 | 34 | * When providing the $source argument, you may provide a string referencing |
| @@ -50,8 +52,13 @@ | ||
| 50 | 52 | * - before: (callable) A callback to invoke before each transfer. The |
| 51 | 53 | * callback accepts a single argument: Aws\CommandInterface $command. |
| 52 | 54 | * The provided command will be either a GetObject, PutObject, |
| 53 | 55 | * InitiateMultipartUpload, or UploadPart command. |
| 56 | + * - after: (callable) A callback to invoke after each transfer promise is fulfilled. | |
| 57 | + * The function is invoked with three arguments: the fulfillment value, the index | |
| 58 | + * position from the iterable list of the promise, and the aggregate | |
| 59 | + * promise that manages all the promises. The aggregate promise may | |
| 60 | + * be resolved from within the callback to short-circuit the promise. | |
| 54 | 61 | * - mup_threshold: (int) Size in bytes in which a multipart upload should |
| 55 | 62 | * be used instead of PutObject. Defaults to 20971520 (20 MB). |
| 56 | 63 | * - concurrency: (int, default=5) Number of files to upload concurrently. |
| 57 | 64 | * The ideal concurrency value will vary based on the number of files |
| @@ -104,8 +111,15 @@ | ||
| 104 | 111 | if (!\is_callable($this->before)) { |
| 105 | 112 | throw new \InvalidArgumentException('before must be a callable.'); |
| 106 | 113 | } |
| 107 | 114 | } |
| 115 | + // Handle "after" callback option. | |
| 116 | + if (isset($options['after'])) { | |
| 117 | + $this->after = $options['after']; | |
| 118 | + if (!\is_callable($this->after)) { | |
| 119 | + throw new \InvalidArgumentException('after must be a callable.'); | |
| 120 | + } | |
| 121 | + } | |
| 108 | 122 | // Handle "debug" option. |
| 109 | 123 | if (isset($options['debug'])) { |
| 110 | 124 | if ($options['debug'] === \true) { |
| 111 | 125 | $options['debug'] = \fopen('php://output', 'w'); |
| @@ -115,8 +129,9 @@ | ||
| 115 | 129 | } |
| 116 | 130 | } |
| 117 | 131 | // Handle "add_content_md5" option. |
| 118 | 132 | $this->addContentMD5 = isset($options['add_content_md5']) && $options['add_content_md5'] === \true; |
| 133 | + MetricsBuilder::appendMetricsCaptureMiddleware($this->client->getHandlerList(), MetricsBuilder::S3_TRANSFER); | |
| 119 | 134 | } |
| 120 | 135 | /** |
| 121 | 136 | * Transfers the files. |
| 122 | 137 | * |
| @@ -227,9 +242,9 @@ | ||
| 227 | 242 | // Create the command. |
| 228 | 243 | $commands[] = $command; |
| 229 | 244 | } |
| 230 | 245 | // Create a GetObject command pool and return the promise. |
| 231 | - return (new Aws\CommandPool($this->client, $commands, ['concurrency' => $this->concurrency, 'before' => $this->before, 'rejected' => function ($reason, $idx, Promise\PromiseInterface $p) { | |
| 246 | + return (new Aws\CommandPool($this->client, $commands, ['concurrency' => $this->concurrency, 'before' => $this->before, 'fulfill' => $this->after, 'rejected' => function ($reason, $idx, Promise\PromiseInterface $p) { | |
| 232 | 247 | $p->reject($reason); |
| 233 | 248 | }]))->promise(); |
| 234 | 249 | } |
| 235 | 250 | private function createUploadPromise() |
| @@ -239,9 +254,9 @@ | ||
| 239 | 254 | return \filesize($file) >= $this->mupThreshold ? $this->uploadMultipart($file) : $this->upload($file); |
| 240 | 255 | }); |
| 241 | 256 | // Create an EachPromise, that will concurrently handle the upload |
| 242 | 257 | // operations' yielded promises from the iterator. |
| 243 | - return Promise\Each::ofLimitAll($files, $this->concurrency); | |
| 258 | + return Promise\Each::ofLimitAll($files, $this->concurrency, $this->after); | |
| 244 | 259 | } |
| 245 | 260 | /** @return Iterator */ |
| 246 | 261 | private function getUploadsIterator() |
| 247 | 262 | { |