Roderik
Roderik

Reputation: 401

Scripted editing of Symfony 2 YAML file breaks the formatting and produces errors

I'm trying to script out our entire installation process of new Symfony 2.1 projects including adding and configuring all our bundles and their dependencies. The end result should be one command that sets up everything so the developer is both forced into our best practices setup, and does not have to spend time on this.

So far this is fairly successful since it is now possible to go from 0 to fully installed CMS in about an hour (mostly due to composer installs). You can see the result here: https://github.com/Kunstmaan/KunstmaanSandbox/blob/feature/update-to-2.1/README.md

The next fase of this project is modifying the Symfony config YAML files. But here I'm stuck.

For the parameters.yml I did this with a ruby script, here is the relevant extract, the full script can be found here: https://github.com/Kunstmaan/KunstmaanSandbox/blob/feature/update-to-2.1/app/Resources/docs/scripts/sandboxinstaller.rb

parametersymlpath = ARGV[1]
projectname = ARGV[2]
parametersyml = YAML.load_file(parametersymlpath)
params = parametersyml["parameters"]
params["searchport"] = 9200
params["searchindexname"] = projectname
params["sentry.dsn"] = "https://XXXXXXXX:[email protected]/XXXX"
params["cdnpath"] = ""
params["requiredlocales"] = "nl|fr|de|en"
params["defaultlocale"] = "nl"
params["websitetitle"] = projectname.capitalize
File.open(parametersymlpath, 'w') {|f| f.write(YAML.dump(parametersyml)) }

So far so good, but the same type of script fails on the config.yml due to these lines:

imports:
    - { resource: @KunstmaanMediaBundle/Resources/config/config.yml }
    - { resource: @KunstmaanAdminBundle/Resources/config/config.yml }
    - { resource: @KunstmaanFormBundle/Resources/config/config.yml }
    - { resource: @KunstmaanSearchBundle/Resources/config/config.yml }
    - { resource: @KunstmaanAdminListBundle/Resources/config/config.yml }

The @ is a reserved character according to the YAML spec and Ruby throws an error.

So I switched to php and the Symfony yaml component since at this point in the install there is a full symfony install and i came up with this standalone command: https://gist.github.com/3526251

But when reading and dumping the config.yml file the lines above for example would turn into

imports:
    - 
      resource: @KunstmaanMediaBundle/Resources/config/config.yml
    - 
      resource: @KunstmaanAdminBundle/Resources/config/config.yml
    - 
      resource: @KunstmaanFormBundle/Resources/config/config.yml
    - 
      resource: @KunstmaanSearchBundle/Resources/config/config.yml
    - 
      resource: @KunstmaanAdminListBundle/Resources/config/config.yml

Which looks like crap and i'm not entirely sure this will even work.

So at this point i'm looking at moving the fully modified config.yml files to the install script and just overwriting the originals. I would rather not go there, since it will take constant maintenance if something changes in the symfony-standard project.

I'm wondering if there is another way?

Upvotes: 0

Views: 405

Answers (1)

choroba
choroba

Reputation: 241808

These two forms are semantically equivalent. They are called the Inline and Indented Blocks, respectively.

Upvotes: 1

Related Questions