Skip to content

Add context helper method similar to laravel. #130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@
"src/session/src/Functions.php",
"src/support/src/Functions.php",
"src/support/src/helpers.php",
"src/translation/src/Functions.php"
"src/translation/src/Functions.php",
"src/core/src/helpers.php"
]
},
"autoload-dev": {
Expand Down
5 changes: 4 additions & 1 deletion src/core/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
"autoload": {
"psr-4": {
"Hypervel\\": "src/"
}
},
"files": [
"src/helpers.php"
]
},
"require": {
"php": "^8.2",
Expand Down
23 changes: 22 additions & 1 deletion src/core/src/Context/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,24 @@ class Context extends HyperfContext
{
protected const DEPTH_KEY = 'di.depth';

public function __call(string $method, array $arguments): mixed
{
return static::{$method}(...$arguments);
}

/**
* Set multiple key-value pairs in the context.
*/
public static function setMany(array $values, ?int $coroutineId = null): void
{
foreach ($values as $key => $value) {
static::set($key, $value, $coroutineId);
}
}

/**
* Copy context data from non-coroutine context to the specified coroutine context.
*/
public static function copyFromNonCoroutine(array $keys = [], ?int $coroutineId = null): void
{
if (is_null($context = Coroutine::getContextFor($coroutineId))) {
Expand All @@ -26,6 +44,9 @@ public static function copyFromNonCoroutine(array $keys = [], ?int $coroutineId
$context->exchangeArray($map);
}

/**
* Destroy all context data for the specified coroutine, preserving only the depth key.
*/
public static function destroyAll(?int $coroutineId = null): void
{
$coroutineId = $coroutineId ?: Coroutine::id();
Expand All @@ -41,7 +62,7 @@ public static function destroyAll(?int $coroutineId = null): void
}

$contextKeys = [];
foreach ($context as $key => $_value) {
foreach ($context as $key => $_) {
if ($key === static::DEPTH_KEY) {
continue;
}
Expand Down
20 changes: 20 additions & 0 deletions src/core/src/helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

use Hypervel\Context\ApplicationContext;
use Hypervel\Context\Context;

if (! function_exists('context')) {
/**
* Get / set the specified context value in the current coroutine.
*/
function context(null|array|string $key = null, mixed $default = null, ?int $coroutineId = null): mixed
{
return match (true) {
is_null($key) => ApplicationContext::getContainer()->get(Context::class),
is_array($key) => Context::setMany($key, $coroutineId),
default => Context::get($key, $default, $coroutineId),
};
}
}
81 changes: 81 additions & 0 deletions tests/Core/ContextTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

declare(strict_types=1);

namespace Hypervel\Tests\Core;

use Hypervel\Context\Context;
use Hypervel\Coroutine\Coroutine;
use PHPUnit\Framework\TestCase;

use function Hypervel\Coroutine\run;

/**
* @internal
* @coversNothing
*/
class ContextTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();

Context::destroyAll();
}

protected function tearDown(): void
{
Context::destroyAll();
parent::tearDown();
}

public function testSetMany(): void
{
$values = [
'key1' => 'value1',
'key2' => 'value2',
'key3' => 'value3',
];

Context::setMany($values);

foreach ($values as $key => $expectedValue) {
$this->assertTrue(Context::has($key));
$this->assertEquals($expectedValue, Context::get($key));
}
}

/**
* @covers ::copyFromNonCoroutine
*/
public function testCopyFromNonCoroutineWithSpecificKeys(): void
{
Context::set('foo', 'foo');
Context::set('bar', 'bar');

run(function () {
Coroutine::create(function () {
Context::copyFromNonCoroutine();
$this->assertSame('foo', Context::get('foo'));
$this->assertSame('bar', Context::get('bar'));
});
});
}

/**
* @covers ::destroyAll
*/
public function testDestroyAll(): void
{
Context::set('key1', 'value1');
Context::set('key2', 'value2');

$this->assertTrue(Context::has('key1'));
$this->assertTrue(Context::has('key2'));

Context::destroyAll();

$this->assertFalse(Context::has('key1'));
$this->assertFalse(Context::has('key2'));
}
}