6
6
7
7
use Butschster \ContextGenerator \ConfigLoader \ConfigLoaderFactoryInterface ;
8
8
use Butschster \ContextGenerator \ConfigLoader \Exception \ConfigLoaderException ;
9
+ use Butschster \ContextGenerator \ConfigLoader \Import \PathPrefixer \DocumentOutputPathPrefixer ;
10
+ use Butschster \ContextGenerator \ConfigLoader \Import \PathPrefixer \SourcePathPrefixer ;
9
11
use Butschster \ContextGenerator \Directories ;
10
12
use Butschster \ContextGenerator \FilesInterface ;
11
13
use Psr \Log \LoggerInterface ;
16
18
final readonly class ImportResolver
17
19
{
18
20
private WildcardPathFinder $ pathFinder ;
21
+ private DocumentOutputPathPrefixer $ documentPrefixer ;
22
+ private SourcePathPrefixer $ sourcePrefixer ;
19
23
20
24
public function __construct (
21
25
private Directories $ dirs ,
@@ -24,6 +28,8 @@ public function __construct(
24
28
private ?LoggerInterface $ logger = null ,
25
29
) {
26
30
$ this ->pathFinder = new WildcardPathFinder ($ files , $ logger );
31
+ $ this ->documentPrefixer = new DocumentOutputPathPrefixer ();
32
+ $ this ->sourcePrefixer = new SourcePathPrefixer ();
27
33
}
28
34
29
35
/**
@@ -98,7 +104,7 @@ private function processWildcardImport(
98
104
ImportConfig $ importCfg ,
99
105
string $ basePath ,
100
106
array &$ parsedImports ,
101
- CircularImportDetector $ detector ,
107
+ CircularImportDetectorInterface $ detector ,
102
108
array &$ importedConfigs ,
103
109
array $ importConfig ,
104
110
): void {
@@ -131,7 +137,7 @@ private function processWildcardImport(
131
137
132
138
// Create a single import config for this match
133
139
$ singleImportCfg = new ImportConfig (
134
- path: \ltrim (str_replace ($ this ->dirs ->rootPath , '' , $ matchingPath ), '/ ' ), // Use the actual file path
140
+ path: \ltrim (\ str_replace ($ this ->dirs ->rootPath , '' , $ matchingPath ), '/ ' ), // Use the actual file path
135
141
absolutePath: $ matchingPath , // The path finder returns absolute paths
136
142
pathPrefix: $ importCfg ->pathPrefix ,
137
143
hasWildcard: false ,
@@ -178,11 +184,11 @@ private function processSingleImport(
178
184
179
185
// Apply path prefix for output document paths if specified
180
186
if ($ importCfg ->pathPrefix !== null ) {
181
- $ importedConfig = $ this ->applyPathPrefix ($ importedConfig , $ importCfg ->pathPrefix );
187
+ $ importedConfig = $ this ->documentPrefixer -> applyPrefix ($ importedConfig , $ importCfg ->pathPrefix );
182
188
}
183
189
184
- // Apply path prefix if specified
185
- $ importedConfig = $ this ->applySourcePathPrefix ($ importedConfig , $ importCfg ->configDirectory ());
190
+ // Apply source path prefix
191
+ $ importedConfig = $ this ->sourcePrefixer -> applyPrefix ($ importedConfig , $ importCfg ->configDirectory ());
186
192
187
193
// Store for later merging
188
194
$ importedConfigs [] = $ importedConfig ;
@@ -239,108 +245,6 @@ private function loadImportConfig(ImportConfig $importConfig): array
239
245
}
240
246
}
241
247
242
- /**
243
- * Apply path prefix to the output path of all documents
244
- *
245
- * The pathPrefix specifies a subdirectory to prepend to all document outputPath values
246
- * in the imported configuration.
247
- *
248
- * Example:
249
- * - Document has outputPath: 'docs/api.md'
250
- * - Import has pathPrefix: 'api/v1'
251
- * - Resulting outputPath: 'api/v1/docs/api.md'
252
- *
253
- * @param array $config The imported configuration
254
- * @param string $pathPrefix The path prefix to apply
255
- * @return array The modified configuration with path prefix applied to document outputPaths
256
- */
257
- private function applyPathPrefix (array $ config , string $ pathPrefix ): array
258
- {
259
- // Apply to document outputPath values
260
- if (isset ($ config ['documents ' ]) && \is_array ($ config ['documents ' ])) {
261
- foreach ($ config ['documents ' ] as &$ document ) {
262
- if (isset ($ document ['outputPath ' ])) {
263
- $ document ['outputPath ' ] = $ this ->combinePaths ($ pathPrefix , $ document ['outputPath ' ]);
264
- }
265
- }
266
- }
267
-
268
- return $ config ;
269
- }
270
-
271
- /**
272
- * Apply path prefix to relevant source paths
273
- */
274
- private function applySourcePathPrefix (array $ config , string $ pathPrefix ): array
275
- {
276
- // Apply to documents array
277
- if (isset ($ config ['documents ' ]) && \is_array ($ config ['documents ' ])) {
278
- foreach ($ config ['documents ' ] as &$ document ) {
279
- if (isset ($ document ['sources ' ]) && \is_array ($ document ['sources ' ])) {
280
- foreach ($ document ['sources ' ] as &$ source ) {
281
- $ source = $ this ->applyPathPrefixToSource ($ source , $ pathPrefix );
282
- }
283
- }
284
- }
285
- }
286
-
287
- return $ config ;
288
- }
289
-
290
- /**
291
- * Apply path prefix to a source configuration
292
- */
293
- private function applyPathPrefixToSource (array $ source , string $ prefix ): array
294
- {
295
- // Handle sourcePaths property
296
- if (isset ($ source ['sourcePaths ' ])) {
297
- $ source ['sourcePaths ' ] = $ this ->applyPrefixToPaths ($ source ['sourcePaths ' ], $ prefix );
298
- }
299
-
300
- // Handle composerPath property (for composer source)
301
- if (isset ($ source ['composerPath ' ]) && $ source ['type ' ] === 'composer ' ) {
302
- if (!\str_starts_with ($ source ['composerPath ' ], '/ ' )) {
303
- $ source ['composerPath ' ] = $ this ->combinePaths ($ prefix , $ source ['composerPath ' ]);
304
- }
305
- }
306
-
307
- return $ source ;
308
- }
309
-
310
- /**
311
- * Apply prefix to path(s)
312
- */
313
- private function applyPrefixToPaths (mixed $ paths , string $ prefix ): mixed
314
- {
315
- if (\is_string ($ paths )) {
316
- // Skip absolute paths
317
- if (\str_starts_with ($ paths , '/ ' )) {
318
- return $ paths ;
319
- }
320
- return $ this ->combinePaths ($ prefix , $ paths );
321
- }
322
-
323
- if (\is_array ($ paths )) {
324
- $ result = [];
325
- foreach ($ paths as $ path ) {
326
- $ result [] = $ this ->applyPrefixToPaths ($ path , $ prefix );
327
- }
328
- return $ result ;
329
- }
330
-
331
- // If it's not a string or array, return as is
332
- return $ paths ;
333
- }
334
-
335
-
336
- /**
337
- * Combine two paths with a separator
338
- */
339
- private function combinePaths (string $ prefix , string $ path ): string
340
- {
341
- return \rtrim ($ prefix , '/ ' ) . '/ ' . \ltrim ($ path , '/ ' );
342
- }
343
-
344
248
/**
345
249
* Merge multiple configurations
346
250
*/
0 commit comments