Symfony Config Code Generator
Transform ContainerBuilder configurations into modern Symfony configuration files with zero effort
Convert your runtime ContainerBuilder into beautiful, production-ready Symfony configuration files!
This library bridges the gap between programmatic container building and modern Symfony configuration, making it perfect for migrations, code generation tools, and bundle configuration exports.
Ever needed to convert a dynamically built Symfony container into static configuration files? Migrating from legacy code? Building developer tools that generate Symfony configs? This is your solution!
π― Runtime to Config - Transform ContainerBuilder instances into modern Symfony configuration
π― Full Feature Support - Handles all Symfony DI features: autowiring, tags, aliases, decorators, and more
π― Clean Output - Generates human-readable configuration using Symfony's best practices
π― Smart Imports - Automatically manages function imports and namespaces
π― Type Safety - Preserves references, parameters, and expressions correctly
- Services - Full service definitions with classes, arguments, and method calls
- Parameters - Regular parameters and environment variables
- References - Service references, typed references, and inner references
- Tags - Service tags with attributes for event listeners, commands, etc.
- Autowiring & Autoconfigure - Modern DI features preserved
- Decorators - Service decoration with priority support
- Aliases - Service and interface aliases
- Expressions - Expression language support for dynamic values
- Tagged Iterators - Inject collections of tagged services
- Environment-specific - Conditional service registration
- Clean Formatting - Properly indented, readable output
- Automatic Imports - Function imports added automatically
- Fluent Interface - Modern configurator syntax
- Type Preservation - Maintains type information for better IDE support
Install via Composer:
composer require ruudk/symfony-config-code-generator --devTransform your ContainerBuilder into a configuration file in seconds:
<?php
declare(strict_types=1);
include 'vendor/autoload.php';
use Ruudk\SymfonyConfigCodeGenerator\SymfonyConfigCodeGenerator;
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\ExpressionLanguage\Expression;
$container = new ContainerBuilder();
// Parameters
$container->setParameter('app.debug', true);
$container->setParameter('database.url', '%env(DATABASE_URL)%');
// Simple service with autowiring
$container->register('app.logger', 'Psr\Log\LoggerInterface')
->setAutowired(true);
// Service with arguments and method calls
$container->register('app.mailer', 'App\Service\MailerService')
->addArgument(new Reference('mailer.transport'))
->addArgument('%database.url%')
->addMethodCall('setLogger', [new Reference('app.logger')])
->addMethodCall('configure', [[
'from' => '[email protected]',
]]);
// Event listener with tags
$container->register('app.request_listener', 'App\EventListener\RequestListener')
->addTag('kernel.event_listener', [
'event' => 'kernel.request',
'priority' => 100,
]);
// Service with expression
$container->register('app.feature_service', 'App\Service\FeatureService')
->addArgument(new Expression('parameter("app.debug") ? "debug" : "production"'));
// Service with tagged iterator
$container->register('app.handler_registry', 'App\Service\HandlerRegistry')
->addArgument(new TaggedIteratorArgument('app.handler'));
// Tagged services
$container->register('app.user_handler', 'App\Handler\UserHandler')
->addTag('app.handler');
// Generate the configuration
echo new SymfonyConfigCodeGenerator()->dumpFile($container);<?php
declare(strict_types=1);
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use function Symfony\Component\DependencyInjection\Loader\Configurator\env;
use function Symfony\Component\DependencyInjection\Loader\Configurator\expr;
use function Symfony\Component\DependencyInjection\Loader\Configurator\param;
use function Symfony\Component\DependencyInjection\Loader\Configurator\service;
use function Symfony\Component\DependencyInjection\Loader\Configurator\tagged_iterator;
// This file was automatically generated and should not be edited.
return static function (ContainerConfigurator $configurator) : void {
$parameters = $configurator->parameters();
$parameters->set(
'app.debug',
true,
);
$parameters->set(
'database.url',
env('DATABASE_URL'),
);
$services = $configurator->services();
$services->set(
'app.feature_service',
App\Service\FeatureService::class,
)
->args(
[
expr('parameter("app.debug") ? "debug" : "production"'),
],
);
$services->set(
'app.handler_registry',
App\Service\HandlerRegistry::class,
)
->args(
[
tagged_iterator('app.handler'),
],
);
$services->set(
'app.logger',
Psr\Log\LoggerInterface::class,
)
->autowire();
$services->set(
'app.mailer',
App\Service\MailerService::class,
)
->args(
[
service('mailer.transport'),
param('database.url'),
],
)
->call(
'setLogger',
[
service('app.logger'),
],
)
->call(
'configure',
[
[
'from' => '[email protected]',
],
],
);
$services->set(
'app.request_listener',
App\EventListener\RequestListener::class,
)
->tag(
'kernel.event_listener',
[
'event' => 'kernel.request',
'priority' => 100,
],
);
$services->set(
'app.user_handler',
App\Handler\UserHandler::class,
)
->tag('app.handler');
};- Legacy Migration - Convert old bundle configurations to modern format
- Code Generation - Build tools that output Symfony configurations
- Configuration Export - Export runtime container state for debugging
- Bundle Development - Generate configuration examples from code
- Testing - Verify container configurations programmatically
This library is powered by ruudk/code-generator, providing robust code generation capabilities with automatic formatting and imports.
Love this tool? Help me keep building awesome open source software!
Your sponsorship helps me dedicate more time to maintaining and improving this project. Every contribution, no matter the size, makes a difference!
I welcome contributions! Whether it's a bug fix, new feature, or documentation improvement, I'd love to see your PRs.
MIT License β Free to use in your projects! If you're using this and finding value, please consider sponsoring to support continued development.