| 1 |
<?php |
| 2 |
|
| 3 |
namespace React\Promise; |
| 4 |
|
| 5 |
function resolve($promiseOrValue = null) |
| 6 |
{ |
| 7 |
if ($promiseOrValue instanceof ExtendedPromiseInterface) { |
| 8 |
return $promiseOrValue; |
| 9 |
} |
| 10 |
|
| 11 |
// Check is_object() first to avoid method_exists() triggering |
| 12 |
// class autoloaders if $promiseOrValue is a string. |
| 13 |
if (\is_object($promiseOrValue) && \method_exists($promiseOrValue, 'then')) { |
| 14 |
$canceller = null; |
| 15 |
|
| 16 |
if (\method_exists($promiseOrValue, 'cancel')) { |
| 17 |
$canceller = [$promiseOrValue, 'cancel']; |
| 18 |
} |
| 19 |
|
| 20 |
return new Promise(function ($resolve, $reject, $notify) use ($promiseOrValue) { |
| 21 |
$promiseOrValue->then($resolve, $reject, $notify); |
| 22 |
}, $canceller); |
| 23 |
} |
| 24 |
|
| 25 |
return new FulfilledPromise($promiseOrValue); |
| 26 |
} |
| 27 |
|
| 28 |
function reject($promiseOrValue = null) |
| 29 |
{ |
| 30 |
if ($promiseOrValue instanceof PromiseInterface) { |
| 31 |
return resolve($promiseOrValue)->then(function ($value) { |
| 32 |
return new RejectedPromise($value); |
| 33 |
}); |
| 34 |
} |
| 35 |
|
| 36 |
return new RejectedPromise($promiseOrValue); |
| 37 |
} |
| 38 |
|
| 39 |
function all($promisesOrValues) |
| 40 |
{ |
| 41 |
return map($promisesOrValues, function ($val) { |
| 42 |
return $val; |
| 43 |
}); |
| 44 |
} |
| 45 |
|
| 46 |
function race($promisesOrValues) |
| 47 |
{ |
| 48 |
$cancellationQueue = new CancellationQueue(); |
| 49 |
$cancellationQueue->enqueue($promisesOrValues); |
| 50 |
|
| 51 |
return new Promise(function ($resolve, $reject, $notify) use ($promisesOrValues, $cancellationQueue) { |
| 52 |
resolve($promisesOrValues) |
| 53 |
->done(function ($array) use ($cancellationQueue, $resolve, $reject, $notify) { |
| 54 |
if (!is_array($array) || !$array) { |
| 55 |
$resolve(); |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
foreach ($array as $promiseOrValue) { |
| 60 |
$cancellationQueue->enqueue($promiseOrValue); |
| 61 |
|
| 62 |
resolve($promiseOrValue) |
| 63 |
->done($resolve, $reject, $notify); |
| 64 |
} |
| 65 |
}, $reject, $notify); |
| 66 |
}, $cancellationQueue); |
| 67 |
} |
| 68 |
|
| 69 |
function any($promisesOrValues) |
| 70 |
{ |
| 71 |
return some($promisesOrValues, 1) |
| 72 |
->then(function ($val) { |
| 73 |
return \array_shift($val); |
| 74 |
}); |
| 75 |
} |
| 76 |
|
| 77 |
function some($promisesOrValues, $howMany) |
| 78 |
{ |
| 79 |
$cancellationQueue = new CancellationQueue(); |
| 80 |
$cancellationQueue->enqueue($promisesOrValues); |
| 81 |
|
| 82 |
return new Promise(function ($resolve, $reject, $notify) use ($promisesOrValues, $howMany, $cancellationQueue) { |
| 83 |
resolve($promisesOrValues) |
| 84 |
->done(function ($array) use ($howMany, $cancellationQueue, $resolve, $reject, $notify) { |
| 85 |
if (!\is_array($array) || $howMany < 1) { |
| 86 |
$resolve([]); |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
$len = \count($array); |
| 91 |
|
| 92 |
if ($len < $howMany) { |
| 93 |
throw new Exception\LengthException( |
| 94 |
\sprintf( |
| 95 |
'Input array must contain at least %d item%s but contains only %s item%s.', |
| 96 |
$howMany, |
| 97 |
1 === $howMany ? '' : 's', |
| 98 |
$len, |
| 99 |
1 === $len ? '' : 's' |
| 100 |
) |
| 101 |
); |
| 102 |
} |
| 103 |
|
| 104 |
$toResolve = $howMany; |
| 105 |
$toReject = ($len - $toResolve) + 1; |
| 106 |
$values = []; |
| 107 |
$reasons = []; |
| 108 |
|
| 109 |
foreach ($array as $i => $promiseOrValue) { |
| 110 |
$fulfiller = function ($val) use ($i, &$values, &$toResolve, $toReject, $resolve) { |
| 111 |
if ($toResolve < 1 || $toReject < 1) { |
| 112 |
return; |
| 113 |
} |
| 114 |
|
| 115 |
$values[$i] = $val; |
| 116 |
|
| 117 |
if (0 === --$toResolve) { |
| 118 |
$resolve($values); |
| 119 |
} |
| 120 |
}; |
| 121 |
|
| 122 |
$rejecter = function ($reason) use ($i, &$reasons, &$toReject, $toResolve, $reject) { |
| 123 |
if ($toResolve < 1 || $toReject < 1) { |
| 124 |
return; |
| 125 |
} |
| 126 |
|
| 127 |
$reasons[$i] = $reason; |
| 128 |
|
| 129 |
if (0 === --$toReject) { |
| 130 |
$reject($reasons); |
| 131 |
} |
| 132 |
}; |
| 133 |
|
| 134 |
$cancellationQueue->enqueue($promiseOrValue); |
| 135 |
|
| 136 |
resolve($promiseOrValue) |
| 137 |
->done($fulfiller, $rejecter, $notify); |
| 138 |
} |
| 139 |
}, $reject, $notify); |
| 140 |
}, $cancellationQueue); |
| 141 |
} |
| 142 |
|
| 143 |
function map($promisesOrValues, callable $mapFunc) |
| 144 |
{ |
| 145 |
$cancellationQueue = new CancellationQueue(); |
| 146 |
$cancellationQueue->enqueue($promisesOrValues); |
| 147 |
|
| 148 |
return new Promise(function ($resolve, $reject, $notify) use ($promisesOrValues, $mapFunc, $cancellationQueue) { |
| 149 |
resolve($promisesOrValues) |
| 150 |
->done(function ($array) use ($mapFunc, $cancellationQueue, $resolve, $reject, $notify) { |
| 151 |
if (!\is_array($array) || !$array) { |
| 152 |
$resolve([]); |
| 153 |
return; |
| 154 |
} |
| 155 |
|
| 156 |
$toResolve = \count($array); |
| 157 |
$values = []; |
| 158 |
|
| 159 |
foreach ($array as $i => $promiseOrValue) { |
| 160 |
$cancellationQueue->enqueue($promiseOrValue); |
| 161 |
$values[$i] = null; |
| 162 |
|
| 163 |
resolve($promiseOrValue) |
| 164 |
->then($mapFunc) |
| 165 |
->done( |
| 166 |
function ($mapped) use ($i, &$values, &$toResolve, $resolve) { |
| 167 |
$values[$i] = $mapped; |
| 168 |
|
| 169 |
if (0 === --$toResolve) { |
| 170 |
$resolve($values); |
| 171 |
} |
| 172 |
}, |
| 173 |
$reject, |
| 174 |
$notify |
| 175 |
); |
| 176 |
} |
| 177 |
}, $reject, $notify); |
| 178 |
}, $cancellationQueue); |
| 179 |
} |
| 180 |
|
| 181 |
function reduce($promisesOrValues, callable $reduceFunc, $initialValue = null) |
| 182 |
{ |
| 183 |
$cancellationQueue = new CancellationQueue(); |
| 184 |
$cancellationQueue->enqueue($promisesOrValues); |
| 185 |
|
| 186 |
return new Promise(function ($resolve, $reject, $notify) use ($promisesOrValues, $reduceFunc, $initialValue, $cancellationQueue) { |
| 187 |
resolve($promisesOrValues) |
| 188 |
->done(function ($array) use ($reduceFunc, $initialValue, $cancellationQueue, $resolve, $reject, $notify) { |
| 189 |
if (!\is_array($array)) { |
| 190 |
$array = []; |
| 191 |
} |
| 192 |
|
| 193 |
$total = \count($array); |
| 194 |
$i = 0; |
| 195 |
|
| 196 |
// Wrap the supplied $reduceFunc with one that handles promises and then |
| 197 |
// delegates to the supplied. |
| 198 |
$wrappedReduceFunc = function ($current, $val) use ($reduceFunc, $cancellationQueue, $total, &$i) { |
| 199 |
$cancellationQueue->enqueue($val); |
| 200 |
|
| 201 |
return $current |
| 202 |
->then(function ($c) use ($reduceFunc, $total, &$i, $val) { |
| 203 |
return resolve($val) |
| 204 |
->then(function ($value) use ($reduceFunc, $total, &$i, $c) { |
| 205 |
return $reduceFunc($c, $value, $i++, $total); |
| 206 |
}); |
| 207 |
}); |
| 208 |
}; |
| 209 |
|
| 210 |
$cancellationQueue->enqueue($initialValue); |
| 211 |
|
| 212 |
\array_reduce($array, $wrappedReduceFunc, resolve($initialValue)) |
| 213 |
->done($resolve, $reject, $notify); |
| 214 |
}, $reject, $notify); |
| 215 |
}, $cancellationQueue); |
| 216 |
} |
| 217 |
|
| 218 |
// Internal functions |
| 219 |
function _checkTypehint(callable $callback, $object) |
| 220 |
{ |
| 221 |
if (!\is_object($object)) { |
| 222 |
return true; |
| 223 |
} |
| 224 |
|
| 225 |
if (\is_array($callback)) { |
| 226 |
$callbackReflection = new \ReflectionMethod($callback[0], $callback[1]); |
| 227 |
} elseif (\is_object($callback) && !$callback instanceof \Closure) { |
| 228 |
$callbackReflection = new \ReflectionMethod($callback, '__invoke'); |
| 229 |
} else { |
| 230 |
$callbackReflection = new \ReflectionFunction($callback); |
| 231 |
} |
| 232 |
|
| 233 |
$parameters = $callbackReflection->getParameters(); |
| 234 |
|
| 235 |
if (!isset($parameters[0])) { |
| 236 |
return true; |
| 237 |
} |
| 238 |
|
| 239 |
$expectedException = $parameters[0]; |
| 240 |
|
| 241 |
if (!$expectedException->getClass()) { |
| 242 |
return true; |
| 243 |
} |
| 244 |
|
| 245 |
return $expectedException->getClass()->isInstance($object); |
| 246 |
} |
| 247 |
|