Radu Murzea
Radu Murzea

Reputation: 10900

How can I transform my YAML file in a PHP one?

I'm using Symfony 2.3.x to implement a project. This involves having a 2 huge lists of key -> value pairs in 2 configuration files.

Currently, these are implemented as YAML files and they look like this:

parameters:
    values:
        part1:
            key1: value1
            key2: value2
            key3: value3
            key4: value4
            key5: value5
            key6: value6
        part2:
            key1: value1
            key2: value2
            key3: value3
            key4: value4
            key5: value5
            key6: value6

and

map:
    key1: ['value']
    key2: ['value1', 'value2']
    key3: ['value1', 'value2', 'value3']
    key4: ['value']
    key5: ['value1', 'value2', 'value3']
    key6: ['value']

(the values in the first one are passed to a class's constructor, that's why it's value are under a parameters root node)

In the Symfony\Component\HttpKernel\DependencyInjection\Extension implementation they are loaded like this (fairly standard):

$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/folder'));
$loader->load('myfile.yml');
$loader->load('myfile2.yml');

Everything works fine, but these files are huge (a few thousand lines each) and it's causing some performance issues. I'm not stuck with using YAML, so I thought that a good performance gain would be to skip the YAML parsing part entirely and have these values as PHP arrays directly. This would probably save me some precious milliseconds.

The problem is that I don't know how to transform them in PHP files. How should those PHP arrays look like ? How does Symfony read that file and gain access to those arrays ?

I searched Symfony's documentation, but basically all of it is about writing things in YAML format. All I could find was that this:

parameters:
    key: value

can be written as:

$container->setParameter('key', 'value');

but there are some issues here:

How can I transform my YAML files into PHP files ?

Thank you in advance :) .

Upvotes: 0

Views: 194

Answers (2)

Radu Murzea
Radu Murzea

Reputation: 10900

I solved this issue by bypassing the performance problem entirely. I simply stored the parsed files in memcache (with a big TTL) and accessed them from there. Everything works fine now.

Upvotes: 0

Wouter J
Wouter J

Reputation: 41934

Parsing the files doesn't really gain you much time imo. When you don't change the container in the dev environment, everything will be cached as normal and you won't have problems.

Also, if you have a few thousands lines of parameters, you might be doing something completely wrong. Are parameters really the thing you want? I think using a database for them might be a lot better.


If you really want to do it this way:

there is no specification as to where $container comes from

This isn't something you have to worry about. It comes from the PHP file loader. You don't have to include the definition in your file.

when you have a few thousand parameters, it really seems to be much slower to call setParameter thousands of times

I think so.

if you have array in array in array in array, how do you make that function call ?

Just use an assocative array as argument to setParameter().

Upvotes: 1

Related Questions