a SENSIOLABS Product
Symfony

Dependency Injection
Reinventing how you manage
PHP classes

Appendix C - The YAML Format

RELEASE INFORMATION

You are currently browsing the documentation for the trunk version.

This appendix describes the YAML format used to describe parameters and services.

Format

The YAML files cannot be validated like the XML ones. So, you need to be careful when writing them.

The YAML file can define three main entries:

Placeholders

Most keys and values can use placeholders. A placeholder is a string enclosed in % signs, which is replaced dynamically at runtime by the corresponding parameter value.

Precedence Rules

When loading a YAML resource, service definitions override the current defined ones.

But for parameters, they are overridden by the current ones. It allows the parameters passed to the container constructor to have precedence over the loaded ones.

$container = new sfServiceContainerBuilder(array('foo' => 'bar'));
$loader = new sfServiceContainerLoaderFileYaml($container);
$loader->load('services.yml');

In the above example, even if the loaded resource defines a foo parameter, the value will still be 'bar' as defined in the builder constructor.

Parameters

The parameters are defined by a YAML array. All the YAML rules apply:

parameters:
  foo: bar
  values:
    - true
    - false
    - 0
    - 1000.3

Parameter values can contain placeholders:

parameters:
  foo: bar
  bar: %foo%
  baz: The placeholders can be %foo% embedded in a string

The previous YAML snippet is equivalent to the following PHP code:

array('foo' => true, 'bar' => true, 'baz' => 'The placeholders can be true embedded in a string')

You can escape a % by doubling it:

parameters:
  foo: The string has no placeholder... %%foo

A parameter can also be a reference to a service:

foo: @bar

Services

Services are defined by creating a hash where the key represents the unique identifier of the service:

services:
  foo: { class: FooClass }

The class entry is the minimum required to define a service.

Attributes

A service entry supports the following attributes:

Here is an example that uses most possibilities:

foo:
    class: FooClass
    constructor: getInstance
    shared: false
    file: %path%/foo.php
    arguments: [foo, @foo, [true, false]]
    configurator: [@baz, configure]
    calls:
      - [ setBar, [ foo, @foo, [true, false] ] ]

Aliases

You can define an alias for an existing service by simply assigning a service reference to the alias:

alias_for_foo: @foo

The alias_for_foo service is now an alias of the foo service.

Imports

Before the YAML file is parsed, the component first reads the import resources defined under the imports entry:

imports:
  - { resource: services.yml }

If you import many resources, they are interpreted in the same order as they are defined. As one resource can override previous defined parameters and services, the order is significant.

If the resource is a relative path, the resource is first looked for in the same directory as the current YAML file. If it is not found, the paths passed to the loader constructor second argument will be looked for one after the other.

By default, the same loader as the current one will be used, but you can also use any other loader class by defining a class attribute:

imports:
  - { resource: services.yml }
  - { resource: "../ini/parameters.ini", class: sfServiceContainerLoaderFileIni }

The same paths as the original loaders will be passed to the new one.

As parameters are seen as simple key/value pairs by the component, when a value is overridden, the value is replaced with the new value.

For instance, if you have the following two YAML files:

file1.yml
parameters:
  complex: [true, false]
 
file2.yml
parameters:
  complex: foo

When loading file1.yml and file2.yml in this order, the value of complex will be "foo".

powered by symfony - "symfony" is a trademark of Fabien Potencier, all rights reserved - The components "animals" are © 2009 Sensio Labs

The Sensio Labs Network

Since 1998, Sensio Labs has been promoting the Open-Source software movement by providing quality web application development, training, consulting.
Sensio Labs also supports several large Open-Source projects.