Skip to content

Commit 5f4b299

Browse files
authored
Merge pull request #98 from context-hub/bug-fixes
fix: Fixed bugs:
2 parents f9cf511 + 504c9e3 commit 5f4b299

20 files changed

+106
-236
lines changed

context-generator renamed to app.php

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#!/usr/bin/env php
21
<?php
32

43
declare(strict_types=1);

box.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313
"vendor"
1414
],
1515
"files": [
16-
"context-generator",
16+
"ctx",
17+
"app.php",
1718
"LICENSE",
1819
"composer.json",
1920
"composer.lock",
20-
"version.json"
21+
"version.json",
22+
"json-schema.json"
2123
],
2224
"output": ".build/phar/ctx.phar"
2325
}

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
}
5555
},
5656
"bin": [
57-
"context-generator"
57+
"ctx"
5858
],
5959
"scripts": {
6060
"cs-check": "vendor/bin/php-cs-fixer fix --dry-run",

context.yaml

+4-25
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import:
2+
- path: src/Console/context.yaml
3+
- path: src/ConfigLoader/context.yaml
4+
15
documents:
26
- description: "Context Generator Project Structure"
37
outputPath: "project-structure.md"
@@ -51,14 +55,6 @@ documents:
5155
- 'FilesInterface.php'
5256
showTreeView: true
5357

54-
- description: Configuration System
55-
outputPath: core/config-loader.md
56-
sources:
57-
- type: file
58-
sourcePaths: src/ConfigLoader
59-
filePattern: '*.php'
60-
showTreeView: true
61-
6258
- description: Document Compilation System
6359
outputPath: core/document.md
6460
sources:
@@ -157,22 +153,6 @@ documents:
157153
filePattern: '*.php'
158154
showTreeView: true
159155

160-
- description: Console Commands
161-
outputPath: console/commands.md
162-
sources:
163-
- type: file
164-
sourcePaths: src/Console
165-
filePattern: '*Command.php'
166-
showTreeView: true
167-
168-
- description: Console Renderers
169-
outputPath: console/renderers.md
170-
sources:
171-
- type: file
172-
sourcePaths: src/Console/Renderer
173-
filePattern: '*.php'
174-
showTreeView: true
175-
176156
- description: Content Building System
177157
outputPath: utilities/content-builder.md
178158
sources:
@@ -273,4 +253,3 @@ documents:
273253
sources:
274254
- type: git_diff
275255
commit: unstaged
276-

ctx

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
require __DIR__ . '/app.php';

src/ConfigLoader/ConfigLoaderFactory.php

+15-13
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Butschster\ContextGenerator\ConfigLoader\Reader\PhpReader;
1515
use Butschster\ContextGenerator\ConfigLoader\Reader\StringJsonReader;
1616
use Butschster\ContextGenerator\ConfigLoader\Reader\YamlReader;
17+
use Butschster\ContextGenerator\Directories;
1718
use Butschster\ContextGenerator\FilesInterface;
1819
use Butschster\ContextGenerator\Lib\Logger\HasPrefixLoggerInterface;
1920
use Psr\Log\LoggerInterface;
@@ -25,23 +26,23 @@
2526
{
2627
public function __construct(
2728
private FilesInterface $files,
28-
private string $rootPath,
29+
private Directories $dirs,
2930
private ?LoggerInterface $logger = null,
3031
) {}
3132

3233
/**
3334
* Create a loader for a specific config file
3435
*
35-
* @param string $rootPath Full path to the config file
3636
* @param array<ConfigParserPluginInterface> $parserPlugins Plugins for the config parser
3737
* @return ConfigLoaderInterface The config loader
3838
*/
39-
public function create(string $rootPath, array $parserPlugins = []): ConfigLoaderInterface
39+
public function create(Directories $dirs, array $parserPlugins = []): ConfigLoaderInterface
4040
{
4141
\assert($this->logger instanceof HasPrefixLoggerInterface);
4242

4343
// Create import resolver
4444
$importResolver = new ImportResolver(
45+
dirs: $dirs,
4546
files: $this->files,
4647
loaderFactory: $this,
4748
logger: $this->logger?->withPrefix('import-resolver'),
@@ -57,7 +58,7 @@ public function create(string $rootPath, array $parserPlugins = []): ConfigLoade
5758
$parserPlugins = [$importParserPlugin, ...$parserPlugins];
5859

5960
// Create parser
60-
$parser = new ConfigParser($this->rootPath, $this->logger, ...$parserPlugins);
61+
$parser = new ConfigParser($dirs->configPath, $this->logger, ...$parserPlugins);
6162

6263
// Create composite parser
6364
$compositeParser = new CompositeConfigParser($parser);
@@ -80,28 +81,28 @@ public function create(string $rootPath, array $parserPlugins = []): ConfigLoade
8081

8182
// Try different file extensions
8283
$jsonLoader = new ConfigLoader(
83-
configPath: $rootPath . '/context.json',
84+
configPath: $dirs->getConfigPath('context.json'),
8485
reader: $jsonReader,
8586
parser: $compositeParser,
8687
logger: $this->logger,
8788
);
8889

8990
$yamlLoader = new ConfigLoader(
90-
configPath: $rootPath . '/context.yaml',
91+
configPath: $dirs->getConfigPath('context.yaml'),
9192
reader: $yamlReader,
9293
parser: $compositeParser,
9394
logger: $this->logger,
9495
);
9596

9697
$ymlLoader = new ConfigLoader(
97-
configPath: $rootPath . '/context.yml',
98+
configPath: $dirs->getConfigPath('context.yml'),
9899
reader: $yamlReader,
99100
parser: $compositeParser,
100101
logger: $this->logger,
101102
);
102103

103104
$phpLoader = new ConfigLoader(
104-
configPath: $rootPath . '/context.php',
105+
configPath: $dirs->getConfigPath('context.php'),
105106
reader: $phpReader,
106107
parser: $compositeParser,
107108
logger: $this->logger,
@@ -115,12 +116,13 @@ public function create(string $rootPath, array $parserPlugins = []): ConfigLoade
115116
}
116117

117118
// 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
119120
{
120121
\assert($this->logger instanceof HasPrefixLoggerInterface);
121122

122123
// Create import resolver
123124
$importResolver = new ImportResolver(
125+
dirs: $dirs,
124126
files: $this->files,
125127
loaderFactory: $this,
126128
logger: $this->logger?->withPrefix('import-resolver'),
@@ -136,13 +138,13 @@ public function createForFile(string $filePath, array $parserPlugins = []): Conf
136138
$parserPlugins = [$importParserPlugin, ...$parserPlugins];
137139

138140
// Create parser
139-
$parser = new ConfigParser($this->rootPath, $this->logger, ...$parserPlugins);
141+
$parser = new ConfigParser($this->dirs->rootPath, $this->logger, ...$parserPlugins);
140142

141143
// Create composite parser
142144
$compositeParser = new CompositeConfigParser($parser);
143145

144146
// Determine the file extension
145-
$extension = \pathinfo($filePath, PATHINFO_EXTENSION);
147+
$extension = \pathinfo($dirs->configPath, PATHINFO_EXTENSION);
146148

147149
// Create the appropriate reader based on file extension
148150
$reader = match ($extension) {
@@ -163,7 +165,7 @@ public function createForFile(string $filePath, array $parserPlugins = []): Conf
163165

164166
// Create loader for the specific file
165167
return new ConfigLoader(
166-
configPath: $filePath,
168+
configPath: $dirs->configPath,
167169
reader: $reader,
168170
parser: $compositeParser,
169171
logger: $this->logger,
@@ -182,7 +184,7 @@ public function createFromString(string $jsonConfig, array $parserPlugins = []):
182184
\assert($this->logger instanceof HasPrefixLoggerInterface);
183185

184186
// Create parser
185-
$parser = new ConfigParser($this->rootPath, $this->logger, ...$parserPlugins);
187+
$parser = new ConfigParser($this->dirs->rootPath, $this->logger, ...$parserPlugins);
186188

187189
// Create composite parser
188190
$compositeParser = new CompositeConfigParser($parser);

src/ConfigLoader/ConfigurationProvider.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Butschster\ContextGenerator\ConfigLoader;
66

77
use Butschster\ContextGenerator\ConfigLoader\Exception\ConfigLoaderException;
8+
use Butschster\ContextGenerator\Directories;
89
use Butschster\ContextGenerator\FilesInterface;
910
use Psr\Log\LoggerInterface;
1011

@@ -16,7 +17,7 @@
1617
public function __construct(
1718
private ConfigLoaderFactory $loaderFactory,
1819
private FilesInterface $files,
19-
private string $rootPath,
20+
private Directories $dirs,
2021
private ?LoggerInterface $logger = null,
2122
private array $parserPlugins = [],
2223
) {}
@@ -47,7 +48,7 @@ public function fromPath(string $configPath): ConfigLoaderInterface
4748
]);
4849

4950
return $this->loaderFactory->create(
50-
rootPath: $resolvedPath,
51+
dirs: $this->dirs->withConfigPath($resolvedPath),
5152
parserPlugins: $this->parserPlugins,
5253
);
5354
}
@@ -56,10 +57,9 @@ public function fromPath(string $configPath): ConfigLoaderInterface
5657
]);
5758

5859
return $this->loaderFactory->createForFile(
59-
filePath: $resolvedPath,
60+
dirs: $this->dirs->withConfigPath($resolvedPath),
6061
parserPlugins: $this->parserPlugins,
6162
);
62-
6363
}
6464

6565
/**
@@ -68,11 +68,11 @@ public function fromPath(string $configPath): ConfigLoaderInterface
6868
public function fromDefaultLocation(): ConfigLoaderInterface
6969
{
7070
$this->logger?->info('Loading configuration from default location', [
71-
'rootPath' => $this->rootPath,
71+
'rootPath' => $this->dirs->rootPath,
7272
]);
7373

7474
return $this->loaderFactory->create(
75-
rootPath: $this->rootPath,
75+
dirs: $this->dirs,
7676
parserPlugins: $this->parserPlugins,
7777
);
7878
}
@@ -87,7 +87,7 @@ private function resolvePath(string $path): string
8787
$resolvedPath = $path;
8888
} else {
8989
// Otherwise, resolve it relative to the root path
90-
$resolvedPath = \rtrim($this->rootPath, '/') . '/' . $path;
90+
$resolvedPath = \rtrim($this->dirs->rootPath, '/') . '/' . $path;
9191
}
9292

9393
// Check if the path exists

src/ConfigLoader/Import/ImportResolver.php

+5-7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Butschster\ContextGenerator\ConfigLoader\ConfigLoaderFactory;
88
use Butschster\ContextGenerator\ConfigLoader\Exception\ConfigLoaderException;
9+
use Butschster\ContextGenerator\Directories;
910
use Butschster\ContextGenerator\FilesInterface;
1011
use Psr\Log\LoggerInterface;
1112

@@ -15,19 +16,14 @@
1516
final readonly class ImportResolver
1617
{
1718
public function __construct(
19+
private Directories $dirs,
1820
private FilesInterface $files,
1921
private ConfigLoaderFactory $loaderFactory,
2022
private ?LoggerInterface $logger = null,
2123
) {}
2224

2325
/**
2426
* Process imports in a configuration
25-
*
26-
* @param array<mixed> $config The configuration containing imports
27-
* @param string $basePath The base path for resolving relative paths
28-
* @param array<string> $parsedImports Already processed import paths to avoid duplicates
29-
* @param CircularImportDetector $detector Circular import detector
30-
* @return array<mixed> The merged configuration with imports processed
3127
*/
3228
public function resolveImports(
3329
array $config,
@@ -116,7 +112,9 @@ private function loadImportConfig(ImportConfig $importConfig): array
116112
}
117113

118114
try {
119-
$loader = $this->loaderFactory->createForFile($importConfig->absolutePath);
115+
$loader = $this->loaderFactory->createForFile(
116+
$this->dirs->withConfigPath($importConfig->absolutePath),
117+
);
120118

121119
if (!$loader->isSupported()) {
122120
throw new ConfigLoaderException(

src/ConfigurationProviderFactory.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ public function create(Directories $dirs): ConfigurationProvider
2222
return new ConfigurationProvider(
2323
loaderFactory: new ConfigLoaderFactory(
2424
files: $this->files,
25-
rootPath: $dirs->rootPath,
25+
dirs: $dirs,
2626
logger: $this->logger->withPrefix('config-loader'),
2727
),
2828
files: $this->files,
29-
rootPath: $dirs->rootPath,
29+
dirs: $dirs,
3030
logger: $this->logger->withPrefix('config-provider'),
3131
parserPlugins: $this->parserPluginRegistry->getPlugins(),
3232
);

src/Console/MCPServerCommand.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function __invoke(
8181
'path' => $dirs->rootPath,
8282
],
8383
);
84-
$loader = $configProvider->fromPath($configPath);
84+
$loader = $configProvider->fromPath($dirs->configPath);
8585
} else {
8686
$this->logger->info('Using default configuration location...');
8787
$loader = $configProvider->fromDefaultLocation();

src/Console/SelfUpdateCommand.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010
use Spiral\Core\Container;
1111
use Symfony\Component\Console\Attribute\AsCommand;
1212
use Symfony\Component\Console\Command\Command;
13-
use Symfony\Component\Console\Input\InputInterface;
1413
use Symfony\Component\Console\Input\InputOption;
15-
use Symfony\Component\Console\Output\OutputInterface;
1614

1715
#[AsCommand(
1816
name: 'self-update',
@@ -43,7 +41,7 @@ public function __construct(
4341
parent::__construct($container);
4442
}
4543

46-
public function __invoke(InputInterface $input, OutputInterface $output): int
44+
public function __invoke(): int
4745
{
4846
$this->output->title('Context Generator Self Update');
4947

src/Console/VersionCommand.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99
use Spiral\Core\Container;
1010
use Symfony\Component\Console\Attribute\AsCommand;
1111
use Symfony\Component\Console\Command\Command;
12-
use Symfony\Component\Console\Input\InputInterface;
1312
use Symfony\Component\Console\Input\InputOption;
14-
use Symfony\Component\Console\Output\OutputInterface;
1513

1614
#[AsCommand(
1715
name: 'version',
@@ -32,7 +30,7 @@ public function __construct(
3230
parent::__construct($container);
3331
}
3432

35-
public function __invoke(InputInterface $input, OutputInterface $output): int
33+
public function __invoke(): int
3634
{
3735
$this->output->title('Context Generator');
3836
$this->output->text('Current version: ' . $this->version);

0 commit comments

Comments
 (0)