Skip to content

Commit b27d887

Browse files
authored
Merge pull request #112 from context-hub/refactor/core
Restructure application components and bootloaders
2 parents 5fe01d1 + ce09d8f commit b27d887

File tree

196 files changed

+3208
-2607
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

196 files changed

+3208
-2607
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ node_modules
1111
.php-cs-fixer.cache
1212
.context
1313
runtime
14-
mcp-*.log
14+
mcp-*.log
15+
.env

app.php

Lines changed: 38 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,17 @@
44

55
namespace Butschster\ContextGenerator;
66

7-
use Butschster\ContextGenerator\ConfigLoader\Parser\ParserPluginRegistry;
8-
use Butschster\ContextGenerator\Console\DisplayCommand;
9-
use Butschster\ContextGenerator\Console\GenerateCommand;
10-
use Butschster\ContextGenerator\Console\InitCommand;
11-
use Butschster\ContextGenerator\Console\MCPServerCommand;
12-
use Butschster\ContextGenerator\Console\SchemaCommand;
13-
use Butschster\ContextGenerator\Console\SelfUpdateCommand;
14-
use Butschster\ContextGenerator\Console\VersionCommand;
15-
use Butschster\ContextGenerator\Lib\Content\ContentBuilderFactory;
16-
use Butschster\ContextGenerator\Lib\Content\Renderer\MarkdownRenderer;
17-
use Butschster\ContextGenerator\Lib\Content\Renderer\RendererInterface;
18-
use Butschster\ContextGenerator\Lib\Files;
19-
use Butschster\ContextGenerator\Lib\GithubClient\GithubClientInterface;
20-
use Butschster\ContextGenerator\Lib\HttpClient\HttpClientFactory;
21-
use Butschster\ContextGenerator\Lib\HttpClient\HttpClientInterface;
22-
use Butschster\ContextGenerator\Lib\Logger\ConsoleLogger;
23-
use Butschster\ContextGenerator\Lib\Logger\LoggerFactory;
24-
use Butschster\ContextGenerator\McpServer\Registry\McpItemsRegistry;
25-
use Butschster\ContextGenerator\McpServer\Routing\McpResponseStrategy;
26-
use Butschster\ContextGenerator\McpServer\Routing\RouteRegistrar;
27-
use Butschster\ContextGenerator\Modifier\SourceModifierRegistry;
28-
use GuzzleHttp\Client;
29-
use GuzzleHttp\Psr7\HttpFactory;
30-
use League\Route\Router;
31-
use League\Route\Strategy\StrategyInterface;
32-
use Monolog\ErrorHandler;
33-
use Psr\Container\ContainerInterface;
7+
use Butschster\ContextGenerator\Application\Application;
8+
use Butschster\ContextGenerator\Application\ExceptionHandler;
9+
use Butschster\ContextGenerator\Application\Kernel;
3410
use Spiral\Core\Container;
35-
use Symfony\Component\Console\Application;
36-
use Symfony\Component\Console\Input\ArgvInput;
37-
use Symfony\Component\Console\Output\ConsoleOutput;
38-
use Symfony\Component\Console\Style\SymfonyStyle;
11+
use Spiral\Core\Options;
3912

4013
// -----------------------------------------------------------------------------
4114
// Prepare Global Environment
4215
// -----------------------------------------------------------------------------
43-
44-
\error_reporting(E_ERROR);
16+
\mb_internal_encoding('UTF-8');
17+
\error_reporting((E_ALL | E_STRICT) ^ E_DEPRECATED);
4518

4619

4720
// -----------------------------------------------------------------------------
@@ -55,9 +28,6 @@
5528
}
5629

5730

58-
$insidePhar = \str_starts_with(__FILE__, 'phar://');
59-
60-
6131
// -----------------------------------------------------------------------------
6232
// Load Composer's Autoloader
6333
// -----------------------------------------------------------------------------
@@ -88,24 +58,16 @@
8858

8959

9060
// -----------------------------------------------------------------------------
91-
// Execute An Application
61+
// Initialize Shared Container
9262
// -----------------------------------------------------------------------------
9363

94-
$application = new Application();
95-
$application->setDefaultCommand('generate');
96-
97-
$input = new ArgvInput();
98-
$output = new SymfonyStyle($input, new ConsoleOutput());
99-
100-
$errorHandler = new ErrorHandler(new ConsoleLogger($output));
101-
$errorHandler->registerExceptionHandler();
102-
$errorHandler->registerErrorHandler();
103-
$errorHandler->registerFatalHandler();
104-
64+
$insidePhar = \str_starts_with(__FILE__, 'phar://');
10565
$vendorPath = \dirname($vendorPath) . '/../';
10666
$versionFile = $vendorPath . '/version.json';
10767
$appPath = \realpath($vendorPath);
108-
68+
if ($insidePhar) {
69+
$appPath = \getcwd();
70+
}
10971
$version = \file_exists($versionFile)
11072
? \json_decode(\file_get_contents($versionFile), true)
11173
: [
@@ -115,91 +77,37 @@
11577

11678
$type = $version['type'] ?? 'phar';
11779

118-
if ($insidePhar) {
119-
$appPath = \getcwd();
120-
}
121-
122-
$container = new Container();
123-
$container->bindSingleton(
124-
Directories::class,
125-
new Directories(
126-
rootPath: $appPath,
127-
outputPath: $appPath . '/.context',
128-
configPath: $appPath,
129-
jsonSchemaPath: __DIR__ . '/json-schema.json',
130-
),
131-
);
80+
$options = new Options();
81+
$options->checkScope = true;
13282

133-
$container->bindSingleton(ParserPluginRegistry::class, static fn() => ParserPluginRegistry::createDefault());
134-
$container->bindSingleton(ConfigurationProviderFactory::class, ConfigurationProviderFactory::class);
135-
$container->bindSingleton(FilesInterface::class, Files::class);
136-
$container->bindSingleton(StrategyInterface::class, McpResponseStrategy::class);
83+
$container = new Container(options: $options);
13784
$container->bindSingleton(
138-
SourceModifierRegistry::class,
139-
static fn(ModifierRegistryFactory $factory) => $factory->create(),
140-
);
141-
$container->bindSingleton(
142-
HttpClientInterface::class,
143-
static function (Client $httpClient, HttpFactory $httpMessageFactory) {
144-
return HttpClientFactory::create(
145-
$httpClient,
146-
$httpMessageFactory,
147-
);
148-
},
149-
);
150-
$container->bindSingleton(RendererInterface::class, MarkdownRenderer::class);
151-
$container->bindSingleton(ContentBuilderFactory::class, ContentBuilderFactory::class);
152-
$container->bindSingleton(
153-
GithubClientInterface::class,
154-
static fn(HttpClientInterface $httpClient) => new GithubClientFactory(
155-
httpClient: $httpClient,
156-
defaultToken: \getenv('GITHUB_TOKEN') ?: null,
85+
Application::class,
86+
new Application(
87+
version: $version['version'] ?? 'dev',
88+
name: 'Context Generator',
89+
isBinary: $type !== 'phar',
15790
),
15891
);
15992

160-
$container->bindSingleton(LoggerFactory::class, LoggerFactory::class);
161-
$container->bindSingleton(Router::class, static function (StrategyInterface $strategy, ContainerInterface $container) {
162-
$router = new Router();
163-
$strategy->setContainer($container);
164-
$router->setStrategy($strategy);
165-
166-
return $router;
167-
});
168-
$container->bindSingleton(RouteRegistrar::class, RouteRegistrar::class);
169-
$container->bindSingleton(McpItemsRegistry::class, McpItemsRegistry::class);
170-
171-
// Register all commands
172-
$application->add(
173-
$container->make(VersionCommand::class, [
174-
'version' => $version['version'] ?? 'dev',
175-
]),
176-
);
177-
178-
$application->add(
179-
$container->make(InitCommand::class),
180-
);
181-
182-
$application->add(
183-
$container->make(SchemaCommand::class),
184-
);
185-
186-
$application->add(
187-
$container->make(SelfUpdateCommand::class, [
188-
'version' => $version['version'] ?? 'dev',
189-
'binaryType' => $type,
190-
]),
191-
);
192-
193-
$application->add(
194-
$container->make(GenerateCommand::class),
195-
);
196-
197-
$application->add(
198-
$container->make(DisplayCommand::class),
199-
);
93+
// -----------------------------------------------------------------------------
94+
// Execute Application
95+
// -----------------------------------------------------------------------------
20096

201-
$application->add(
202-
$container->make(MCPServerCommand::class),
203-
);
97+
$app = Kernel::create(
98+
directories: [
99+
'root' => $appPath,
100+
'output' => $appPath . '/.context',
101+
'config' => $appPath,
102+
'json-schema' => __DIR__ . '/json-schema.json',
103+
],
104+
exceptionHandler: ExceptionHandler::class,
105+
container: $container,
106+
)->run();
107+
108+
if ($app === null) {
109+
exit(255);
110+
}
204111

205-
$application->run($input, $output);
112+
$code = (int) $app->serve();
113+
exit($code);

composer.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,15 @@
3030
"nette/php-generator": "^4.1",
3131
"nikic/php-parser": "^v4.0 | ^v5.0",
3232
"vlucas/phpdotenv": "^5.6",
33+
"spiral/core": "^3.15",
34+
"spiral/console": "^3.15",
35+
"spiral/exceptions": "^3.15",
36+
"spiral/boot": "^3.15",
37+
"spiral/files": "^3.15",
3338
"symfony/yaml": "^7.2",
3439
"logiscape/mcp-sdk-php": "^1.0",
3540
"league/route": "^6.2",
36-
"laminas/laminas-diactoros": "^3.5",
37-
"spiral/core": "^3.15"
41+
"laminas/laminas-diactoros": "^3.5"
3842
},
3943
"require-dev": {
4044
"buggregator/trap": "^1.13",

context.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,4 @@ documents:
6666
outputPath: "changes.md"
6767
sources:
6868
- type: git_diff
69-
commit: unstaged
69+
commit: unstaged

psalm.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,32 @@
1111
<directory name="vendor"/>
1212
</ignoreFiles>
1313
</projectFiles>
14+
<issueHandlers>
15+
<InvalidArgument>
16+
<errorLevel type="suppress">
17+
<referencedFunction name="Spiral\Core\ScopeInterface::runScope"/>
18+
</errorLevel>
19+
</InvalidArgument>
20+
<InternalMethod>
21+
<errorLevel type="suppress">
22+
<referencedMethod name="Spiral\Core\Attribute\Scope::__construct"/>
23+
<referencedMethod name="Spiral\Core\Scope::__construct"/>
24+
</errorLevel>
25+
</InternalMethod>
26+
<InternalClass>
27+
<errorLevel type="suppress">
28+
<referencedClass name="Spiral\Core\Attribute\Scope"/>
29+
<referencedClass name="Spiral\Core\Attribute\Proxy"/>
30+
<referencedClass name="Spiral\Core\Scope"/>
31+
</errorLevel>
32+
</InternalClass>
33+
</issueHandlers>
1434
<forbiddenFunctions>
1535
<function name="var_dump"/>
1636
<function name="dd"/>
1737
<function name="dump"/>
1838
<function name="trap"/>
39+
<function name="tr"/>
40+
<function name="td"/>
1941
</forbiddenFunctions>
2042
</psalm>

src/Application/AppScope.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+
enum AppScope: string
8+
{
9+
case Compiler = 'compiler';
10+
case Mcp = 'mcp';
11+
case McpServer = 'mcp-server';
12+
case McpServerRequest = 'mcp-server-request';
13+
}

src/Application/Application.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Butschster\ContextGenerator\Application;
6+
7+
final readonly class Application
8+
{
9+
public function __construct(
10+
public string $version,
11+
public string $name,
12+
public bool $isBinary,
13+
) {}
14+
}

0 commit comments

Comments
 (0)