Skip to content

Commit d1accbe

Browse files
authored
Merge pull request #118 from context-hub/feature/json-schema-init
JSON Schema Support for IDE Autocompletion
2 parents 6f6833b + 1196351 commit d1accbe

File tree

5 files changed

+48
-15
lines changed

5 files changed

+48
-15
lines changed

src/Application/JsonSchema.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Butschster\ContextGenerator\Application;
6+
7+
final readonly class JsonSchema
8+
{
9+
/**
10+
* The URL where the JSON schema is hosted
11+
*/
12+
public const string SCHEMA_URL = 'https://raw.githubusercontent.com/context-hub/generator/refs/heads/main/json-schema.json';
13+
}

src/Config/Registry/ConfigRegistry.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@
77
/**
88
* Container for all registries
99
*/
10-
final class ConfigRegistry
10+
final class ConfigRegistry implements \JsonSerializable
1111
{
1212
/** @var array<string, RegistryInterface> */
1313
private array $registries = [];
1414

15+
public function __construct(
16+
private readonly ?string $schema = null,
17+
) {}
18+
1519
/**
1620
* Register a new registry
1721
*/
@@ -65,4 +69,20 @@ public function all(): array
6569
{
6670
return $this->registries;
6771
}
72+
73+
public function jsonSerialize(): array
74+
{
75+
$data = [
76+
'$schema' => $this->schema,
77+
];
78+
79+
foreach ($this->registries as $type => $registry) {
80+
$data[$type] = $registry;
81+
}
82+
83+
return \array_filter(
84+
$data,
85+
static fn($value) => $value !== null && $value !== [],
86+
);
87+
}
6888
}

src/Console/InitCommand.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
namespace Butschster\ContextGenerator\Console;
66

7+
use Butschster\ContextGenerator\Application\JsonSchema;
78
use Butschster\ContextGenerator\Config\ConfigType;
9+
use Butschster\ContextGenerator\Config\Registry\ConfigRegistry;
810
use Butschster\ContextGenerator\Directories;
911
use Butschster\ContextGenerator\Document\Document;
1012
use Butschster\ContextGenerator\Document\DocumentRegistry;
@@ -51,7 +53,11 @@ public function __invoke(Directories $dirs, FilesInterface $files): int
5153
return Command::FAILURE;
5254
}
5355

54-
$content = new DocumentRegistry([
56+
$config = new ConfigRegistry(
57+
schema: JsonSchema::SCHEMA_URL,
58+
);
59+
60+
$config->register(new DocumentRegistry([
5561
new Document(
5662
description: 'Project structure overview',
5763
outputPath: 'project-structure.md',
@@ -62,13 +68,13 @@ public function __invoke(Directories $dirs, FilesInterface $files): int
6268
),
6369
),
6470
),
65-
]);
71+
]));
6672

6773
try {
6874
$content = match ($type) {
69-
ConfigType::Json => \json_encode($content, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES),
75+
ConfigType::Json => \json_encode($config, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES),
7076
ConfigType::Yaml => Yaml::dump(
71-
\json_decode(\json_encode($content), true),
77+
\json_decode(\json_encode($config), true),
7278
10,
7379
2,
7480
Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK,

src/Console/SchemaCommand.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Butschster\ContextGenerator\Console;
66

7+
use Butschster\ContextGenerator\Application\JsonSchema;
78
use Butschster\ContextGenerator\Directories;
89
use Butschster\ContextGenerator\Lib\HttpClient\Exception\HttpException;
910
use Butschster\ContextGenerator\Lib\HttpClient\HttpClientInterface;
@@ -19,11 +20,6 @@
1920
)]
2021
final class SchemaCommand extends BaseCommand
2122
{
22-
/**
23-
* The URL where the JSON schema is hosted
24-
*/
25-
private const string SCHEMA_URL = 'https://raw.githubusercontent.com/context-hub/generator/refs/heads/main/json-schema.json';
26-
2723
#[Option(
2824
name: 'download',
2925
shortcut: 'd',
@@ -49,7 +45,7 @@ public function __invoke(
4945
$outputPath = $dirs->getFilePath($this->outputPath);
5046

5147
// Always show the URL where the schema is hosted
52-
$this->output->info('JSON schema URL: ' . self::SCHEMA_URL);
48+
$this->output->info('JSON schema URL: ' . JsonSchema::SCHEMA_URL);
5349

5450
// If no download requested, exit early
5551
if (!$this->download) {
@@ -59,7 +55,7 @@ public function __invoke(
5955

6056
// Download and save the schema
6157
try {
62-
$response = $httpClient->get(self::SCHEMA_URL, [
58+
$response = $httpClient->get(JsonSchema::SCHEMA_URL, [
6359
'User-Agent' => 'Context-Generator-Schema-Download',
6460
'Accept' => 'application/json',
6561
]);

src/Document/DocumentRegistry.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ public function getItems(): array
3838

3939
public function jsonSerialize(): array
4040
{
41-
return [
42-
'documents' => $this->documents,
43-
];
41+
return $this->documents;
4442
}
4543
}

0 commit comments

Comments
 (0)