77
88class ConfigPresets
99{
10+ private string $ group ;
11+ private string $ defaultPresetKey ;
12+ private string $ presetGroupKey ;
13+ private ?CanProvideConfig $ configProvider ;
14+
1015 public function __construct (
11- private string $ group ,
12- private string $ defaultPresetKey = 'defaultPreset ' ,
13- private string $ presetGroupKey = 'presets ' ,
14- private ?CanProvideConfig $ config = null ,
16+ string $ group ,
17+ string $ defaultPresetKey = 'defaultPreset ' ,
18+ string $ presetGroupKey = 'presets ' ,
19+ ?CanProvideConfig $ configProvider = null ,
1520 ) {
16- if ($ this ->config === null ) {
17- $ this ->config = ConfigResolver::default ();
18- }
21+ $ this ->group = $ group ;
22+ $ this ->defaultPresetKey = $ defaultPresetKey ;
23+ $ this ->presetGroupKey = $ presetGroupKey ;
24+ $ this ->configProvider = ConfigResolver::makeWith ($ configProvider );
1925 }
2026
2127 // FLUENT CREATION API ///////////////////////////////////////////////////
2228
23- public static function using (
24- ?CanProvideConfig $ config ,
25- ): self {
29+ public static function using (?CanProvideConfig $ config ): self {
2630 return new self (
2731 group: '' ,
28- config : $ config ?? ConfigResolver:: default () ,
32+ configProvider : $ config ,
2933 );
3034 }
3135
3236 public function for (string $ group ): self {
3337 return new self (
3438 group: $ group ,
35- config: $ this ->config ,
39+ defaultPresetKey: $ this ->defaultPresetKey ,
40+ presetGroupKey: $ this ->presetGroupKey ,
41+ configProvider: $ this ->configProvider ,
3642 );
3743 }
3844
3945 public function withConfigProvider (CanProvideConfig $ config ): self {
4046 return new self (
41- group: $ this ->group ,
47+ group : $ this ->group ,
4248 defaultPresetKey: $ this ->defaultPresetKey ,
43- presetGroupKey: $ this ->presetGroupKey ,
44- config: $ config ,
49+ presetGroupKey : $ this ->presetGroupKey ,
50+ configProvider : ConfigResolver:: makeWith ( $ config) ,
4551 );
4652 }
4753
4854 // PUBLIC INTERFACE //////////////////////////////////////////////////////
4955
5056 public function get (?string $ preset = null ): array {
51- // If no preset specified, get the default preset name
5257 $ presetName = $ preset ?? $ this ->defaultPresetName ();
53-
54- // Build path to the actual preset configuration
5558 $ presetPath = $ this ->presetPath ($ presetName );
56-
57- // Get the preset configuration
58- $ presetConfig = $ this ->config ->get ($ presetPath );
59-
59+ $ presetConfig = $ this ->configProvider ->get ($ presetPath );
6060 if (!is_array ($ presetConfig )) {
6161 throw new ConfigPresetNotFoundException ("Preset ' {$ presetName }' not found at path: {$ presetPath }" );
6262 }
63-
6463 return $ presetConfig ;
6564 }
6665
@@ -72,40 +71,40 @@ public function getOrDefault(?string $preset = null): array {
7271 return $ this ->default ();
7372 }
7473 $ presetPath = $ this ->presetPath ($ preset );
75- return $ this ->config ->get ($ presetPath );
74+ return $ this ->configProvider ->get ($ presetPath );
7675 }
7776
7877 public function hasPreset (string $ preset ): bool {
7978 $ presetPath = $ this ->presetPath ($ preset );
80- return $ this ->config ->has ($ presetPath );
79+ return $ this ->configProvider ->has ($ presetPath );
8180 }
8281
8382 public function default (): array {
8483 if (!$ this ->hasDefault ()) {
85- throw new ConfigPresetNotFoundException ("No default preset configured at path : {$ this ->defaultPresetKey }" );
84+ throw new ConfigPresetNotFoundException ("No default preset configured for group : {$ this ->group }" );
8685 }
8786 $ defaultPresetName = $ this ->defaultPresetName ();
8887 return $ this ->get ($ defaultPresetName );
8988 }
9089
9190 public function presets (): array {
9291 $ presetsPath = $ this ->path ($ this ->presetGroupKey );
93- $ presets = $ this ->config ->get ($ presetsPath , []);
92+ $ presets = $ this ->configProvider ->get ($ presetsPath , []);
9493 return is_array ($ presets ) ? array_keys ($ presets ) : [];
9594 }
9695
9796 // INTERNAL ////////////////////////////////////////////////////////
9897
9998 private function hasDefault (): bool {
10099 $ defaultPath = $ this ->path ($ this ->defaultPresetKey );
101- return $ this ->config ->has ($ defaultPath );
100+ return $ this ->configProvider ->has ($ defaultPath );
102101 }
103102
104103 private function defaultPresetName (): string {
105- $ defaultPath = $ this ->path ($ this ->defaultPresetKey );
106- $ defaultPreset = $ this ->config ->get ($ defaultPath );
104+ $ defaultPresetPath = $ this ->path ($ this ->defaultPresetKey );
105+ $ defaultPreset = $ this ->configProvider ->get ($ defaultPresetPath );
107106 if (empty ($ defaultPreset )) {
108- throw new ConfigPresetNotFoundException ("No default preset configured at path: {$ defaultPath }" );
107+ throw new ConfigPresetNotFoundException ("No default preset configured at path: {$ defaultPresetPath } for group: { $ this -> group }" );
109108 }
110109 return $ defaultPreset ;
111110 }
0 commit comments