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
@@ -57,7 +57,7 @@ public function create(string $rootPath, array $parserPlugins = []): ConfigLoade
57
57
$ parserPlugins = [$ importParserPlugin , ...$ parserPlugins ];
58
58
59
59
// Create parser
60
- $ parser = new ConfigParser ($ this -> rootPath , $ this ->logger , ...$ parserPlugins );
60
+ $ parser = new ConfigParser ($ dirs -> configPath , $ this ->logger , ...$ parserPlugins );
61
61
62
62
// Create composite parser
63
63
$ compositeParser = new CompositeConfigParser ($ parser );
@@ -80,28 +80,28 @@ public function create(string $rootPath, array $parserPlugins = []): ConfigLoade
80
80
81
81
// Try different file extensions
82
82
$ jsonLoader = new ConfigLoader (
83
- configPath: $ rootPath . ' / context.json ' ,
83
+ configPath: $ dirs -> getConfigPath ( ' context.json ') ,
84
84
reader: $ jsonReader ,
85
85
parser: $ compositeParser ,
86
86
logger: $ this ->logger ,
87
87
);
88
88
89
89
$ yamlLoader = new ConfigLoader (
90
- configPath: $ rootPath . ' / context.yaml ' ,
90
+ configPath: $ dirs -> getConfigPath ( ' context.yaml ') ,
91
91
reader: $ yamlReader ,
92
92
parser: $ compositeParser ,
93
93
logger: $ this ->logger ,
94
94
);
95
95
96
96
$ ymlLoader = new ConfigLoader (
97
- configPath: $ rootPath . ' / context.yml ' ,
97
+ configPath: $ dirs -> getConfigPath ( ' context.yml ') ,
98
98
reader: $ yamlReader ,
99
99
parser: $ compositeParser ,
100
100
logger: $ this ->logger ,
101
101
);
102
102
103
103
$ phpLoader = new ConfigLoader (
104
- configPath: $ rootPath . ' / context.php ' ,
104
+ configPath: $ dirs -> getConfigPath ( ' context.php ') ,
105
105
reader: $ phpReader ,
106
106
parser: $ compositeParser ,
107
107
logger: $ this ->logger ,
@@ -115,7 +115,7 @@ public function create(string $rootPath, array $parserPlugins = []): ConfigLoade
115
115
}
116
116
117
117
// Add a new method to ConfigLoaderFactory to create a loader for a specific file path
118
- public function createForFile (string $ filePath , array $ parserPlugins = []): ConfigLoaderInterface
118
+ public function createForFile (Directories $ dirs , array $ parserPlugins = []): ConfigLoaderInterface
119
119
{
120
120
\assert ($ this ->logger instanceof HasPrefixLoggerInterface);
121
121
@@ -136,13 +136,13 @@ public function createForFile(string $filePath, array $parserPlugins = []): Conf
136
136
$ parserPlugins = [$ importParserPlugin , ...$ parserPlugins ];
137
137
138
138
// Create parser
139
- $ parser = new ConfigParser ($ this ->rootPath , $ this ->logger , ...$ parserPlugins );
139
+ $ parser = new ConfigParser ($ this ->dirs -> rootPath , $ this ->logger , ...$ parserPlugins );
140
140
141
141
// Create composite parser
142
142
$ compositeParser = new CompositeConfigParser ($ parser );
143
143
144
144
// Determine the file extension
145
- $ extension = \pathinfo ($ filePath , PATHINFO_EXTENSION );
145
+ $ extension = \pathinfo ($ dirs -> configPath , PATHINFO_EXTENSION );
146
146
147
147
// Create the appropriate reader based on file extension
148
148
$ reader = match ($ extension ) {
@@ -163,7 +163,7 @@ public function createForFile(string $filePath, array $parserPlugins = []): Conf
163
163
164
164
// Create loader for the specific file
165
165
return new ConfigLoader (
166
- configPath: $ filePath ,
166
+ configPath: $ dirs -> configPath ,
167
167
reader: $ reader ,
168
168
parser: $ compositeParser ,
169
169
logger: $ this ->logger ,
@@ -182,7 +182,7 @@ public function createFromString(string $jsonConfig, array $parserPlugins = []):
182
182
\assert ($ this ->logger instanceof HasPrefixLoggerInterface);
183
183
184
184
// Create parser
185
- $ parser = new ConfigParser ($ this ->rootPath , $ this ->logger , ...$ parserPlugins );
185
+ $ parser = new ConfigParser ($ this ->dirs -> rootPath , $ this ->logger , ...$ parserPlugins );
186
186
187
187
// Create composite parser
188
188
$ compositeParser = new CompositeConfigParser ($ parser );
0 commit comments