diff --git a/src/Context/ScopeBound/ContextHolder.php b/src/Context/ScopeBound/ContextHolder.php deleted file mode 100644 index ff5780c13..000000000 --- a/src/Context/ScopeBound/ContextHolder.php +++ /dev/null @@ -1,20 +0,0 @@ -context = $context; - } -} diff --git a/src/Context/ScopeBound/ScopeBoundCallable.php b/src/Context/ScopeBound/ScopeBoundCallable.php deleted file mode 100644 index 969b14f51..000000000 --- a/src/Context/ScopeBound/ScopeBoundCallable.php +++ /dev/null @@ -1,31 +0,0 @@ -current(); - - return function (...$args) use ($callable, $context, $storage) { - $scope = $storage->attach($context); - - try { - return $callable(...$args); - } finally { - $scope->detach(); - } - }; - } -} diff --git a/src/Context/ScopeBound/ScopeBoundPromise.php b/src/Context/ScopeBound/ScopeBoundPromise.php deleted file mode 100644 index cd26e99f9..000000000 --- a/src/Context/ScopeBound/ScopeBoundPromise.php +++ /dev/null @@ -1,77 +0,0 @@ -storage = $storage; - $this->contextHolder = $contextHolder; - $this->promise = $promise; - } - - public static function wrap(object $promise, ?ContextStorageInterface $storage = null): ScopeBoundPromise - { - if (!method_exists($promise, 'then')) { - throw new InvalidArgumentException(); - } - - $storage ??= Context::storage(); - - return $promise instanceof ScopeBoundPromise && $storage === $promise->storage - ? $promise - : new self($storage, new ContextHolder($storage->current()), $promise); - } - - public function then(callable $onFulfilled = null, callable $onRejected = null): ScopeBoundPromise - { - $contextHolder = new ContextHolder($this->storage->current()); - - return new self($this->storage, $contextHolder, $this->promise->then( - $this->wrapCallback($onFulfilled, $contextHolder), - $this->wrapCallback($onRejected, $contextHolder), - )); - } - - private function wrapCallback(?callable $callable, ContextHolder $child): ?callable - { - if (!$callable) { - return null; - } - - return function (...$args) use ($callable, $child) { - $scope = $this->storage->attach($this->contextHolder->context); - $scope[__CLASS__] = $sentinel = new stdClass(); - - try { - return $callable(...$args); - } finally { - $child->context = $this->storage->current(); - while (($s = $this->storage->scope()) && ($s[__CLASS__] ?? null) !== $sentinel) { - $s->detach(); - } - - $scope->detach(); - } - }; - } -} diff --git a/tests/Unit/Context/ScopeBound/ScopeBoundCallableTest.php b/tests/Unit/Context/ScopeBound/ScopeBoundCallableTest.php deleted file mode 100644 index 090b9537f..000000000 --- a/tests/Unit/Context/ScopeBound/ScopeBoundCallableTest.php +++ /dev/null @@ -1,33 +0,0 @@ -attach($storage->current()->with($contextKey, 'value')); - - $callable = fn (ContextKey $contextKey) => $storage->current()->get($contextKey); - $scopeBoundCallable = ScopeBoundCallable::wrap($callable, $storage); - - $scope->detach(); - - $this->assertNull($callable($contextKey)); - $this->assertSame('value', $scopeBoundCallable($contextKey)); - $this->assertNull($callable($contextKey)); - } -} diff --git a/tests/Unit/Context/ScopeBound/ScopeBoundPromiseTest.php b/tests/Unit/Context/ScopeBound/ScopeBoundPromiseTest.php deleted file mode 100644 index 9506d8b77..000000000 --- a/tests/Unit/Context/ScopeBound/ScopeBoundPromiseTest.php +++ /dev/null @@ -1,87 +0,0 @@ -then(fn ($value) => $value) - ->then(fn ($value) => $this->assertSame(5, $value)) - ; - } - - public function test_promise_then_propagates_context_in_promise() - { - $storage = new ContextStorage(); - - $contextKey = new ContextKey(); - $promise = self::getFulfilledPromise(); - - ScopeBoundPromise::wrap($promise, $storage) - ->then(fn () => $storage->attach($storage->current()->with($contextKey, 'value'))) - ->then(fn () => $this->assertSame('value', $storage->current()->get($contextKey))) - ; - } - - public function test_promise_then_preserves_scope_outside_of_promise() - { - $storage = new ContextStorage(); - - $contextKey = new ContextKey(); - $promise = self::getFulfilledPromise(); - - ScopeBoundPromise::wrap($promise, $storage) - ->then(fn () => $storage->attach($storage->current()->with($contextKey, 'value'))) - ; - - $this->assertNull($storage->current()->get($contextKey)); - } - - public function test_promise_invalid() - { - $this->expectException(InvalidArgumentException::class); - - ScopeBoundPromise::wrap(new stdClass()); - } - - private static function getFulfilledPromise($value = null): object - { - return new class($value) { - private $value; - - /** - * @param mixed $value - */ - public function __construct($value) - { - $this->value = $value; - } - - public function then(?callable $onFulfilled = null): self - { - return $onFulfilled - ? new self($onFulfilled($this->value)) - : $this; - } - }; - } -}