This commit is contained in:
2026-05-01 23:40:14 +08:00
commit b8f599a617
3867 changed files with 478663 additions and 0 deletions
@@ -0,0 +1,95 @@
<?php
namespace Illuminate\Http\Client\Promises;
use GuzzleHttp\Promise\PromiseInterface;
use Illuminate\Support\Traits\ForwardsCalls;
/**
* A decorated Promise which allows for chaining callbacks.
*/
class FluentPromise implements PromiseInterface
{
use ForwardsCalls;
/**
* Create a new fluent promise instance.
*
* @param \GuzzleHttp\Promise\PromiseInterface $guzzlePromise
*/
public function __construct(protected PromiseInterface $guzzlePromise)
{
}
#[\Override]
public function then(?callable $onFulfilled = null, ?callable $onRejected = null): PromiseInterface
{
return $this->__call('then', [$onFulfilled, $onRejected]);
}
#[\Override]
public function otherwise(callable $onRejected): PromiseInterface
{
return $this->__call('otherwise', [$onRejected]);
}
#[\Override]
public function resolve($value): void
{
$this->guzzlePromise->resolve($value);
}
#[\Override]
public function reject($reason): void
{
$this->guzzlePromise->reject($reason);
}
#[\Override]
public function cancel(): void
{
$this->guzzlePromise->cancel();
}
#[\Override]
public function wait(bool $unwrap = true)
{
return $this->__call('wait', [$unwrap]);
}
#[\Override]
public function getState(): string
{
return $this->guzzlePromise->getState();
}
/**
* Get the underlying Guzzle promise.
*
* @return \GuzzleHttp\Promise\PromiseInterface
*/
public function getGuzzlePromise(): PromiseInterface
{
return $this->guzzlePromise;
}
/**
* Proxy requests to the underlying promise interface and update the local promise.
*
* @param string $method
* @param array $parameters
* @return mixed
*/
public function __call($method, $parameters)
{
$result = $this->forwardCallTo($this->guzzlePromise, $method, $parameters);
if (! $result instanceof PromiseInterface) {
return $result;
}
$this->guzzlePromise = $result;
return $this;
}
}
+129
View File
@@ -0,0 +1,129 @@
<?php
namespace Illuminate\Http\Client\Promises;
use Closure;
use GuzzleHttp\Promise\PromiseInterface;
use RuntimeException;
class LazyPromise implements PromiseInterface
{
/**
* The callbacks to execute after the Guzzle Promise has been built.
*
* @var list<callable>
*/
protected array $pending = [];
/**
* The promise built by the creator.
*
* @var \GuzzleHttp\Promise\PromiseInterface
*/
protected PromiseInterface $guzzlePromise;
/**
* Create a new lazy promise instance.
*
* @param (\Closure(): \GuzzleHttp\Promise\PromiseInterface) $promiseBuilder The callback to build a new PromiseInterface.
*/
public function __construct(protected Closure $promiseBuilder)
{
}
/**
* Build the promise from the promise builder.
*
* @return \GuzzleHttp\Promise\PromiseInterface
*
* @throws \RuntimeException If the promise has already been built
*/
public function buildPromise(): PromiseInterface
{
if (! $this->promiseNeedsBuilt()) {
throw new RuntimeException('Promise already built');
}
$this->guzzlePromise = call_user_func($this->promiseBuilder);
foreach ($this->pending as $pendingCallback) {
$pendingCallback($this->guzzlePromise);
}
$this->pending = [];
return $this->guzzlePromise;
}
#[\Override]
public function then(?callable $onFulfilled = null, ?callable $onRejected = null): PromiseInterface
{
if ($this->promiseNeedsBuilt()) {
$this->pending[] = static fn (PromiseInterface $promise) => $promise->then($onFulfilled, $onRejected);
return $this;
}
return $this->guzzlePromise->then($onFulfilled, $onRejected);
}
#[\Override]
public function otherwise(callable $onRejected): PromiseInterface
{
if ($this->promiseNeedsBuilt()) {
$this->pending[] = static fn (PromiseInterface $promise) => $promise->otherwise($onRejected);
return $this;
}
return $this->guzzlePromise->otherwise($onRejected);
}
#[\Override]
public function getState(): string
{
if ($this->promiseNeedsBuilt()) {
return PromiseInterface::PENDING;
}
return $this->guzzlePromise->getState();
}
#[\Override]
public function resolve($value): void
{
throw new \LogicException('Cannot resolve a lazy promise.');
}
#[\Override]
public function reject($reason): void
{
throw new \LogicException('Cannot reject a lazy promise.');
}
#[\Override]
public function cancel(): void
{
throw new \LogicException('Cannot cancel a lazy promise.');
}
#[\Override]
public function wait(bool $unwrap = true)
{
if ($this->promiseNeedsBuilt()) {
$this->buildPromise();
}
return $this->guzzlePromise->wait($unwrap);
}
/**
* Determine if the promise has been created from the promise builder.
*
* @return bool
*/
public function promiseNeedsBuilt(): bool
{
return ! isset($this->guzzlePromise);
}
}