Updated with latest symplify/config-transformer.
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 configs? Are you doing to close down for a weekend to switch your code base?
Or maybe... 5 minute job?
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!
composer require symplify/config-transformer --dev
switch-format
CommandThis 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
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\PhpFileLoader;
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 PhpFileLoader($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');
}
}
That's it! One little tool for you, one big leap for a PHP programmer-kind.
framework
, doctrine
or twig
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!
Do you learn from my contents or use open-source packages like Rector every day?
Consider supporting it on GitHub Sponsors.
I'd really appreciate it!