Skip to content

Commit 35a1ab4

Browse files
patel-vanshpaulbalandanmichalsn
authored
feat: Add Global Context feature (#9970)
* Added Context class * Added some convenient methods * Enhance logger to properly include global context * Add unit tests for Context class functionality * Added tests for global context logging behaviour * cs-fix * Add docs * add context() helper function * cs-fix and fixing static analysis problems * Some doc fixing * Fix rst errors. * Apply suggested changes * Set $logGlobalContext to false by default * Add docs for helper method * cs-fix * Fix the doc issue * Applying code suggestions Co-authored-by: John Paul E. Balandan, CPA <paulbalandan@gmail.com> Co-authored-by: michalsn <michal@sniatala.pl> * changed type from mixed|null to mixed * Added __serialize and __unserialize methods. * Returning $default if $key is not present. And only calling ArrayHelper if needed. * Remove contructor Co-authored-by: Michal Sniatala <michal@sniatala.pl> --------- Co-authored-by: John Paul E. Balandan, CPA <paulbalandan@gmail.com> Co-authored-by: michalsn <michal@sniatala.pl>
1 parent f048e06 commit 35a1ab4

File tree

35 files changed

+1902
-0
lines changed

35 files changed

+1902
-0
lines changed

app/Config/Logger.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,22 @@ class Logger extends BaseConfig
5151
*/
5252
public string $dateFormat = 'Y-m-d H:i:s';
5353

54+
/**
55+
* --------------------------------------------------------------------------
56+
* Whether to log the global context
57+
* --------------------------------------------------------------------------
58+
*
59+
* You can enable/disable logging of global context data, which comes from the
60+
* `CodeIgniter\Context\Context` class. This data is automatically included in
61+
* logs, and can be set using the `set()` method of the Context class. This is
62+
* useful for including additional information in your logs, such as user IDs,
63+
* request IDs, etc.
64+
*
65+
* **NOTE:** This **DOES NOT** include any data that has been marked as hidden
66+
* using the `setHidden()` method of the Context class.
67+
*/
68+
public bool $logGlobalContext = false;
69+
5470
/**
5571
* --------------------------------------------------------------------------
5672
* Log Handlers

system/Common.php

Lines changed: 12 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,17 @@ 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+
function context(): Context
222+
{
223+
return service('context');
224+
}
225+
}
226+
215227
if (! function_exists('cookie')) {
216228
/**
217229
* Simpler way to create a new Cookie instance.

system/Config/Services.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use CodeIgniter\Cache\ResponseCache;
1919
use CodeIgniter\CLI\Commands;
2020
use CodeIgniter\CodeIgniter;
21+
use CodeIgniter\Context\Context;
2122
use CodeIgniter\Database\ConnectionInterface;
2223
use CodeIgniter\Database\MigrationRunner;
2324
use CodeIgniter\Debug\Exceptions;
@@ -868,4 +869,16 @@ public static function typography(bool $getShared = true)
868869

869870
return new Typography();
870871
}
872+
873+
/**
874+
* The Context class provides a way to store and retrieve static data throughout requests.
875+
*/
876+
public static function context(bool $getShared = true): Context
877+
{
878+
if ($getShared) {
879+
return static::getSharedInstance('context');
880+
}
881+
882+
return new Context();
883+
}
871884
}

0 commit comments

Comments
 (0)