ai-context/symfony-ai-context-bundle
Installation:
composer require ai-context/symfony-ai-context-bundle --dev
Ensure the bundle is enabled in config/bundles.php (Symfony Flex handles this automatically).
Generate Context:
php bin/console ai-context:generate
Outputs a structured JSON file at var/ai_context/ai-context.json by default.
First Use Case: Use the generated JSON to query an LLM (e.g., GPT) for project insights:
"Analyze this Symfony project structure and explain how the ArticleController interacts with the StockManagerService."
Development Workflow:
ai-context:generate in a pre-commit hook or CI pipeline to keep the context updated.var/ai_context/) for traceability.Integration with AI Tools:
$context = file_get_contents('var/ai_context/ai-context.json');
$response = Http::post('https://api.openai.com/v1/chat/completions', [
'body' => ['messages' => [['role' => 'user', 'content' => $context]]],
]);
Dynamic Context:
Supported extractors table). Example:
# config/packages/ai_context.yaml
ai_context:
include:
custom_extractors: true
Then implement AiContextBundle\Extractor\ExtractorInterface.Symfony Events:
ai_context.generate events to post-process the context:
// src/EventListener/AiContextListener.php
public function onGenerate(AiContextGenerateEvent $event) {
$context = $event->getContext();
$context['custom_data'] = ['env' => $_ENV['APP_ENV']];
}
Performance:
config/packages/ai_context.yaml:
ai_context:
paths:
entities: ['%kernel.project_dir%/src/Entity/*'] # Exclude subdirectories
public/ and set HTTP cache headers).Doctrine Metadata:
doctrine/orm is installed and metadata is cached:
php bin/console doctrine:cache:clear-metadata
Type Hinting:
composer require --dev phpstan/phpstan-symfony
Symfony Flex Conflicts:
config/bundles.php with dev: true.php bin/console ai-context:generate --verbose
ai-context-checksum.json). Use it to detect changes:
diff var/ai-context/ai-context-checksum.json var/ai-context/ai-context-checksum.json.bak
Custom Extractors:
ExtractorInterface to add support for new features (e.g., custom services):
namespace App\Extractor;
use AiContextBundle\Extractor\ExtractorInterface;
class CustomServiceExtractor implements ExtractorInterface {
public function extract(array &$context): void {
$context['custom_services'] = $this->getCustomServices();
}
}
config/packages/ai_context.yaml:
ai_context:
extractors:
- App\Extractor\CustomServiceExtractor
Post-Processing:
ai_context.post_generate event to modify the JSON before saving:
$event->getContext()['metadata']['generated_at'] = (new \DateTime())->format('c');
Output Format:
JsonEncoder service:
services:
ai_context.json_encoder:
class: App\Encoder\CustomJsonEncoder
tags: ['ai_context.encoder']
var/ai_context/ to .gitignore but commit the JSON periodically for historical context.- name: Generate AI Context
run: php bin/console ai-context:generate
ai_context:
include:
entities: false # Skip entities if unchanged
```markdown
---
How can I help you explore Laravel packages today?