Skip to content

Commit

Permalink
Merge pull request #296 from jolicode/deprecations
Browse files Browse the repository at this point in the history
Avoid internal deprecation when calling with() without a context
  • Loading branch information
lyrixx authored Feb 26, 2024
2 parents bc667be + 3c8c517 commit c6c9008
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/ContextRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ public function get(?string $name = null): Context
return $context;
}

public function hasCurrentContext(): bool
{
return isset($this->currentContext);
}

public function setCurrentContext(Context $context): void
{
$this->currentContext = $context;
Expand Down
14 changes: 10 additions & 4 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -808,11 +808,15 @@ function with(
Context|string|null $context = null,
): mixed {
$contextRegistry = GlobalHelper::getContextRegistry();
$initialContext = $contextRegistry->getCurrentContext();
$context ??= $initialContext;

$initialContext = null;
if ($contextRegistry->hasCurrentContext()) {
$initialContext = $contextRegistry->getCurrentContext();
}

$context ??= new Context();
if (\is_string($context)) {
$context = context($context);
$context = $contextRegistry->get($context);
}

if (null !== $data) {
Expand Down Expand Up @@ -856,7 +860,9 @@ function with(
try {
return $callback($context);
} finally {
$contextRegistry->setCurrentContext($initialContext);
if ($initialContext) {
$contextRegistry->setCurrentContext($initialContext);
}
}
}

Expand Down

0 comments on commit c6c9008

Please sign in to comment.