PluginProbe
Media Cloud Sync / 1.2.6
Media Cloud Sync v1.2.6
1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 All 34 releases
media-cloud-sync / includes / sdk / s3 / GuzzleHttp / Promise / Coroutine.php

Coroutine.php in Media Cloud Sync 1.2.6, at includes/sdk/s3/GuzzleHttp/Promise/Coroutine.php

152 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Dudlewebs\WPMCS\s3\GuzzleHttp\Promise;
4
5 use Exception;
6 use Generator;
7 use Throwable;
8 /**
9 * Creates a promise that is resolved using a generator that yields values or
10 * promises (somewhat similar to C#'s async keyword).
11 *
12 * When called, the Coroutine::of method will start an instance of the generator
13 * and returns a promise that is fulfilled with its final yielded value.
14 *
15 * Control is returned back to the generator when the yielded promise settles.
16 * This can lead to less verbose code when doing lots of sequential async calls
17 * with minimal processing in between.
18 *
19 * use GuzzleHttp\Promise;
20 *
21 * function createPromise($value) {
22 * return new Promise\FulfilledPromise($value);
23 * }
24 *
25 * $promise = Promise\Coroutine::of(function () {
26 * $value = (yield createPromise('a'));
27 * try {
28 * $value = (yield createPromise($value . 'b'));
29 * } catch (\Exception $e) {
30 * // The promise was rejected.
31 * }
32 * yield $value . 'c';
33 * });
34 *
35 * // Outputs "abc"
36 * $promise->then(function ($v) { echo $v; });
37 *
38 * @param callable $generatorFn Generator function to wrap into a promise.
39 *
40 * @return Promise
41 *
42 * @link https://github.com/petkaantonov/bluebird/blob/master/API.md#generators inspiration
43 */
44 final class Coroutine implements PromiseInterface
45 {
46 /**
47 * @var PromiseInterface|null
48 */
49 private $currentPromise;
50 /**
51 * @var Generator
52 */
53 private $generator;
54 /**
55 * @var Promise
56 */
57 private $result;
58 public function __construct(callable $generatorFn)
59 {
60 $this->generator = $generatorFn();
61 $this->result = new Promise(function () {
62 while (isset($this->currentPromise)) {
63 $this->currentPromise->wait();
64 }
65 });
66 try {
67 $this->nextCoroutine($this->generator->current());
68 } catch (\Exception $exception) {
69 $this->result->reject($exception);
70 } catch (Throwable $throwable) {
71 $this->result->reject($throwable);
72 }
73 }
74 /**
75 * Create a new coroutine.
76 *
77 * @return self
78 */
79 public static function of(callable $generatorFn)
80 {
81 return new self($generatorFn);
82 }
83 public function then(callable $onFulfilled = null, callable $onRejected = null)
84 {
85 return $this->result->then($onFulfilled, $onRejected);
86 }
87 public function otherwise(callable $onRejected)
88 {
89 return $this->result->otherwise($onRejected);
90 }
91 public function wait($unwrap = \true)
92 {
93 return $this->result->wait($unwrap);
94 }
95 public function getState()
96 {
97 return $this->result->getState();
98 }
99 public function resolve($value)
100 {
101 $this->result->resolve($value);
102 }
103 public function reject($reason)
104 {
105 $this->result->reject($reason);
106 }
107 public function cancel()
108 {
109 $this->currentPromise->cancel();
110 $this->result->cancel();
111 }
112 private function nextCoroutine($yielded)
113 {
114 $this->currentPromise = Create::promiseFor($yielded)->then([$this, '_handleSuccess'], [$this, '_handleFailure']);
115 }
116 /**
117 * @internal
118 */
119 public function _handleSuccess($value)
120 {
121 unset($this->currentPromise);
122 try {
123 $next = $this->generator->send($value);
124 if ($this->generator->valid()) {
125 $this->nextCoroutine($next);
126 } else {
127 $this->result->resolve($value);
128 }
129 } catch (Exception $exception) {
130 $this->result->reject($exception);
131 } catch (Throwable $throwable) {
132 $this->result->reject($throwable);
133 }
134 }
135 /**
136 * @internal
137 */
138 public function _handleFailure($reason)
139 {
140 unset($this->currentPromise);
141 try {
142 $nextYield = $this->generator->throw(Create::exceptionFor($reason));
143 // The throw was caught, so keep iterating on the coroutine
144 $this->nextCoroutine($nextYield);
145 } catch (Exception $exception) {
146 $this->result->reject($exception);
147 } catch (Throwable $throwable) {
148 $this->result->reject($throwable);
149 }
150 }
151 }
152