Skip to content

Commit 1136fa0

Browse files
committed
Apply suggested changes
1 parent daf6d6b commit 1136fa0

19 files changed

Lines changed: 71 additions & 123 deletions

File tree

app/Config/Logger.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class Logger extends BaseConfig
6565
* **NOTE:** This **DOES NOT** include any data that has been marked as hidden
6666
* using the `setHidden()` method of the Context class.
6767
*/
68-
public bool $logGlobalContext = false;
68+
public bool $logGlobalContext = true;
6969

7070
/**
7171
* --------------------------------------------------------------------------

system/Context/Context.php

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
namespace CodeIgniter\Context;
1515

16+
use CodeIgniter\Helpers\Array\ArrayHelper;
17+
1618
class Context
1719
{
1820
/**
@@ -82,6 +84,7 @@ public function setHidden(array|string $key, mixed $value = null): self
8284

8385
/**
8486
* Get a value from the context by its key, or return a default value if the key does not exist.
87+
* Supports dot notation for nested arrays (e.g., 'user.profile.name' to access $data['user']['profile']['name']).
8588
*
8689
* @param string $key The key to identify the data.
8790
* @param mixed|null $default The default value to return if the key does not exist in the context.
@@ -90,7 +93,7 @@ public function setHidden(array|string $key, mixed $value = null): self
9093
*/
9194
public function get(string $key, mixed $default = null): mixed
9295
{
93-
return $this->data[$key] ?? $default;
96+
return ArrayHelper::dotSearch($key, $this->data) ?? $default;
9497
}
9598

9699
/**
@@ -145,7 +148,7 @@ public function getAll(): array
145148
*/
146149
public function getHidden(string $key, mixed $default = null): mixed
147150
{
148-
return $this->hiddenData[$key] ?? $default;
151+
return ArrayHelper::dotSearch($key, $this->hiddenData) ?? $default;
149152
}
150153

151154
/**
@@ -190,18 +193,6 @@ public function getAllHidden(): array
190193
return $this->hiddenData;
191194
}
192195

193-
/**
194-
* Check if a key does not exist in the context. Exactly the opposite of `has()`.
195-
*
196-
* @param string $key The key to check for non-existence in the context.
197-
*
198-
* @return bool True if the key does not exist in the context, false otherwise.
199-
*/
200-
public function missing(string $key): bool
201-
{
202-
return ! $this->has($key);
203-
}
204-
205196
/**
206197
* Check if a key exists in the context.
207198
*
@@ -211,19 +202,7 @@ public function missing(string $key): bool
211202
*/
212203
public function has(string $key): bool
213204
{
214-
return array_key_exists($key, $this->data);
215-
}
216-
217-
/**
218-
* Check if a key does not exist in the hidden context. Exactly the opposite of `hasHidden()`.
219-
*
220-
* @param string $key The key to check for non-existence in the hidden context.
221-
*
222-
* @return bool True if the key does not exist in the hidden context, false otherwise.
223-
*/
224-
public function missingHidden(string $key): bool
225-
{
226-
return ! $this->hasHidden($key);
205+
return ArrayHelper::dotKeyExists($key, $this->data);
227206
}
228207

229208
/**
@@ -235,7 +214,7 @@ public function missingHidden(string $key): bool
235214
*/
236215
public function hasHidden(string $key): bool
237216
{
238-
return array_key_exists($key, $this->hiddenData);
217+
return ArrayHelper::dotKeyExists($key, $this->hiddenData);
239218
}
240219

241220
/**

system/Log/Logger.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ class Logger implements LoggerInterface
121121
*
122122
* @var bool
123123
*/
124-
protected $logGlobalContext;
124+
protected bool $logGlobalContext = true;
125125

126126
/**
127127
* Constructor.
@@ -164,7 +164,7 @@ public function __construct($config, bool $debug = CI_DEBUG)
164164
$this->logCache = [];
165165
}
166166

167-
$this->logGlobalContext = $config->logGlobalContext;
167+
$this->logGlobalContext = $config->logGlobalContext ?? $this->logGlobalContext;
168168
}
169169

170170
/**
@@ -265,7 +265,7 @@ public function log($level, string|Stringable $message, array $context = []): vo
265265

266266
if ($this->logGlobalContext) {
267267
$globalContext = service('context')->getAll();
268-
if (is_array($globalContext) && $globalContext !== []) {
268+
if ($globalContext !== []) {
269269
$message .= ' ' . json_encode($globalContext);
270270
}
271271
}

tests/system/Context/ContextTest.php

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -328,15 +328,6 @@ public function testHasKey(): void
328328
$this->assertTrue($context->has('user_id'));
329329
}
330330

331-
public function testMissingKey(): void
332-
{
333-
$context = service('context');
334-
$this->assertTrue($context->missing('user_id'));
335-
336-
$context->set('user_id', 123);
337-
$this->assertFalse($context->missing('user_id'));
338-
}
339-
340331
public function testHasHiddenKey(): void
341332
{
342333
$context = service('context');
@@ -346,15 +337,6 @@ public function testHasHiddenKey(): void
346337
$this->assertTrue($context->hasHidden('api_key'));
347338
}
348339

349-
public function testMissingHiddenKey(): void
350-
{
351-
$context = service('context');
352-
$this->assertTrue($context->missingHidden('api_key'));
353-
354-
$context->setHidden('api_key', 'secret');
355-
$this->assertFalse($context->missingHidden('api_key'));
356-
}
357-
358340
public function testRemoveSingleValue(): void
359341
{
360342
$context = service('context');

user_guide_src/source/general/context.rst

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,6 @@ You can check if a key exists in the context:
9494

9595
.. literalinclude:: context/010.php
9696

97-
Or check if a key is missing (the opposite of ``has()``):
98-
99-
.. literalinclude:: context/011.php
100-
10197
*********************
10298
Removing Context Data
10399
*********************
@@ -107,21 +103,21 @@ Removing a Single Value
107103

108104
You can remove data from the context using the ``remove()`` method:
109105

110-
.. literalinclude:: context/012.php
106+
.. literalinclude:: context/011.php
111107

112108
Removing Multiple Values
113109
=========================
114110

115111
To remove multiple keys at once, pass an array:
116112

117-
.. literalinclude:: context/013.php
113+
.. literalinclude:: context/012.php
118114

119115
Clearing All Data
120116
=================
121117

122118
To remove all context data:
123119

124-
.. literalinclude:: context/014.php
120+
.. literalinclude:: context/013.php
125121

126122
*********************
127123
Hidden Context Data
@@ -136,47 +132,47 @@ Setting Hidden Data
136132

137133
Use the ``setHidden()`` method to store sensitive data:
138134

139-
.. literalinclude:: context/015.php
135+
.. literalinclude:: context/014.php
140136

141137
You can also set multiple hidden values at once:
142138

143-
.. literalinclude:: context/016.php
139+
.. literalinclude:: context/015.php
144140

145141
Getting Hidden Data
146142
===================
147143

148144
Retrieve hidden data using ``getHidden()``:
149145

150-
.. literalinclude:: context/017.php
146+
.. literalinclude:: context/016.php
151147

152148
The same methods available for regular data also work with hidden data:
153149

154-
.. literalinclude:: context/018.php
150+
.. literalinclude:: context/017.php
155151

156152
Checking Hidden Data
157153
====================
158154

159155
Check if a hidden key exists:
160156

161-
.. literalinclude:: context/019.php
157+
.. literalinclude:: context/018.php
162158

163159
Removing Hidden Data
164160
====================
165161

166162
Remove hidden data using ``removeHidden()``:
167163

168-
.. literalinclude:: context/020.php
164+
.. literalinclude:: context/019.php
169165

170166
Clearing Hidden Data
171167
====================
172168

173169
To clear all hidden data without affecting regular context data:
174170

175-
.. literalinclude:: context/021.php
171+
.. literalinclude:: context/020.php
176172

177173
To clear both regular and hidden data:
178174

179-
.. literalinclude:: context/022.php
175+
.. literalinclude:: context/021.php
180176

181177
.. important:: Regular data and hidden data are stored separately. A key can exist in both regular and hidden storage with different values. Use ``get()`` for regular data and ``getHidden()`` for hidden data.
182178

@@ -193,11 +189,11 @@ Enabling Global Context Logging
193189
To enable automatic logging of context data, set the ``$logGlobalContext`` property to ``true`` in your
194190
**app/Config/Logger.php** file:
195191

196-
.. literalinclude:: context/023.php
192+
.. literalinclude:: context/022.php
197193

198194
When enabled, all context data (excluding hidden data) will be automatically appended to your log messages as JSON:
199195

200-
.. literalinclude:: context/024.php
196+
.. literalinclude:: context/023.php
201197

202198
This would produce a log entry like:
203199

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
<?php
22

3-
if ($context->missing('user_id')) {
4-
// user_id hasn't been set yet
5-
}
3+
$context->remove('user_id');
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<?php
22

3-
$context->remove('user_id');
3+
$context->remove(['user_id', 'username', 'request_id']);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<?php
22

3-
$context->remove(['user_id', 'username', 'request_id']);
3+
$context->clear();
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<?php
22

3-
$context->clear();
3+
$context->setHidden('api_key', 'sk_live_abc123xyz789');
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
<?php
22

3-
$context->setHidden('api_key', 'sk_live_abc123xyz789');
3+
$context->setHidden([
4+
'api_key' => 'sk_live_abc123xyz789',
5+
'api_secret' => 'secret_key_here',
6+
'db_password' => 'database_password',
7+
]);

0 commit comments

Comments
 (0)