How to Switch from YAML/XML Configs to PHP Today with Symplify

Found a typo? Edit me
This post was updated at June 2021 with fresh know-how.
What is new?

Updated Rector YAML to PHP configuration, as current standard. With automated input types.


In previous post, we looked 10 reasons to switch from YAML to PHP configs. Still asking why? I dare you to disagree with 1 reason there.

If you have 1 config file, you already are on PHP side now. Close this post and enjoy life.

But what if you have 10 or even 100 YAML/XML configs? Are you doing to close down for a weekend to switch your code base?

Or maybe... 5 minute job?

Break-Even Automation

With legacy migrations, we have to handle a lot of processes that are automated. If you work on 1 project for many years, it's a matter of habit to handle it manually. But handling 3 projects a month makes you think, how to automated any repeated work. It's cheaper, faster, and more reliable. The same way the writing tests are.


We made symplify/config-transformer to handle this work for us.


Credit for inspiration first reported bugs, feature feedback, and exceptional test cases goes to archeoprog and Ryan Weaver. Their input helped get the Symplify package to high quality and covered Symfony features I didn't even know. Thank you, guys!


1. Install symplify/config-transformer

composer require symplify/config-transformer --dev

2. Run switch-format Command

This command takes 1 argument - paths to files or directories to convert:

vendor/bin/config-transformer switch-format app/config


This is how my real workflow looks like: from low hanging fruit of 1 file to the main config, to all packages. Each followed by a separated commit, so it's easier to review and fix in case of regression.

vendor/bin/config-transformer switch-format ecs.yaml
# commit

vendor/bin/config-transformer switch-format rector.yaml
# commit

vendor/bin/config-transformer switch-format app/packages
# commit

vendor/bin/config-transformer switch-format app/config/config.yaml
# commit

3. Upgrade paths to PHP in Extensions and Kernel

The config format switch is one part; the next is to update loaders in PHP code. Again, it's valid to handle it manually with search & replace in PHPStorm.

 use Symfony\Component\Config\FileLocator;
 use Symfony\Component\DependencyInjection\ContainerBuilder;
-use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
+use Symfony\Component\DependencyInjection\Loader\PhplFileLoader;
 use Symfony\Component\HttpKernel\DependencyInjection\Extension;

 final class SomeExtension extends Extension
 {
     public function load(array $configs, ContainerBuilder $container)
     {
-        $loader = new YamlFileLoader($container, new FileLocator());
+        $loader = new PhplFileLoader($container, new FileLocator());
-        $loader->load(__DIR__ . '/../Resources/config/controller.yaml');
+        $loader->load(__DIR__ . '/../Resources/config/controller.php');
-        $loader->load(__DIR__ . '/../Resources/config/events.yaml');
+        $loader->load(__DIR__ . '/../Resources/config/events.php');
     }
 }

But in case your code is not standard and can't be bothered with correct regular expressions, Rector got you covered:

composer require rector/rector --dev

Setup rector.php:

<?php

use Symplify\ConfigTransformer\ValueObject\Format;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Rector\Symfony\Rector\Class_\ChangeFileLoaderInExtensionAndKernelRector;

return function (ContainerConfigurator $containerConfigurator): void {
    $services = $containerConfigurator->services();
    $services->set(ChangeFileLoaderInExtensionAndKernelRector::class)
        ->call('configure', [[
            ChangeFileLoaderInExtensionAndKernelRector::FROM => Format::YAML,
            ChangeFileLoaderInExtensionAndKernelRector::TO => Format::PHP,
        ]]);
};

And let Rector handle the boring work:

vendor/bin/rector process app src


That's it! One little tool for you, one big leap for a PHP programmer-kind.

Supported Features

Let Us Know, Help You Grow

Is something broken? Have you found a space for improvement?

Create an issue and let us know. We'd love to hear it.


Happy coding!


Have you find this post useful? Do you want more?

Follow me on Twitter, RSS or support me on GitHub Sponsors.