diff options
Diffstat (limited to 'vendor/open-telemetry/sdk/Common/Future')
5 files changed, 160 insertions, 0 deletions
diff --git a/vendor/open-telemetry/sdk/Common/Future/CancellationInterface.php b/vendor/open-telemetry/sdk/Common/Future/CancellationInterface.php new file mode 100644 index 000000000..16909ec6d --- /dev/null +++ b/vendor/open-telemetry/sdk/Common/Future/CancellationInterface.php @@ -0,0 +1,18 @@ +<?php + +declare(strict_types=1); + +namespace OpenTelemetry\SDK\Common\Future; + +use Closure; +use Throwable; + +interface CancellationInterface +{ + /** + * @param Closure(Throwable): void $callback + */ + public function subscribe(Closure $callback): string; + + public function unsubscribe(string $id): void; +} diff --git a/vendor/open-telemetry/sdk/Common/Future/CompletedFuture.php b/vendor/open-telemetry/sdk/Common/Future/CompletedFuture.php new file mode 100644 index 000000000..7f0cd6536 --- /dev/null +++ b/vendor/open-telemetry/sdk/Common/Future/CompletedFuture.php @@ -0,0 +1,48 @@ +<?php + +declare(strict_types=1); + +namespace OpenTelemetry\SDK\Common\Future; + +use Closure; +use Throwable; + +/** + * @template T + * @template-implements FutureInterface<T> + */ +final class CompletedFuture implements FutureInterface +{ + /** @var T */ + private $value; + + /** + * @param T $value + */ + public function __construct($value) + { + $this->value = $value; + } + + public function await() + { + return $this->value; + } + + public function map(Closure $closure): FutureInterface + { + $c = $closure; + unset($closure); + + try { + return new CompletedFuture($c($this->value)); + } catch (Throwable $e) { + return new ErrorFuture($e); + } + } + + public function catch(Closure $closure): FutureInterface + { + return $this; + } +} diff --git a/vendor/open-telemetry/sdk/Common/Future/ErrorFuture.php b/vendor/open-telemetry/sdk/Common/Future/ErrorFuture.php new file mode 100644 index 000000000..32cf3d995 --- /dev/null +++ b/vendor/open-telemetry/sdk/Common/Future/ErrorFuture.php @@ -0,0 +1,40 @@ +<?php + +declare(strict_types=1); + +namespace OpenTelemetry\SDK\Common\Future; + +use Closure; +use Throwable; + +final class ErrorFuture implements FutureInterface +{ + private Throwable $throwable; + + public function __construct(Throwable $throwable) + { + $this->throwable = $throwable; + } + + public function await() + { + throw $this->throwable; + } + + public function map(Closure $closure): FutureInterface + { + return $this; + } + + public function catch(Closure $closure): FutureInterface + { + $c = $closure; + unset($closure); + + try { + return new CompletedFuture($c($this->throwable)); + } catch (Throwable $e) { + return new ErrorFuture($e); + } + } +} diff --git a/vendor/open-telemetry/sdk/Common/Future/FutureInterface.php b/vendor/open-telemetry/sdk/Common/Future/FutureInterface.php new file mode 100644 index 000000000..850699bf6 --- /dev/null +++ b/vendor/open-telemetry/sdk/Common/Future/FutureInterface.php @@ -0,0 +1,34 @@ +<?php + +declare(strict_types=1); + +namespace OpenTelemetry\SDK\Common\Future; + +use Closure; + +/** + * @template-covariant T + */ +interface FutureInterface +{ + /** + * @psalm-return T + */ + public function await(); + + /** + * @psalm-template U + * @psalm-param Closure(T): U $closure + * @psalm-return FutureInterface<U> + * + * @psalm-suppress InvalidTemplateParam + */ + public function map(Closure $closure): FutureInterface; + + /** + * @psalm-template U + * @psalm-param Closure(\Throwable): U $closure + * @psalm-return FutureInterface<T|U> + */ + public function catch(Closure $closure): FutureInterface; +} diff --git a/vendor/open-telemetry/sdk/Common/Future/NullCancellation.php b/vendor/open-telemetry/sdk/Common/Future/NullCancellation.php new file mode 100644 index 000000000..5e5b642f9 --- /dev/null +++ b/vendor/open-telemetry/sdk/Common/Future/NullCancellation.php @@ -0,0 +1,20 @@ +<?php + +declare(strict_types=1); + +namespace OpenTelemetry\SDK\Common\Future; + +use Closure; + +final class NullCancellation implements CancellationInterface +{ + public function subscribe(Closure $callback): string + { + return self::class; + } + + public function unsubscribe(string $id): void + { + // no-op + } +} |