@@ -15,11 +15,24 @@ use Butschster\ContextGenerator\Console\SelfUpdateCommand;
15
15
use Butschster \ContextGenerator \Console \VersionCommand ;
16
16
use Butschster \ContextGenerator \Lib \Content \ContentBuilderFactory ;
17
17
use Butschster \ContextGenerator \Lib \Content \Renderer \MarkdownRenderer ;
18
+ use Butschster \ContextGenerator \Lib \Content \Renderer \RendererInterface ;
18
19
use Butschster \ContextGenerator \Lib \Files ;
20
+ use Butschster \ContextGenerator \Lib \GithubClient \GithubClientInterface ;
19
21
use Butschster \ContextGenerator \Lib \HttpClient \HttpClientFactory ;
22
+ use Butschster \ContextGenerator \Lib \HttpClient \HttpClientInterface ;
23
+ use Butschster \ContextGenerator \Lib \Logger \ConsoleLogger ;
20
24
use Butschster \ContextGenerator \Lib \Logger \LoggerFactory ;
25
+ use Butschster \ContextGenerator \McpServer \Registry \McpItemsRegistry ;
26
+ use Butschster \ContextGenerator \McpServer \Routing \McpResponseStrategy ;
27
+ use Butschster \ContextGenerator \McpServer \Routing \RouteRegistrar ;
28
+ use Butschster \ContextGenerator \Modifier \SourceModifierRegistry ;
21
29
use GuzzleHttp \Client ;
22
30
use GuzzleHttp \Psr7 \HttpFactory ;
31
+ use League \Route \Router ;
32
+ use League \Route \Strategy \StrategyInterface ;
33
+ use Monolog \ErrorHandler ;
34
+ use Psr \Container \ContainerInterface ;
35
+ use Spiral \Core \Container ;
23
36
use Symfony \Component \Console \Application ;
24
37
use Symfony \Component \Console \Input \ArgvInput ;
25
38
use Symfony \Component \Console \Output \ConsoleOutput ;
@@ -29,7 +42,7 @@ use Symfony\Component\Console\Style\SymfonyStyle;
29
42
// Prepare Global Environment
30
43
// -----------------------------------------------------------------------------
31
44
32
- \error_reporting (E_ALL & ~ E_DEPRECATED & ~ E_USER_DEPRECATED );
45
+ \error_reporting (E_ERROR );
33
46
34
47
35
48
// -----------------------------------------------------------------------------
@@ -85,6 +98,11 @@ $application->setDefaultCommand('generate');
85
98
$ input = new ArgvInput ();
86
99
$ output = new SymfonyStyle ($ input , new ConsoleOutput ());
87
100
101
+ $ errorHandler = new ErrorHandler (new ConsoleLogger ($ output ));
102
+ $ errorHandler ->registerExceptionHandler ();
103
+ $ errorHandler ->registerErrorHandler ();
104
+ $ errorHandler ->registerFatalHandler ();
105
+
88
106
$ vendorPath = \dirname ($ vendorPath ) . '/../ ' ;
89
107
$ versionFile = $ vendorPath . '/version.json ' ;
90
108
$ appPath = \realpath ($ vendorPath );
@@ -102,109 +120,87 @@ if ($insidePhar) {
102
120
$ appPath = \getcwd ();
103
121
}
104
122
105
- // Create base services
106
- $ files = new Files ();
107
- $ httpClient = new Client ();
108
- $ httpMessageFactory = new HttpFactory ();
109
- $ httpClientAdapter = HttpClientFactory::create (
110
- $ httpClient ,
111
- $ httpMessageFactory ,
123
+ $ container = new Container ();
124
+ $ container ->bindSingleton (
125
+ Directories::class,
126
+ new Directories (
127
+ rootPath: $ appPath ,
128
+ outputPath: $ appPath . '/.context ' ,
129
+ configPath: $ appPath ,
130
+ jsonSchemaPath: __DIR__ . '/json-schema.json ' ,
131
+ ),
112
132
);
113
133
114
- // Create the factory chain for the GenerateCommand
115
- $ contentBuilderFactory = new ContentBuilderFactory (
116
- defaultRenderer: new MarkdownRenderer (),
134
+ $ container ->bindSingleton (ParserPluginRegistry::class, static fn () => ParserPluginRegistry::createDefault ());
135
+ $ container ->bindSingleton (ConfigurationProviderFactory::class, ConfigurationProviderFactory::class);
136
+ $ container ->bindSingleton (FilesInterface::class, Files::class);
137
+ $ container ->bindSingleton (StrategyInterface::class, McpResponseStrategy::class);
138
+ $ container ->bindSingleton (
139
+ SourceModifierRegistry::class,
140
+ static fn (ModifierRegistryFactory $ factory ) => $ factory ->create (),
117
141
);
118
-
119
- $ githubClientFactory = new GithubClientFactory (
120
- httpClient: $ httpClientAdapter ,
121
- defaultToken: \getenv ('GITHUB_TOKEN ' ) ?: null ,
142
+ $ container ->bindSingleton (
143
+ HttpClientInterface::class,
144
+ static function (Client $ httpClient , HttpFactory $ httpMessageFactory ) {
145
+ return HttpClientFactory::create (
146
+ $ httpClient ,
147
+ $ httpMessageFactory ,
148
+ );
149
+ },
122
150
);
123
-
124
- $ variableResolverFactory = new VariableResolverFactory ();
125
- $ modifierRegistryFactory = new ModifierRegistryFactory ();
126
-
127
- // Create the source fetcher registry factory with its dependencies
128
- $ sourceFetcherRegistryFactory = new SourceFetcherRegistryFactory (
129
- httpClient: $ httpClientAdapter ,
130
- contentBuilderFactory: $ contentBuilderFactory ,
131
- variableResolverFactory: $ variableResolverFactory ,
132
- githubClientFactory: $ githubClientFactory ,
133
- );
134
-
135
- // Create the document compiler factory with its dependencies
136
- $ documentCompilerFactory = new DocumentCompilerFactory (
137
- files: $ files ,
138
- sourceFetcherRegistryFactory: $ sourceFetcherRegistryFactory ,
139
- modifierRegistryFactory: $ modifierRegistryFactory ,
140
- contentBuilderFactory: $ contentBuilderFactory ,
151
+ $ container ->bindSingleton (RendererInterface::class, MarkdownRenderer::class);
152
+ $ container ->bindSingleton (ContentBuilderFactory::class, ContentBuilderFactory::class);
153
+ $ container ->bindSingleton (
154
+ GithubClientInterface::class,
155
+ static fn (HttpClientInterface $ httpClient ) => new GithubClientFactory (
156
+ httpClient: $ httpClient ,
157
+ defaultToken: \getenv ('GITHUB_TOKEN ' ) ?: null ,
158
+ ),
141
159
);
142
160
143
- $ configProviderFactory = new ConfigurationProviderFactory (
144
- parserPluginRegistry: ParserPluginRegistry::createDefault (),
145
- );
161
+ $ container ->bindSingleton (LoggerFactory::class, LoggerFactory::class);
162
+ $ container ->bindSingleton (Router::class, static function (StrategyInterface $ strategy , ContainerInterface $ container ) {
163
+ $ router = new Router ();
164
+ $ strategy ->setContainer ($ container );
165
+ $ router ->setStrategy ($ strategy );
146
166
147
- $ logger = LoggerFactory:: create (
148
- output: $ output ,
149
- loggingEnabled: $ output -> isVerbose () || $ output -> isDebug () || $ output -> isVeryVerbose (),
150
- );
167
+ return $ router ;
168
+ });
169
+ $ container -> bindSingleton (RouteRegistrar::class, RouteRegistrar::class);
170
+ $ container -> bindSingleton (McpItemsRegistry::class, McpItemsRegistry::class );
151
171
152
172
// Register all commands
153
173
$ application ->add (
154
- new VersionCommand (
155
- version: $ version ['version ' ] ?? 'dev ' ,
156
- httpClient: $ httpClientAdapter ,
157
- ),
174
+ $ container ->make (VersionCommand::class, [
175
+ 'version ' => $ version ['version ' ] ?? 'dev ' ,
176
+ ]),
158
177
);
159
178
160
179
$ application ->add (
161
- new InitCommand (
162
- baseDir: $ appPath ,
163
- files: $ files ,
164
- ),
180
+ $ container ->make (InitCommand::class),
165
181
);
166
182
167
183
$ application ->add (
168
- new SchemaCommand (
169
- httpClient: $ httpClientAdapter ,
170
- ),
184
+ $ container ->make (SchemaCommand::class),
171
185
);
172
186
173
187
$ application ->add (
174
- new SelfUpdateCommand (
175
- version: $ version ['version ' ] ?? 'dev ' ,
176
- httpClient: $ httpClientAdapter ,
177
- files: $ files ,
178
- binaryType: $ type ,
179
- ),
188
+ $ container ->make (SelfUpdateCommand::class, [
189
+ 'version ' => $ version ['version ' ] ?? 'dev ' ,
190
+ 'binaryType ' => $ type ,
191
+ ]),
180
192
);
181
193
182
194
$ application ->add (
183
- new GenerateCommand (
184
- rootPath: $ appPath ,
185
- outputPath: $ appPath . '/.context ' ,
186
- files: $ files ,
187
- documentCompilerFactory: $ documentCompilerFactory ,
188
- configurationProviderFactory: $ configProviderFactory ,
189
- ),
195
+ $ container ->make (GenerateCommand::class),
190
196
);
191
197
192
198
$ application ->add (
193
- new DisplayCommand (
194
- rootPath: $ appPath ,
195
- files: $ files ,
196
- configurationProviderFactory: $ configProviderFactory ,
197
- ),
199
+ $ container ->make (DisplayCommand::class),
198
200
);
199
201
200
202
$ application ->add (
201
- new MCPServerCommand (
202
- rootPath: $ appPath ,
203
- jsonSchemaPath: __DIR__ . '/json-schema.json ' ,
204
- files: $ files ,
205
- documentCompilerFactory: $ documentCompilerFactory ,
206
- configurationProviderFactory: $ configProviderFactory ,
207
- ),
203
+ $ container ->make (MCPServerCommand::class),
208
204
);
209
205
210
206
$ application ->run ($ input , $ output );
0 commit comments