@@ -15,11 +15,24 @@ use Butschster\ContextGenerator\Console\SelfUpdateCommand;
1515use Butschster \ContextGenerator \Console \VersionCommand ;
1616use Butschster \ContextGenerator \Lib \Content \ContentBuilderFactory ;
1717use Butschster \ContextGenerator \Lib \Content \Renderer \MarkdownRenderer ;
18+ use Butschster \ContextGenerator \Lib \Content \Renderer \RendererInterface ;
1819use Butschster \ContextGenerator \Lib \Files ;
20+ use Butschster \ContextGenerator \Lib \GithubClient \GithubClientInterface ;
1921use Butschster \ContextGenerator \Lib \HttpClient \HttpClientFactory ;
22+ use Butschster \ContextGenerator \Lib \HttpClient \HttpClientInterface ;
23+ use Butschster \ContextGenerator \Lib \Logger \ConsoleLogger ;
2024use 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 ;
2129use GuzzleHttp \Client ;
2230use 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 ;
2336use Symfony \Component \Console \Application ;
2437use Symfony \Component \Console \Input \ArgvInput ;
2538use Symfony \Component \Console \Output \ConsoleOutput ;
@@ -29,7 +42,7 @@ use Symfony\Component\Console\Style\SymfonyStyle;
2942// Prepare Global Environment
3043// -----------------------------------------------------------------------------
3144
32- \error_reporting (E_ALL & ~ E_DEPRECATED & ~ E_USER_DEPRECATED );
45+ \error_reporting (E_ERROR );
3346
3447
3548// -----------------------------------------------------------------------------
@@ -85,6 +98,11 @@ $application->setDefaultCommand('generate');
8598$ input = new ArgvInput ();
8699$ output = new SymfonyStyle ($ input , new ConsoleOutput ());
87100
101+ $ errorHandler = new ErrorHandler (new ConsoleLogger ($ output ));
102+ $ errorHandler ->registerExceptionHandler ();
103+ $ errorHandler ->registerErrorHandler ();
104+ $ errorHandler ->registerFatalHandler ();
105+
88106$ vendorPath = \dirname ($ vendorPath ) . '/../ ' ;
89107$ versionFile = $ vendorPath . '/version.json ' ;
90108$ appPath = \realpath ($ vendorPath );
@@ -102,109 +120,87 @@ if ($insidePhar) {
102120 $ appPath = \getcwd ();
103121}
104122
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+ ),
112132);
113133
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 (),
117141);
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+ },
122150);
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+ ),
141159);
142160
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 );
146166
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 );
151171
152172// Register all commands
153173$ 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+ ]),
158177);
159178
160179$ application ->add (
161- new InitCommand (
162- baseDir: $ appPath ,
163- files: $ files ,
164- ),
180+ $ container ->make (InitCommand::class),
165181);
166182
167183$ application ->add (
168- new SchemaCommand (
169- httpClient: $ httpClientAdapter ,
170- ),
184+ $ container ->make (SchemaCommand::class),
171185);
172186
173187$ 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+ ]),
180192);
181193
182194$ 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),
190196);
191197
192198$ application ->add (
193- new DisplayCommand (
194- rootPath: $ appPath ,
195- files: $ files ,
196- configurationProviderFactory: $ configProviderFactory ,
197- ),
199+ $ container ->make (DisplayCommand::class),
198200);
199201
200202$ 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),
208204);
209205
210206$ application ->run ($ input , $ output );
0 commit comments