|
3 | 3 | namespace LogicalSteps\Async;
|
4 | 4 |
|
5 | 5 |
|
| 6 | +use Amp\Loop\Driver; |
6 | 7 | use Closure;
|
| 8 | +use Exception; |
7 | 9 | use Generator;
|
8 | 10 | use Psr\Log\LoggerInterface;
|
9 | 11 | use React\EventLoop\LoopInterface;
|
10 |
| -use Amp\Loop\Driver; |
11 | 12 | use React\Promise\Promise;
|
12 | 13 | use React\Promise\PromiseInterface;
|
13 | 14 | use ReflectionException;
|
|
18 | 19 | use ReflectionObject;
|
19 | 20 | use Throwable;
|
20 | 21 | use TypeError;
|
| 22 | +use UnexpectedValueException; |
21 | 23 |
|
22 | 24 | use function GuzzleHttp\Promise\all as guzzleAll;
|
23 | 25 |
|
|
45 | 47 | */
|
46 | 48 | class Async
|
47 | 49 | {
|
48 |
| - const PROMISE_REACT = 'React\Promise\PromiseInterface'; |
49 |
| - const PROMISE_AMP = 'Amp\Promise'; |
50 |
| - const PROMISE_GUZZLE = 'GuzzleHttp\Promise\PromiseInterface'; |
51 |
| - const PROMISE_HTTP = 'Http\Promise\Promise'; |
| 50 | + public const PROMISE_REACT = 'React\Promise\PromiseInterface'; |
| 51 | + public const PROMISE_AMP = 'Amp\Promise'; |
| 52 | + public const PROMISE_GUZZLE = 'GuzzleHttp\Promise\PromiseInterface'; |
| 53 | + public const PROMISE_HTTP = 'Http\Promise\Promise'; |
52 | 54 |
|
53 | 55 | /** @var string action to return a promise instead of awaiting the response of the process. */
|
54 |
| - const promise = 'promise'; |
| 56 | + public const promise = 'promise'; |
55 | 57 | /** @var string action to run current process side by side with the remainder of the process. */
|
56 |
| - const parallel = 'parallel'; |
| 58 | + public const parallel = 'parallel'; |
57 | 59 | /** @var string action to await for all parallel processes previously to finish. */
|
58 |
| - const all = 'all'; |
| 60 | + public const all = 'all'; |
59 | 61 | /** @var string action to await for current processes to finish. this is the default action. */
|
60 |
| - const await = 'await'; |
| 62 | + public const await = 'await'; |
61 | 63 | /** @var string action to run current process after finished executing the function. */
|
62 |
| - const later = 'later'; |
| 64 | + public const later = 'later'; |
63 | 65 |
|
64 |
| - const ACTIONS = [self::await, self::parallel, self::all, self::promise, self::later]; |
| 66 | + public const ACTIONS = [self::await, self::parallel, self::all, self::promise, self::later]; |
65 | 67 |
|
66 | 68 | public static $knownPromises = [
|
67 | 69 | self::PROMISE_REACT,
|
@@ -288,8 +290,8 @@ protected function _wait($process)
|
288 | 290 | $this->logger->info('end');
|
289 | 291 | }
|
290 | 292 | if ($isRejected) {
|
291 |
| - if (!$exception instanceof \Exception) { |
292 |
| - $exception = new \UnexpectedValueException( |
| 293 | + if (!$exception instanceof Exception) { |
| 294 | + $exception = new UnexpectedValueException( |
293 | 295 | 'process failed with ' . (is_object($exception) ? get_class($exception) : gettype($exception))
|
294 | 296 | );
|
295 | 297 | }
|
@@ -434,7 +436,7 @@ function ($error = null, $results = null) {
|
434 | 436 | /**
|
435 | 437 | * Handle known promise interfaces
|
436 | 438 | *
|
437 |
| - * @param \React\Promise\PromiseInterface|\GuzzleHttp\Promise\PromiseInterface|\Amp\Promise|\Http\Promise\Promise $knownPromise |
| 439 | + * @param PromiseInterface|\GuzzleHttp\Promise\PromiseInterface|\Amp\Promise|\Http\Promise\Promise $knownPromise |
438 | 440 | * @param string $interface
|
439 | 441 | * @param callable $callback
|
440 | 442 | * @param int $depth
|
@@ -478,7 +480,7 @@ function ($error = null, $result = null) use ($resolver, $rejector) {
|
478 | 480 | );
|
479 | 481 | break;
|
480 | 482 | }
|
481 |
| - } catch (\Exception $e) { |
| 483 | + } catch (Exception $e) { |
482 | 484 | $rejector($e);
|
483 | 485 | }
|
484 | 486 | }
|
@@ -512,14 +514,12 @@ private function logCallback(callable $callable, array $parameters, int $depth =
|
512 | 514 | } else {
|
513 | 515 | $name .= '::' . $callable[1];
|
514 | 516 | }
|
| 517 | + } elseif (is_string($callable)) { |
| 518 | + $name = $callable; |
| 519 | + } elseif ($callable instanceof Closure) { |
| 520 | + $name = '$closure'; |
515 | 521 | } else {
|
516 |
| - if (is_string($callable)) { |
517 |
| - $name = $callable; |
518 |
| - } elseif ($callable instanceof Closure) { |
519 |
| - $name = '$closure'; |
520 |
| - } else { |
521 |
| - $name = '$callable'; |
522 |
| - } |
| 522 | + $name = '$callable'; |
523 | 523 | }
|
524 | 524 | $this->logger->info(
|
525 | 525 | sprintf("%s %s%s", $this->action(), $name, $this->format($parameters)),
|
|
0 commit comments