How to convert symfony yaml config files to xml format?
I'd like to have Symfony configuration files as XML files.
I know, there are many code examples in the symfony-book but not all configurations-types are shown.
Is there any way to convert the existing YAML files given from the Symfony demo project to XML files to use these files as a base?
I found a dumper class in /Symfony/vendor/symfony/src/Symfony/Co开发者_C百科mponent/DependencyInjection/Dumper/XmlDumper.php
but I don't know if this tool is useful for my problem, never mind how to use it.
As far as I know, there is no generic Yaml-to-XML dumper in Symfony. I found this comment from Fabien's blog back at the end of 2009:
Fabien — December 21, 2009 10:38 #3
@simo: You can have create a generic converter from XML to YAML or vice-versa, because the semantics are quite different. In symfony, we support both YAML and XML, but the conversion is hand-crafted for each feature.
However, many 3rd-party bundles (including most of the FriendsOfSymfony bundles) have chosen to use XML as their configuration format. You can browse through code on github for examples, or if you have a specific conversion question, you can bring it here to StackOverflow.
The Symfony2 package (2.4.0) contains yml type files for their AcmeDemoBundle. It is a hassle to change those existing yml files. However, you can make a little modifications that make your newly created bundles use xml format config and routing. You can modify /app/config/config.yml
# resource: "%kernel.root_dir%/config/routing.yml"
resource: "%kernel.root_dir%/config/routing.xml"
Also, /app/config/routing_dev.yml
# resource: routing.yml
resource: routing.xml
By doing these changes, you can choose to use xml when you build your own bundles.
A simple solution is to use symplify/config-transformer.
First, install:
composer req symplify/config-transformer
And then simply:
vendor/bin/config-transformer switch-format app/config/whatever.yaml -i yaml -o xml
You can do it for the whole directory and also to switch to PHP based configuration:
vendor/bin/config-transformer switch-format app/config -i xml -o php
After doing it, you'll probably have to edit your App\Kernel
since by default it mostly loads YML files only (and some PHP config files).
If one starts with the default 5.3 Kernel, and had converted all configuration to XML, you'd need to change the file so:
namespace App;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
class Kernel extends BaseKernel
{
use MicroKernelTrait;
protected function configureContainer(ContainerConfigurator $container): void
{
$container->import('../config/{packages}/*.xml');
$container->import('../config/{packages}/'.$this->environment.'/*.xml');
$container->import('../config/services.xml');
$container->import('../config/{services}_'.$this->environment.'.xml');
}
protected function configureRoutes(RoutingConfigurator $routes): void
{
$routes->import('../config/{routes}/'.$this->environment.'/*.xml');
$routes->import('../config/{routes}/*.xml');
$routes->import('../config/routes.xml');
}
}
more a comment like an answer because it doesn't solve the real question (convert from yaml to xml) but the comment space was too small and to unformated for this info and as it is currently state of the art to use php configs instead of yaml or xml it might be accepted from the stackoverflow community as a helpful.
so the current solution would be to use migrify/config-transformer
https://github.com/migrify/config-transformer
vendor/bin/config-transformer switch-format app/config --input-format xml --output-format yaml
How to Switch from YAML/XML Configs to PHP Today with Migrify
https://tomasvotruba.com/blog/2020/07/27/how-to-switch-from-yaml-xml-configs-to-php-today-with-migrify/
10 Cool Features You Get after switching from YAML to PHP Configs
https://tomasvotruba.com/blog/2020/07/16/10-cool-features-you-get-after-switching-from-yaml-to-php-configs/
精彩评论