1

Please consider following Yaml file:

files: 
  /etc/cron.d/{templateJob}: 
    group: root
    mode: "000644"
    owner: root
    content: |
        {templateJobTime} root /usr/local/bin/{templateJob}.sh

  /usr/local/bin/{templateJob}.sh: 
    mode: "000755"
    owner: root
    group: root
    content: |
        #!/bin/bash
        date > /tmp/date
        curl -s {templateJobURL}
        exit 0

commands: 
  remove_old_cron: 
    command: "rm -f /etc/cron.d/*.bak"

I want to parse and dump the above file using \Symfony\Component\Yaml\Yaml after some modifications, currently, I'm getting the following output:

//input
$content = Yaml::parse(file_get_contents('file.config'));
//modifications

//output
file_put_contents('file.config',Yaml::dump($content, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));

#Output


files:
    '/etc/cron.d/{templateJob}':
        group: root
        mode: '000644'
        owner: root
        content: "{templateJobTime} root /usr/local/bin/{templateJobFile}.sh\n"
    '/usr/local/bin/{testJobFile}.sh':
        mode: '000755'
        owner: root
        group: root
        content: "#!/bin/bash\ndate > /tmp/date\ncurl -s {templateJobURL}\nexit 0\n"
commands:
    remove_old_cron:
        command: 'rm -f /etc/cron.d/*.bak'

see the difference between input and output file, how can I get the correct output in input file format.

Osama Alvi
  • 659
  • 4
  • 14
  • 1
    Symphony-specific notes: [The docs](https://symfony.com/doc/current/components/yaml.html) make clear that Symphony's YAML component is pretty bare-bones and ignores YAML conventions (like e.g. naming `parse` what is labelled **load** in the YAML spec). It does not give you access to any lower-level API like the node graph or the event tree, which limits your options. While you can give some flags to modify general output layout, you will not in general be able to replicate the input layout, unless you craft it specifically into something Symphony would output. – flyx Mar 02 '21 at 10:38

0 Answers0