-
Notifications
You must be signed in to change notification settings - Fork 2k
feat: add optional merge directives for registrars #10329
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
Open
michalsn
wants to merge
3
commits into
codeigniter4:4.8
Choose a base branch
from
michalsn:feat/registrars-merge-directives
base: 4.8
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| <?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\Config; | ||
|
|
||
| use CodeIgniter\Exceptions\InvalidArgumentException; | ||
|
|
||
| /** | ||
| * Describes how a Registrar value should be merged into an existing | ||
| * Config property. Interpreted when returned as the value of a config | ||
| * property; nested directives are honored inside Merge::byKey(). | ||
| * | ||
| * @see \CodeIgniter\Config\BaseConfig | ||
| */ | ||
| final readonly class Merge | ||
| { | ||
| /** | ||
| * Discard the existing value and use the new one. | ||
| */ | ||
| public const REPLACE = 'replace'; | ||
|
|
||
| /** | ||
| * Add absent list items to the end of the existing value. | ||
| */ | ||
| public const APPEND = 'append'; | ||
|
|
||
| /** | ||
| * Add absent list items to the front of the existing value. | ||
| */ | ||
| public const PREPEND = 'prepend'; | ||
|
|
||
| /** | ||
| * Insert list items immediately before the anchor element. | ||
| */ | ||
| public const BEFORE = 'before'; | ||
|
|
||
| /** | ||
| * Insert list items immediately after the anchor element. | ||
| */ | ||
| public const AFTER = 'after'; | ||
|
|
||
| /** | ||
| * Deep-merge by key: string keys recurse, integer keys append, scalars replace. | ||
| */ | ||
| public const BY_KEY = 'byKey'; | ||
|
|
||
| /** | ||
| * @param mixed $value Any value for REPLACE; array for the list strategies and BY_KEY. | ||
| * @param mixed $anchor The element BEFORE/AFTER position against (matched strictly). | ||
| */ | ||
| private function __construct( | ||
| public string $strategy, | ||
| public mixed $value, | ||
| public mixed $anchor = null, | ||
| ) { | ||
| } | ||
|
|
||
| /** | ||
| * Replace the existing value entirely (terminal: the payload is used | ||
| * verbatim). Accepts any type, so it works for scalars too: | ||
| * Merge::replace(false), Merge::replace('driver'), Merge::replace(null), | ||
| * or arrays (e.g. ['a','b'] + ['c'] => ['c']). | ||
| */ | ||
| public static function replace(mixed $value): self | ||
| { | ||
| return new self(self::REPLACE, $value); | ||
| } | ||
|
|
||
| /** | ||
| * Append absent list items to the end of the existing value | ||
| * (e.g. ['a','b'] + ['b','c'] => ['a','b','c']). Values already present are | ||
| * left where they are. The payload is literal - for nested control, use | ||
| * byKey() rather than nesting directives in an append() payload. List keys | ||
| * are not preserved: the value is treated as a list. | ||
| * | ||
| * @param list<mixed> $value | ||
| */ | ||
| public static function append(array $value): self | ||
| { | ||
| return new self(self::APPEND, $value); | ||
| } | ||
|
|
||
| /** | ||
| * Prepend absent list items to the front of the existing value | ||
| * (e.g. ['a','b'] + ['c'] => ['c','a','b']). Values already present are left | ||
| * where they are. List keys are not preserved: the value is treated as a list. | ||
| * | ||
| * @param list<mixed> $value | ||
| */ | ||
| public static function prepend(array $value): self | ||
| { | ||
| return new self(self::PREPEND, $value); | ||
| } | ||
|
|
||
| /** | ||
| * Insert list items immediately before the first element equal (===) to | ||
| * $anchor. An already-present value is moved to this position. If the anchor | ||
| * is not in the list this falls back to prepend() and does not relocate | ||
| * already-present values. List keys are not preserved. | ||
| * | ||
| * @param list<mixed> $value | ||
| * | ||
| * @throws InvalidArgumentException if $anchor is also one of the inserted values. | ||
| */ | ||
| public static function before(mixed $anchor, array $value): self | ||
| { | ||
| self::assertAnchorNotInPayload($anchor, $value, self::BEFORE); | ||
|
|
||
| return new self(self::BEFORE, $value, $anchor); | ||
| } | ||
|
|
||
| /** | ||
| * Insert list items immediately after the first element equal (===) to | ||
| * $anchor. An already-present value is moved to this position. If the anchor | ||
| * is not in the list this falls back to append() and does not relocate | ||
| * already-present values. List keys are not preserved. | ||
| * | ||
| * @param list<mixed> $value | ||
| * | ||
| * @throws InvalidArgumentException if $anchor is also one of the inserted values. | ||
| */ | ||
| public static function after(mixed $anchor, array $value): self | ||
| { | ||
| self::assertAnchorNotInPayload($anchor, $value, self::AFTER); | ||
|
|
||
| return new self(self::AFTER, $value, $anchor); | ||
| } | ||
|
|
||
| /** | ||
| * Guards against anchoring a before()/after() insert on a value that is also | ||
| * being inserted. That request is contradictory - the anchor would be removed | ||
| * by de-duplication before it could be located - so it is rejected outright. | ||
| * | ||
| * @param list<mixed> $value | ||
| */ | ||
| private static function assertAnchorNotInPayload(mixed $anchor, array $value, string $strategy): void | ||
| { | ||
| if (in_array($anchor, $value, true)) { | ||
| throw new InvalidArgumentException( | ||
| 'Merge::' . $strategy . '() cannot use a value that is also being inserted as its anchor.', | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Deep-merge into the existing value by key: associative (string) keys are | ||
| * merged/recursed, list (integer) keys append, scalar leaves are replaced. | ||
| * Nested Merge directives ARE honored within the payload. Named byKey() to | ||
| * distance it from PHP's array_merge_recursive(), which collects scalars | ||
| * into arrays. | ||
| * | ||
| * @param array<array-key, mixed> $value | ||
| */ | ||
| public static function byKey(array $value): self | ||
| { | ||
| return new self(self::BY_KEY, $value); | ||
| } | ||
| } |
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,37 @@ | ||
| <?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 Tests\Support\Config; | ||
|
|
||
| use CodeIgniter\Config\Merge; | ||
|
|
||
| /** | ||
| * Registrar exercising the ordering directives (prepend/before/after) through | ||
| * the real registrar flow, including nesting inside byKey() for a Filters-style | ||
| * globals list. | ||
| */ | ||
| class MergeOrderRegistrar | ||
| { | ||
| public static function MergeRegistrarConfig(): array | ||
| { | ||
| return [ | ||
| // Order a filter relative to an existing one in a nested list. | ||
| 'globals' => Merge::byKey([ | ||
| 'before' => Merge::after('csrf', ['auth']), | ||
| 'after' => Merge::prepend(['honeypot']), | ||
| ]), | ||
| // Property-root list ordering. | ||
| 'list' => Merge::before('a', ['z']), | ||
| ]; | ||
| } | ||
| } |
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,30 @@ | ||
| <?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 Tests\Support\Config; | ||
|
|
||
| /** | ||
| * Plain-array registrar (no directives) used to assert the legacy shallow | ||
| * merge behavior is unchanged — nested siblings are dropped. | ||
| */ | ||
| class MergePlainRegistrar | ||
| { | ||
| public static function MergeRegistrarConfig(): array | ||
| { | ||
| return [ | ||
| 'arrayNested' => [ | ||
| 'key2' => ['val4' => 'subVal4'], | ||
| ], | ||
| ]; | ||
| } | ||
| } |
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.