-
-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extracted functions to static methods (#113)
* Extracted functions to static methods * Update src/Utils.php Co-authored-by: Tobias Nyholm <tobias.nyholm@gmail.com>
- Loading branch information
1 parent
bbf3b20
commit 07e2de8
Showing
36 changed files
with
1,095 additions
and
553 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,7 +33,7 @@ | |
}, | ||
"extra": { | ||
"branch-alias": { | ||
"dev-master": "1.3-dev" | ||
"dev-master": "1.4-dev" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
<?php | ||
|
||
namespace GuzzleHttp\Promise; | ||
|
||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
<?php | ||
|
||
namespace GuzzleHttp\Promise; | ||
|
||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
namespace GuzzleHttp\Promise; | ||
|
||
final class Create | ||
{ | ||
/** | ||
* Creates a promise for a value if the value is not a promise. | ||
* | ||
* @param mixed $value Promise or value. | ||
* | ||
* @return PromiseInterface | ||
*/ | ||
public static function promiseFor($value) | ||
{ | ||
if ($value instanceof PromiseInterface) { | ||
return $value; | ||
} | ||
|
||
// Return a Guzzle promise that shadows the given promise. | ||
if (is_object($value) && method_exists($value, 'then')) { | ||
$wfn = method_exists($value, 'wait') ? [$value, 'wait'] : null; | ||
$cfn = method_exists($value, 'cancel') ? [$value, 'cancel'] : null; | ||
$promise = new Promise($wfn, $cfn); | ||
$value->then([$promise, 'resolve'], [$promise, 'reject']); | ||
return $promise; | ||
} | ||
|
||
return new FulfilledPromise($value); | ||
} | ||
|
||
/** | ||
* Creates a rejected promise for a reason if the reason is not a promise. | ||
* If the provided reason is a promise, then it is returned as-is. | ||
* | ||
* @param mixed $reason Promise or reason. | ||
* | ||
* @return PromiseInterface | ||
*/ | ||
public static function rejectionFor($reason) | ||
{ | ||
if ($reason instanceof PromiseInterface) { | ||
return $reason; | ||
} | ||
|
||
return new RejectedPromise($reason); | ||
} | ||
|
||
/** | ||
* Create an exception for a rejected promise value. | ||
* | ||
* @param mixed $reason | ||
* | ||
* @return \Exception|\Throwable | ||
*/ | ||
public static function exceptionFor($reason) | ||
{ | ||
if ($reason instanceof \Exception || $reason instanceof \Throwable) { | ||
return $reason; | ||
} | ||
|
||
return new RejectionException($reason); | ||
} | ||
|
||
/** | ||
* Returns an iterator for the given value. | ||
* | ||
* @param mixed $value | ||
* | ||
* @return \Iterator | ||
*/ | ||
public static function iterFor($value) | ||
{ | ||
if ($value instanceof \Iterator) { | ||
return $value; | ||
} | ||
|
||
if (is_array($value)) { | ||
return new \ArrayIterator($value); | ||
} | ||
|
||
return new \ArrayIterator([$value]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
|
||
namespace GuzzleHttp\Promise; | ||
|
||
final class Each | ||
{ | ||
/** | ||
* Given an iterator that yields promises or values, returns a promise that | ||
* is fulfilled with a null value when the iterator has been consumed or | ||
* the aggregate promise has been fulfilled or rejected. | ||
* | ||
* $onFulfilled is a function that accepts the fulfilled value, iterator | ||
* index, and the aggregate promise. The callback can invoke any necessary | ||
* side effects and choose to resolve or reject the aggregate if needed. | ||
* | ||
* $onRejected is a function that accepts the rejection reason, iterator | ||
* index, and the aggregate promise. The callback can invoke any necessary | ||
* side effects and choose to resolve or reject the aggregate if needed. | ||
* | ||
* @param mixed $iterable Iterator or array to iterate over. | ||
* @param callable $onFulfilled | ||
* @param callable $onRejected | ||
* | ||
* @return PromiseInterface | ||
*/ | ||
public static function of( | ||
$iterable, | ||
callable $onFulfilled = null, | ||
callable $onRejected = null | ||
) { | ||
return (new EachPromise($iterable, [ | ||
'fulfilled' => $onFulfilled, | ||
'rejected' => $onRejected | ||
]))->promise(); | ||
} | ||
|
||
/** | ||
* Like of, but only allows a certain number of outstanding promises at any | ||
* given time. | ||
* | ||
* $concurrency may be an integer or a function that accepts the number of | ||
* pending promises and returns a numeric concurrency limit value to allow | ||
* for dynamic a concurrency size. | ||
* | ||
* @param mixed $iterable | ||
* @param int|callable $concurrency | ||
* @param callable $onFulfilled | ||
* @param callable $onRejected | ||
* | ||
* @return PromiseInterface | ||
*/ | ||
public static function ofLimit( | ||
$iterable, | ||
$concurrency, | ||
callable $onFulfilled = null, | ||
callable $onRejected = null | ||
) { | ||
return (new EachPromise($iterable, [ | ||
'fulfilled' => $onFulfilled, | ||
'rejected' => $onRejected, | ||
'concurrency' => $concurrency | ||
]))->promise(); | ||
} | ||
|
||
/** | ||
* Like limit, but ensures that no promise in the given $iterable argument | ||
* is rejected. If any promise is rejected, then the aggregate promise is | ||
* rejected with the encountered rejection. | ||
* | ||
* @param mixed $iterable | ||
* @param int|callable $concurrency | ||
* @param callable $onFulfilled | ||
* | ||
* @return PromiseInterface | ||
*/ | ||
public static function ofLimitAll( | ||
$iterable, | ||
$concurrency, | ||
callable $onFulfilled = null | ||
) { | ||
return each_limit( | ||
$iterable, | ||
$concurrency, | ||
$onFulfilled, | ||
function ($reason, $idx, PromiseInterface $aggregate) { | ||
$aggregate->reject($reason); | ||
} | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.