-
Notifications
You must be signed in to change notification settings - Fork 2k
feat: Add global Context feature #9970
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
Changes from 12 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
f6a9530
Added Context class
patel-vansh 30cdc19
Added some convenient methods
patel-vansh a166678
Enhance logger to properly include global context
patel-vansh 43c2d52
Add unit tests for Context class functionality
patel-vansh b0d5848
Added tests for global context logging behaviour
patel-vansh f62d577
cs-fix
patel-vansh 2e4ca31
Add docs
patel-vansh da3a843
Merge remote-tracking branch 'upstream/4.8' into feat/context
patel-vansh b055119
add context() helper function
patel-vansh c60b9f9
cs-fix and fixing static analysis problems
patel-vansh 66fd4ae
Some doc fixing
patel-vansh daf6d6b
Fix rst errors.
patel-vansh 1136fa0
Apply suggested changes
patel-vansh 07f895e
Set $logGlobalContext to false by default
patel-vansh 7bf481a
Add docs for helper method
patel-vansh 5a6fbca
cs-fix
patel-vansh 2f11d7c
Fix the doc issue
patel-vansh bf7ed1b
Merge remote-tracking branch 'upstream/4.8' into feat/context
patel-vansh f36449e
Applying code suggestions
patel-vansh 4bd55ed
changed type from mixed|null to mixed
patel-vansh f066488
Added __serialize and __unserialize methods.
patel-vansh 9f77ab8
Returning $default if $key is not present. And only calling ArrayHelp…
patel-vansh dc8ca2a
Remove contructor
patel-vansh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,321 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * This file is part of CodeIgniter 4 framework. | ||
| * | ||
| * (c) CodeIgniter Foundation <admin@codeigniter.com> | ||
| * | ||
| * For the full copyright and license information, please view | ||
| * the LICENSE file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace CodeIgniter\Context; | ||
|
|
||
| class Context | ||
paulbalandan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| /** | ||
| * The data stored in the context. | ||
| * | ||
| * @var array<string, mixed> | ||
| */ | ||
| protected array $data; | ||
|
|
||
| /** | ||
| * The data that is stored, but not included in logs. | ||
patel-vansh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * | ||
| * @var array<string, mixed> | ||
| */ | ||
| private array $hiddenData; | ||
|
|
||
| /** | ||
| * Constructor | ||
| */ | ||
patel-vansh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| public function __construct() | ||
| { | ||
| $this->data = []; | ||
| $this->hiddenData = []; | ||
| } | ||
|
|
||
| /** | ||
| * Set a key-value pair to the context. | ||
| * | ||
| * @param array<string, mixed>|string $key The key to identify the data. Can be a string or an array of key-value pairs. | ||
| * @param mixed $value The value to be stored in the context. | ||
| * | ||
| * @return $this | ||
| */ | ||
| public function set(array|string $key, mixed $value = null): self | ||
paulbalandan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| if (is_array($key)) { | ||
| $this->data = array_merge($this->data, $key); | ||
paulbalandan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| $this->data[$key] = $value; | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Set a hidden key-value pair to the context. This data will not be included in logs. | ||
| * | ||
| * @param array<string, mixed>|string $key The key to identify the data. Can be a string or an array of key-value pairs. | ||
| * @param mixed $value The value to be stored in the context. | ||
| * | ||
| * @return $this | ||
| */ | ||
| public function setHidden(array|string $key, mixed $value = null): self | ||
patel-vansh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| if (is_array($key)) { | ||
| $this->hiddenData = array_merge($this->hiddenData, $key); | ||
paulbalandan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| $this->hiddenData[$key] = $value; | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Get a value from the context by its key, or return a default value if the key does not exist. | ||
| * | ||
| * @param string $key The key to identify the data. | ||
| * @param mixed|null $default The default value to return if the key does not exist in the context. | ||
paulbalandan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * | ||
| * @return mixed The value associated with the key, or the default value if the key does not exist. | ||
| */ | ||
| public function get(string $key, mixed $default = null): mixed | ||
| { | ||
| return $this->data[$key] ?? $default; | ||
| } | ||
patel-vansh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Get only the specified keys from the context. If a key does not exist, it will be ignored. | ||
| * | ||
| * @param list<string>|string $keys An array of keys to retrieve from the context. | ||
| * | ||
| * @return array<string, mixed> An array of key-value pairs for the specified keys that exist in the context. | ||
| */ | ||
| public function getOnly(array|string $keys): array | ||
| { | ||
| if (is_string($keys)) { | ||
| $keys = [$keys]; | ||
| } | ||
|
|
||
| return array_filter($this->data, static fn ($k): bool => in_array($k, $keys, true), ARRAY_FILTER_USE_KEY); | ||
| } | ||
michalsn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Get all keys from the context except the specified keys. | ||
| * | ||
| * @param list<string>|string $keys An array of keys to exclude from the context. | ||
| * | ||
| * @return array<string, mixed> An array of key-value pairs for all keys in the context except the specified keys. | ||
| */ | ||
| public function getExcept(array|string $keys): array | ||
| { | ||
| if (is_string($keys)) { | ||
| $keys = [$keys]; | ||
| } | ||
|
|
||
| return array_filter($this->data, static fn ($k): bool => ! in_array($k, $keys, true), ARRAY_FILTER_USE_KEY); | ||
| } | ||
michalsn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Get all data from the context | ||
| * | ||
| * @return array<string, mixed> An array of all key-value pairs in the context. | ||
| */ | ||
| public function getAll(): array | ||
| { | ||
| return $this->data; | ||
| } | ||
|
|
||
| /** | ||
| * Get a hidden value from the context by its key, or return a default value if the key does not exist. | ||
| * | ||
| * @param string $key The key to identify the data. | ||
| * @param mixed|null $default The default value to return if the key does not exist in the context. | ||
| * | ||
| * @return mixed The value associated with the key, or the default value if the key does not exist. | ||
| */ | ||
| public function getHidden(string $key, mixed $default = null): mixed | ||
| { | ||
| return $this->hiddenData[$key] ?? $default; | ||
| } | ||
|
|
||
| /** | ||
| * Get only the specified keys from the hidden context. If a key does not exist, it will be ignored. | ||
| * | ||
| * @param list<string>|string $keys An array of keys to retrieve from the hidden context. | ||
| * | ||
| * @return array<string, mixed> An array of key-value pairs for the specified keys that exist in the hidden context. | ||
| */ | ||
| public function getOnlyHidden(array|string $keys): array | ||
| { | ||
| if (is_string($keys)) { | ||
| $keys = [$keys]; | ||
| } | ||
|
|
||
| return array_filter($this->hiddenData, static fn ($k): bool => in_array($k, $keys, true), ARRAY_FILTER_USE_KEY); | ||
| } | ||
|
|
||
| /** | ||
| * Get all keys from the hidden context except the specified keys. | ||
| * | ||
| * @param list<string>|string $keys An array of keys to exclude from the hidden context. | ||
| * | ||
| * @return array<string, mixed> An array of key-value pairs for all keys in the hidden context except the specified keys. | ||
| */ | ||
| public function getExceptHidden(array|string $keys): array | ||
| { | ||
| if (is_string($keys)) { | ||
| $keys = [$keys]; | ||
| } | ||
|
|
||
| return array_filter($this->hiddenData, static fn ($k): bool => ! in_array($k, $keys, true), ARRAY_FILTER_USE_KEY); | ||
| } | ||
|
|
||
| /** | ||
| * Get all hidden data from the context | ||
| * | ||
| * @return array<string, mixed> An array of all key-value pairs in the hidden context. | ||
| */ | ||
| public function getAllHidden(): array | ||
| { | ||
| return $this->hiddenData; | ||
| } | ||
|
|
||
| /** | ||
| * Check if a key does not exist in the context. Exactly the opposite of `has()`. | ||
| * | ||
| * @param string $key The key to check for non-existence in the context. | ||
| * | ||
| * @return bool True if the key does not exist in the context, false otherwise. | ||
| */ | ||
| public function missing(string $key): bool | ||
| { | ||
| return ! $this->has($key); | ||
| } | ||
patel-vansh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Check if a key exists in the context. | ||
| * | ||
| * @param string $key The key to check for existence in the context. | ||
| * | ||
| * @return bool True if the key exists in the context, false otherwise. | ||
| */ | ||
| public function has(string $key): bool | ||
| { | ||
| return array_key_exists($key, $this->data); | ||
| } | ||
|
|
||
| /** | ||
| * Check if a key does not exist in the hidden context. Exactly the opposite of `hasHidden()`. | ||
| * | ||
| * @param string $key The key to check for non-existence in the hidden context. | ||
| * | ||
| * @return bool True if the key does not exist in the hidden context, false otherwise. | ||
| */ | ||
| public function missingHidden(string $key): bool | ||
| { | ||
| return ! $this->hasHidden($key); | ||
| } | ||
|
|
||
| /** | ||
| * Check if a key exists in the hidden context. | ||
| * | ||
| * @param string $key The key to check for existence in the hidden context. | ||
| * | ||
| * @return bool True if the key exists in the hidden context, false otherwise. | ||
| */ | ||
| public function hasHidden(string $key): bool | ||
| { | ||
| return array_key_exists($key, $this->hiddenData); | ||
| } | ||
|
|
||
| /** | ||
| * Remove a key-value pair from the context by its key. | ||
| * | ||
| * @param list<string>|string $key The key to identify the data to be removed from the context. | ||
| * | ||
| * @return $this | ||
| */ | ||
| public function remove(array|string $key): self | ||
| { | ||
| if (is_array($key)) { | ||
| foreach ($key as $k) { | ||
| unset($this->data[$k]); | ||
| } | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| unset($this->data[$key]); | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Remove a key-value pair from the hidden context by its key. | ||
| * | ||
| * @param list<string>|string $key The key to identify the data to be removed from the hidden context. | ||
| * | ||
| * @return $this | ||
| */ | ||
| public function removeHidden(array|string $key): self | ||
| { | ||
| if (is_array($key)) { | ||
| foreach ($key as $k) { | ||
| unset($this->hiddenData[$k]); | ||
| } | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| unset($this->hiddenData[$key]); | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Clear all data from the context, including hidden data. | ||
| * | ||
| * @return $this | ||
| */ | ||
| public function clearAll(): self | ||
| { | ||
| $this->clear(); | ||
| $this->clearHidden(); | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Clear all data from the context. | ||
| * | ||
| * @return $this | ||
| */ | ||
| public function clear(): self | ||
| { | ||
| $this->data = []; | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Clear all hidden data from the context. | ||
| * | ||
| * @return $this | ||
| */ | ||
| public function clearHidden(): self | ||
| { | ||
| $this->hiddenData = []; | ||
|
|
||
| return $this; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.