Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2016
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.06 KB | None | 0 0
  1. <?php
  2.  
  3. namespace React\Promise;
  4.  
  5. function resolve($promiseOrValue = null)
  6. {
  7. if (!$promiseOrValue instanceof PromiseInterface) {
  8. return new FulfilledPromise($promiseOrValue);
  9. }
  10.  
  11. if ($promiseOrValue instanceof ExtendedPromiseInterface) {
  12. return $promiseOrValue;
  13. }
  14.  
  15. return new Promise(function ($resolve, $reject, $notify) use ($promiseOrValue) {
  16. $promiseOrValue->then($resolve, $reject, $notify);
  17. });
  18. }
  19.  
  20. function reject($promiseOrValue = null)
  21. {
  22. if ($promiseOrValue instanceof PromiseInterface) {
  23. return resolve($promiseOrValue)->then(function ($value) {
  24. return new RejectedPromise($value);
  25. });
  26. }
  27.  
  28. return new RejectedPromise($promiseOrValue);
  29. }
  30.  
  31. function all($promisesOrValues)
  32. {
  33. return map($promisesOrValues, function ($val) {
  34. return $val;
  35. });
  36. }
  37.  
  38. function race($promisesOrValues)
  39. {
  40. return resolve($promisesOrValues)
  41. ->then(function ($array) {
  42. if (!is_array($array) || !$array) {
  43. return resolve();
  44. }
  45.  
  46. return new Promise(function ($resolve, $reject, $notify) use ($array) {
  47. foreach ($array as $promiseOrValue) {
  48. resolve($promiseOrValue)
  49. ->done($resolve, $reject, $notify);
  50. }
  51. });
  52. });
  53. }
  54.  
  55. function any($promisesOrValues)
  56. {
  57. return some($promisesOrValues, 1)
  58. ->then(function ($val) {
  59. return array_shift($val);
  60. });
  61. }
  62.  
  63. function some($promisesOrValues, $howMany)
  64. {
  65. return resolve($promisesOrValues)
  66. ->then(function ($array) use ($howMany) {
  67. if (!is_array($array) || !$array || $howMany < 1) {
  68. return resolve([]);
  69. }
  70.  
  71. return new Promise(function ($resolve, $reject, $notify) use ($array, $howMany) {
  72. $len = count($array);
  73. $toResolve = min($howMany, $len);
  74. $toReject = ($len - $toResolve) + 1;
  75. $values = [];
  76. $reasons = [];
  77.  
  78. foreach ($array as $i => $promiseOrValue) {
  79. $fulfiller = function ($val) use ($i, &$values, &$toResolve, $toReject, $resolve) {
  80. if ($toResolve < 1 || $toReject < 1) {
  81. return;
  82. }
  83.  
  84. $values[$i] = $val;
  85.  
  86. if (0 === --$toResolve) {
  87. $resolve($values);
  88. }
  89. };
  90.  
  91. $rejecter = function ($reason) use ($i, &$reasons, &$toReject, $toResolve, $reject) {
  92. if ($toResolve < 1 || $toReject < 1) {
  93. return;
  94. }
  95.  
  96. $reasons[$i] = $reason;
  97.  
  98. if (0 === --$toReject) {
  99. $reject($reasons);
  100. }
  101. };
  102.  
  103. resolve($promiseOrValue)
  104. ->done($fulfiller, $rejecter, $notify);
  105. }
  106. });
  107. });
  108. }
  109.  
  110. function map($promisesOrValues, callable $mapFunc)
  111. {
  112. return resolve($promisesOrValues)
  113. ->then(function ($array) use ($mapFunc) {
  114. if (!is_array($array) || !$array) {
  115. return resolve([]);
  116. }
  117.  
  118. return new Promise(function ($resolve, $reject, $notify) use ($array, $mapFunc) {
  119. $toResolve = count($array);
  120. $values = [];
  121.  
  122. foreach ($array as $i => $promiseOrValue) {
  123. resolve($promiseOrValue)
  124. ->then($mapFunc)
  125. ->done(
  126. function ($mapped) use ($i, &$values, &$toResolve, $resolve) {
  127. $values[$i] = $mapped;
  128.  
  129. if (0 === --$toResolve) {
  130. $resolve($values);
  131. }
  132. },
  133. $reject,
  134. $notify
  135. );
  136. }
  137. });
  138. });
  139. }
  140.  
  141. function reduce($promisesOrValues, callable $reduceFunc, $initialValue = null)
  142. {
  143. return resolve($promisesOrValues)
  144. ->then(function ($array) use ($reduceFunc, $initialValue) {
  145. if (!is_array($array)) {
  146. $array = [];
  147. }
  148.  
  149. $total = count($array);
  150. $i = 0;
  151.  
  152. // Wrap the supplied $reduceFunc with one that handles promises and then
  153. // delegates to the supplied.
  154. $wrappedReduceFunc = function ($current, $val) use ($reduceFunc, $total, &$i) {
  155. return resolve($current)
  156. ->then(function ($c) use ($reduceFunc, $total, &$i, $val) {
  157. return resolve($val)
  158. ->then(function ($value) use ($reduceFunc, $total, &$i, $c) {
  159. return $reduceFunc($c, $value, $i++, $total);
  160. });
  161. });
  162. };
  163.  
  164. return array_reduce($array, $wrappedReduceFunc, $initialValue);
  165. });
  166. }
  167.  
  168. // Internal functions
  169. function _checkTypehint(callable $callback, $object)
  170. {
  171. if (!is_object($object)) {
  172. return true;
  173. }
  174.  
  175. if (is_array($callback)) {
  176. $callbackReflection = new \ReflectionMethod($callback[0], $callback[1]);
  177. } elseif (is_object($callback) && !$callback instanceof \Closure) {
  178. $callbackReflection = new \ReflectionMethod($callback, '__invoke');
  179. } else {
  180. $callbackReflection = new \ReflectionFunction($callback);
  181. }
  182.  
  183. $parameters = $callbackReflection->getParameters();
  184.  
  185. if (!isset($parameters[0])) {
  186. return true;
  187. }
  188.  
  189. $expectedException = $parameters[0];
  190.  
  191. if (!$expectedException->getClass()) {
  192. return true;
  193. }
  194.  
  195. return $expectedException->getClass()->isInstance($object);
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement