Skip to content

Commit 6ddfca0

Browse files
authored
Merge pull request #15 from infocyph/feature/improvement-25
unify
2 parents 075513c + 9c6ef30 commit 6ddfca0

File tree

11 files changed

+199
-197
lines changed

11 files changed

+199
-197
lines changed

README.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,8 @@ real-world PHP projects.
3939

4040
| Class | Description |
4141
|---------------------|---------------------------------------------------------------------------------------------------------------------|
42-
| **Config** | Dot-access configuration loader. |
42+
| **Config** | Dot-access configuration loader with explicit hook-aware variants (`getWithHooks`, `setWithHooks`, `fillWithHooks`). |
4343
| **LazyFileConfig** | First-segment lazy loader (`db.host` loads `db.php` on demand) for lower memory usage on large config trees. |
44-
| **DynamicConfig** | Extends `Config` with **on-get/on-set hooks** to transform values dynamically (e.g., encrypt/decrypt, auto-format). |
4544
| **BaseConfigTrait** | Shared config logic. |
4645

4746

@@ -59,7 +58,7 @@ real-world PHP projects.
5958

6059
| Trait | Description |
6160
|---------------|------------------------------------------------------------------------------------------------|
62-
| **HookTrait** | Generic hook system for on-get/on-set callbacks. Used by `DynamicConfig` & `HookedCollection`. |
61+
| **HookTrait** | Generic hook system for on-get/on-set callbacks. Used by `Config`, `LazyFileConfig`, and `HookedCollection`. |
6362
| **DTOTrait** | Utility trait for DTO-like behavior: populate, extract, cast arrays/objects easily. |
6463

6564

@@ -140,12 +139,12 @@ $flat = DotNotation::flatten($user);
140139
// [ 'profile.name' => 'Alice', 'profile.email' => '[email protected]' ]
141140
```
142141

143-
### 🔹 Dynamic Config with Hooks
142+
### 🔹 Config Hooks (Explicit)
144143

145144
```php
146-
use Infocyph\ArrayKit\Config\DynamicConfig;
145+
use Infocyph\ArrayKit\Config\Config;
147146

148-
$config = new DynamicConfig();
147+
$config = new Config();
149148

150149
// Load from file
151150
$config->loadFile(__DIR__.'/config.php');
@@ -157,8 +156,8 @@ $config->onSet('auth.password', fn($v) => password_hash($v, PASSWORD_BCRYPT));
157156
$config->onGet('secure.key', fn($v) => decrypt($v));
158157

159158
// Use it
160-
$config->set('auth.password', 'secret123');
161-
$hashed = $config->get('auth.password');
159+
$config->setWithHooks('auth.password', 'secret123');
160+
$hashed = $config->getWithHooks('auth.password');
162161
```
163162

164163
### 🔹 Hooked Collection

docs/config.rst

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ Classes:
77

88
- ``Infocyph\ArrayKit\Config\Config``
99
- ``Infocyph\ArrayKit\Config\LazyFileConfig``
10-
- ``Infocyph\ArrayKit\Config\DynamicConfig``
1110

12-
``DynamicConfig`` extends ``Config`` by adding value hooks.
11+
``Config`` supports optional hooks via explicit ``getWithHooks()``,
12+
``setWithHooks()``, and ``fillWithHooks()`` methods.
1313
``LazyFileConfig`` loads namespace files only on first keyed access.
1414

1515
Loading Configuration
@@ -129,23 +129,24 @@ Array-Value Helpers
129129
// ['cors', 'auth', 'throttle']
130130
$middleware = $config->get('middleware');
131131
132-
DynamicConfig Hooks
133-
-------------------
132+
Config Hooks (Explicit)
133+
-----------------------
134134

135-
``DynamicConfig`` allows per-key transformation on read/write.
135+
``Config`` allows per-key transformation on read/write, while keeping
136+
``get()/set()/fill()`` hook-free for maximum base-path performance.
136137

137138
.. code-block:: php
138139
139140
<?php
140-
use Infocyph\ArrayKit\Config\DynamicConfig;
141+
use Infocyph\ArrayKit\Config\Config;
141142
142-
$config = new DynamicConfig();
143+
$config = new Config();
143144
144145
$config->onSet('user.name', fn ($v) => strtoupper((string) $v));
145146
$config->onGet('user.name', fn ($v) => strtolower((string) $v));
146147
147-
$config->set('user.name', 'Alice');
148-
echo $config->get('user.name'); // alice
148+
$config->setWithHooks('user.name', 'Alice');
149+
echo $config->getWithHooks('user.name'); // alice
149150
150151
Bulk operations with hooks:
151152

@@ -154,12 +155,12 @@ Bulk operations with hooks:
154155
<?php
155156
$config->onSet('user.email', fn ($v) => trim((string) $v));
156157
157-
$config->set([
158+
$config->setWithHooks([
158159
'user.name' => 'JOHN',
159160
'user.email' => ' [email protected] ',
160161
]);
161162
162-
$vals = $config->get(['user.name', 'user.email']);
163+
$vals = $config->getWithHooks(['user.name', 'user.email']);
163164
164165
Practical Pattern
165166
-----------------
@@ -169,15 +170,15 @@ Use config as a mutable runtime container for app setup:
169170
.. code-block:: php
170171
171172
<?php
172-
$config = new DynamicConfig();
173+
$config = new Config();
173174
$config->loadFile(__DIR__.'/config.php');
174175
175176
// Normalize selected runtime values
176177
$config->onSet('app.timezone', fn ($v) => trim((string) $v));
177178
$config->onGet('app.timezone', fn ($v) => strtoupper((string) $v));
178179
179-
$config->set('app.timezone', ' utc ');
180-
$tz = $config->get('app.timezone'); // UTC
180+
$config->setWithHooks('app.timezone', ' utc ');
181+
$tz = $config->getWithHooks('app.timezone'); // UTC
181182
182183
LazyFileConfig
183184
--------------
@@ -231,9 +232,9 @@ LazyFileConfig methods:
231232
- ``preload()``, ``isLoaded()``, ``loadedNamespaces()``
232233
- ``all()`` (throws by design)
233234

234-
DynamicConfig methods:
235+
Hook-aware methods (Config and LazyFileConfig):
235236

236-
- ``get()`` (hook-aware override)
237-
- ``set()`` (hook-aware override)
238-
- ``fill()`` (hook-aware override)
237+
- ``getWithHooks()``
238+
- ``setWithHooks()``
239+
- ``fillWithHooks()``
239240
- ``onGet()``, ``onSet()``

docs/quick-usage.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ Config + Hooks Example
6060
.. code-block:: php
6161
6262
<?php
63-
use Infocyph\ArrayKit\Config\DynamicConfig;
63+
use Infocyph\ArrayKit\Config\Config;
6464
65-
$config = new DynamicConfig();
65+
$config = new Config();
6666
$config->set('auth.password', 'secret');
6767
$config->onGet('auth.password', fn ($v) => strtoupper((string) $v));
68-
echo $config->get('auth.password'); // SECRET
68+
echo $config->getWithHooks('auth.password'); // SECRET
6969
7070
Global Helper Example
7171
---------------------

docs/rule-reference.rst

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ Runtime configuration with hooks:
5858
.. code-block:: php
5959
6060
<?php
61-
$config = new \Infocyph\ArrayKit\Config\DynamicConfig();
61+
$config = new \Infocyph\ArrayKit\Config\Config();
6262
$config->onSet('app.name', fn ($v) => trim((string) $v));
63-
$config->set('app.name', ' ArrayKit ');
64-
echo $config->get('app.name'); // ArrayKit
63+
$config->setWithHooks('app.name', ' ArrayKit ');
64+
echo $config->getWithHooks('app.name'); // ArrayKit
6565
6666
Static array utilities for data shaping:
6767

@@ -376,16 +376,16 @@ LazyFileConfig loads top-level config files on first keyed access:
376376
public function loadedNamespaces(): array
377377
public function all(): array // throws (design choice)
378378
379-
DynamicConfig
379+
Config Hook-Aware Variants
380380
--------------------------------------
381381

382-
DynamicConfig extends Config behavior with hooks and overrides:
382+
Both Config and LazyFileConfig expose explicit hook-aware methods:
383383

384384
.. code-block:: php
385385
386-
public function get(int|string|array|null $key = null, mixed $default = null): mixed
387-
public function set(string|array|null $key = null, mixed $value = null, bool $overwrite = true): bool
388-
public function fill(string|array $key, mixed $value = null): bool
386+
public function getWithHooks(int|string|array|null $key = null, mixed $default = null): mixed
387+
public function setWithHooks(string|array|null $key = null, mixed $value = null, bool $overwrite = true): bool
388+
public function fillWithHooks(string|array $key, mixed $value = null): bool
389389
public function onGet(string $offset, callable $callback): static
390390
public function onSet(string $offset, callable $callback): static
391391

docs/traits-and-helpers.rst

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ Main methods:
8181
``HookTrait`` is used internally by:
8282

8383
- ``Infocyph\ArrayKit\Collection\HookedCollection``
84-
- ``Infocyph\ArrayKit\Config\DynamicConfig``
84+
- ``Infocyph\ArrayKit\Config\Config``
85+
- ``Infocyph\ArrayKit\Config\LazyFileConfig``
8586

8687
HookedCollection Integration
8788
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -103,20 +104,20 @@ HookedCollection Integration
103104
$c['role'] = 'admin';
104105
echo $c['role']; // Role: admin
105106
106-
DynamicConfig Integration
107-
~~~~~~~~~~~~~~~~~~~~~~~~~
107+
Config Integration
108+
~~~~~~~~~~~~~~~~~~
108109

109110
.. code-block:: php
110111
111112
<?php
112-
use Infocyph\ArrayKit\Config\DynamicConfig;
113+
use Infocyph\ArrayKit\Config\Config;
113114
114-
$config = new DynamicConfig();
115+
$config = new Config();
115116
$config->onSet('user.email', fn ($v) => trim((string) $v));
116117
$config->onGet('user.email', fn ($v) => strtolower((string) $v));
117118
118-
$config->set('user.email', ' [email protected] ');
119-
echo $config->get('user.email'); // [email protected]
119+
$config->setWithHooks('user.email', ' [email protected] ');
120+
echo $config->getWithHooks('user.email'); // [email protected]
120121
121122
Multiple Hooks on Same Key
122123
~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -129,7 +130,7 @@ Hooks run in registration order:
129130
$config->onSet('username', fn ($v) => trim((string) $v));
130131
$config->onSet('username', fn ($v) => strtolower((string) $v));
131132
132-
$config->set('username', ' ALICE '); // becomes "alice"
133+
$config->setWithHooks('username', ' ALICE '); // becomes "alice"
133134
134135
Global Helper Functions
135136
-----------------------

src/Config/Config.php

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,75 @@
44

55
namespace Infocyph\ArrayKit\Config;
66

7+
use Infocyph\ArrayKit\traits\HookTrait;
8+
79
/**
810
* Class Config
911
*
10-
* Example usage of the BaseConfigTrait to provide
11-
* core configuration handling, plus any additional
12-
* features from the Multi trait.
12+
* Provides base configuration storage with optional hook-aware variants.
13+
*
14+
* Core methods from BaseConfigTrait (`get`, `set`, `fill`) remain fast and
15+
* hook-free. Hook processing is explicit through `getWithHooks`,
16+
* `setWithHooks`, and `fillWithHooks`.
1317
*/
1418
class Config
1519
{
1620
use BaseConfigTrait;
21+
use HookTrait;
22+
23+
/**
24+
* Hook-aware variant of fill().
25+
*/
26+
public function fillWithHooks(string|array $key, mixed $value = null): bool
27+
{
28+
if (is_array($key)) {
29+
$processed = [];
30+
foreach ($key as $path => $entry) {
31+
$processed[$path] = $this->processValue($path, $entry, 'set');
32+
}
33+
34+
return $this->fill($processed);
35+
}
36+
37+
$processed = $this->processValue($key, $value, 'set');
38+
39+
return $this->fill($key, $processed);
40+
}
41+
42+
/**
43+
* Hook-aware variant of get().
44+
*/
45+
public function getWithHooks(int|string|array|null $key = null, mixed $default = null): mixed
46+
{
47+
$value = $this->get($key, $default);
48+
49+
if (is_array($key)) {
50+
foreach ($value as $path => $entry) {
51+
$value[$path] = $this->processValue($path, $entry, 'get');
52+
}
53+
54+
return $value;
55+
}
56+
57+
return $this->processValue($key, $value, 'get');
58+
}
59+
60+
/**
61+
* Hook-aware variant of set().
62+
*/
63+
public function setWithHooks(string|array|null $key = null, mixed $value = null, bool $overwrite = true): bool
64+
{
65+
if (is_array($key)) {
66+
$processed = [];
67+
foreach ($key as $path => $entry) {
68+
$processed[$path] = $this->processValue($path, $entry, 'set');
69+
}
70+
71+
return $this->set($processed, null, $overwrite);
72+
}
73+
74+
$processedValue = $this->processValue($key, $value, 'set');
75+
76+
return $this->set($key, $processedValue, $overwrite);
77+
}
1778
}

0 commit comments

Comments
 (0)