Skip to content

Commit b055119

Browse files
committed
add context() helper function
1 parent da3a843 commit b055119

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

system/Common.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use CodeIgniter\Cache\CacheInterface;
1515
use CodeIgniter\Config\BaseConfig;
1616
use CodeIgniter\Config\Factories;
17+
use CodeIgniter\Context\Context;
1718
use CodeIgniter\Cookie\Cookie;
1819
use CodeIgniter\Cookie\CookieStore;
1920
use CodeIgniter\Cookie\Exceptions\CookieException;
@@ -212,6 +213,19 @@ function config(string $name, bool $getShared = true)
212213
}
213214
}
214215

216+
if (! function_exists('context')) {
217+
/**
218+
* Provides access to the Context object, which is used to store
219+
* contextual data during a request that can be accessed globally.
220+
*
221+
* @return Context
222+
*/
223+
function context(): Context
224+
{
225+
return service('context');
226+
}
227+
}
228+
215229
if (! function_exists('cookie')) {
216230
/**
217231
* Simpler way to create a new Cookie instance.

tests/system/CommonFunctionsTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,4 +821,11 @@ public function testRenderBacktrace(): void
821821
$this->assertMatchesRegularExpression('/^\s*\d* .+(?:\(\d+\))?: \S+(?:(?:\->|::)\S+)?\(.*\)$/', $render);
822822
}
823823
}
824+
825+
public function testContext(): void
826+
{
827+
service('context')->set('foo', 'bar');
828+
829+
$this->assertSame('bar', context()->get('foo'));
830+
}
824831
}

0 commit comments

Comments
 (0)