14
14
use Butschster \ContextGenerator \ConfigLoader \Reader \PhpReader ;
15
15
use Butschster \ContextGenerator \ConfigLoader \Reader \StringJsonReader ;
16
16
use Butschster \ContextGenerator \ConfigLoader \Reader \YamlReader ;
17
+ use Butschster \ContextGenerator \Directories ;
17
18
use Butschster \ContextGenerator \FilesInterface ;
18
19
use Butschster \ContextGenerator \Lib \Logger \HasPrefixLoggerInterface ;
19
20
use Psr \Log \LoggerInterface ;
25
26
{
26
27
public function __construct (
27
28
private FilesInterface $ files ,
28
- private string $ rootPath ,
29
+ private Directories $ dirs ,
29
30
private ?LoggerInterface $ logger = null ,
30
31
) {}
31
32
32
33
/**
33
34
* Create a loader for a specific config file
34
35
*
35
- * @param string $rootPath Full path to the config file
36
36
* @param array<ConfigParserPluginInterface> $parserPlugins Plugins for the config parser
37
37
* @return ConfigLoaderInterface The config loader
38
38
*/
39
- public function create (string $ rootPath , array $ parserPlugins = []): ConfigLoaderInterface
39
+ public function create (Directories $ dirs , array $ parserPlugins = []): ConfigLoaderInterface
40
40
{
41
41
\assert ($ this ->logger instanceof HasPrefixLoggerInterface);
42
42
43
43
// Create import resolver
44
44
$ importResolver = new ImportResolver (
45
+ dirs: $ dirs ,
45
46
files: $ this ->files ,
46
47
loaderFactory: $ this ,
47
48
logger: $ this ->logger ?->withPrefix('import-resolver ' ),
@@ -57,7 +58,7 @@ public function create(string $rootPath, array $parserPlugins = []): ConfigLoade
57
58
$ parserPlugins = [$ importParserPlugin , ...$ parserPlugins ];
58
59
59
60
// Create parser
60
- $ parser = new ConfigParser ($ this -> rootPath , $ this ->logger , ...$ parserPlugins );
61
+ $ parser = new ConfigParser ($ dirs -> configPath , $ this ->logger , ...$ parserPlugins );
61
62
62
63
// Create composite parser
63
64
$ compositeParser = new CompositeConfigParser ($ parser );
@@ -80,28 +81,28 @@ public function create(string $rootPath, array $parserPlugins = []): ConfigLoade
80
81
81
82
// Try different file extensions
82
83
$ jsonLoader = new ConfigLoader (
83
- configPath: $ rootPath . ' / context.json ' ,
84
+ configPath: $ dirs -> getConfigPath ( ' context.json ') ,
84
85
reader: $ jsonReader ,
85
86
parser: $ compositeParser ,
86
87
logger: $ this ->logger ,
87
88
);
88
89
89
90
$ yamlLoader = new ConfigLoader (
90
- configPath: $ rootPath . ' / context.yaml ' ,
91
+ configPath: $ dirs -> getConfigPath ( ' context.yaml ') ,
91
92
reader: $ yamlReader ,
92
93
parser: $ compositeParser ,
93
94
logger: $ this ->logger ,
94
95
);
95
96
96
97
$ ymlLoader = new ConfigLoader (
97
- configPath: $ rootPath . ' / context.yml ' ,
98
+ configPath: $ dirs -> getConfigPath ( ' context.yml ') ,
98
99
reader: $ yamlReader ,
99
100
parser: $ compositeParser ,
100
101
logger: $ this ->logger ,
101
102
);
102
103
103
104
$ phpLoader = new ConfigLoader (
104
- configPath: $ rootPath . ' / context.php ' ,
105
+ configPath: $ dirs -> getConfigPath ( ' context.php ') ,
105
106
reader: $ phpReader ,
106
107
parser: $ compositeParser ,
107
108
logger: $ this ->logger ,
@@ -115,12 +116,13 @@ public function create(string $rootPath, array $parserPlugins = []): ConfigLoade
115
116
}
116
117
117
118
// Add a new method to ConfigLoaderFactory to create a loader for a specific file path
118
- public function createForFile (string $ filePath , array $ parserPlugins = []): ConfigLoaderInterface
119
+ public function createForFile (Directories $ dirs , array $ parserPlugins = []): ConfigLoaderInterface
119
120
{
120
121
\assert ($ this ->logger instanceof HasPrefixLoggerInterface);
121
122
122
123
// Create import resolver
123
124
$ importResolver = new ImportResolver (
125
+ dirs: $ dirs ,
124
126
files: $ this ->files ,
125
127
loaderFactory: $ this ,
126
128
logger: $ this ->logger ?->withPrefix('import-resolver ' ),
@@ -136,13 +138,13 @@ public function createForFile(string $filePath, array $parserPlugins = []): Conf
136
138
$ parserPlugins = [$ importParserPlugin , ...$ parserPlugins ];
137
139
138
140
// Create parser
139
- $ parser = new ConfigParser ($ this ->rootPath , $ this ->logger , ...$ parserPlugins );
141
+ $ parser = new ConfigParser ($ this ->dirs -> rootPath , $ this ->logger , ...$ parserPlugins );
140
142
141
143
// Create composite parser
142
144
$ compositeParser = new CompositeConfigParser ($ parser );
143
145
144
146
// Determine the file extension
145
- $ extension = \pathinfo ($ filePath , PATHINFO_EXTENSION );
147
+ $ extension = \pathinfo ($ dirs -> configPath , PATHINFO_EXTENSION );
146
148
147
149
// Create the appropriate reader based on file extension
148
150
$ reader = match ($ extension ) {
@@ -163,7 +165,7 @@ public function createForFile(string $filePath, array $parserPlugins = []): Conf
163
165
164
166
// Create loader for the specific file
165
167
return new ConfigLoader (
166
- configPath: $ filePath ,
168
+ configPath: $ dirs -> configPath ,
167
169
reader: $ reader ,
168
170
parser: $ compositeParser ,
169
171
logger: $ this ->logger ,
@@ -182,7 +184,7 @@ public function createFromString(string $jsonConfig, array $parserPlugins = []):
182
184
\assert ($ this ->logger instanceof HasPrefixLoggerInterface);
183
185
184
186
// Create parser
185
- $ parser = new ConfigParser ($ this ->rootPath , $ this ->logger , ...$ parserPlugins );
187
+ $ parser = new ConfigParser ($ this ->dirs -> rootPath , $ this ->logger , ...$ parserPlugins );
186
188
187
189
// Create composite parser
188
190
$ compositeParser = new CompositeConfigParser ($ parser );
0 commit comments