There are a few last tricks to learn before writing your own YAML files. They will allow you to avoid configuration duplication and to deal with your own YAML formats.
5.6.1. Using Constants in YAML Configuration Files
Some configuration settings rely on the value of other settings. To avoid setting the same value twice, symfony supports constants in YAML files. On encountering a setting name (one that can be accessed by sfConfig::get()
) in capital letters enclosed in %
signs, the configuration handlers replace them with their current value. See Listing 5-20 for an example.
Listing 5-20 - Using Constants in YAML Files, Example from autoload.yml
autoload:
symfony:
name: symfony
path: %SF_SYMFONY_LIB_DIR%
recursive: on
exclude: [vendor]
The path parameter will take the value returned by sfConfig::get('sf_symfony_lib_dir')
. If you want one configuration file to rely on another, you need to make sure that the file you rely on is already parsed (look in the symfony source to find out the order in which the configuration files are parsed). app.yml
is one of the last files parsed, so you may rely on others in it.
5.6.2. Using Scriptable Configuration
It may happen that your configuration relies on external parameters (such as a database or another configuration file). To deal with these particular cases, the symfony configuration files are parsed as PHP files before being passed to the YAML parser. It means that you can put PHP code in YAML files, as in Listing 5-21.
Listing 5-21 - YAML Files Can Contain PHP
all:
translation:
format: <?php echo (sfConfig::get('sf_i18n') == true ? 'xliff' : 'none')."\n" ?>
But be aware that the configuration is parsed very early in the life of a request, so you will not have any symfony built-in methods or functions to help you.
Also, as the echo
language construct does not add a carriage return by default, you need to add a "\n" or use the echoln
helper to keep the YAML format valid.
all:
translation:
format: <?php echoln(sfConfig::get('sf_i18n') == true ? 'xliff' : 'none') ?>
Caution In the production environment, the configuration is cached, so the configuration files are parsed (and executed) only once after the cache is cleared.
5.6.3. Browsing Your Own YAML File
Whenever you want to read a YAML file directly, you can use the sfYaml
class. It is a YAML parser that can turn a YAML file into a PHP associative array. Listing 5-22 presents a sample YAML file, and Listing 5-23 shows you how to parse it.
Listing 5-22 - Sample test.yml
File
house:
family:
name: Doe
parents: [John, Jane]
children: [Paul, Mark, Simone]
address:
number: 34
street: Main Street
city: Nowheretown
zipcode: 12345
Listing 5-23 - Using the sfYaml
Class to Turn a YAML File into an Associative Array
$test = sfYaml::load('/path/to/test.yml');
print_r($test);
Array(
[house] => Array(
[family] => Array(
[name] => Doe
[parents] => Array(
[0] => John
[1] => Jane
)
[children] => Array(
[0] => Paul
[1] => Mark
[2] => Simone
)
)
[address] => Array(
[number] => 34
[street] => Main Street
[city] => Nowheretown
[zipcode] => 12345
)
)
)