pepper
pepper

Reputation: 1772

How does a Symfony pass parameters into a yaml config?

A Symfony Config component allows to create and load a configuration. For instance, it allows to use a yml for config.

However, in a Symfony framework configuration uses parameters with a special syntax - %parameter.key%

I've looked at Symfony YamlFileLoader but it only load a content of a yaml-file and handle such sections as imports, parameters and services. The only goal of the parameters is to be stored in a container parameter bag.

So I'm wondering what's magic used to replace placeholders in a yaml config file?

Moreover. I've tried to load a config:

application:
    aBooleanKey: "true"

and I've got an error since aBooleanKey requires a boolean value but got a string.

Upvotes: 2

Views: 2704

Answers (3)

max4ever
max4ever

Reputation: 12142

In symfony 3.4 they "fix" the bug of boolean passed as arguments that get casted into strings.

https://symfony.com/blog/new-in-symfony-3-4-advanced-environment-variables

parameters:
    # roughly equivalent to "(bool) getenv('CONNECTION_ENABLED')"
    app.connection.enabled: '%env(bool:CONNECTION_ENABLED)%'

Upvotes: 0

Timurib
Timurib

Reputation: 2743

So I'm wondering what's magic used to replace placeholders in a yaml config file?

This Parameters are part of the Dependency Injection component, not the Config.

The only goal of the parameters is to be stored in a container parameter bag.

Not quite. Special compiler pass resolves the placeholders. It works at the optimization step of the container compilation and inject actual values, so that placeholders can be used in the service definitions and in the semantic configuration files.

... and I've got an error since aBooleanKey requires a boolean value but got a string.

I guess that caused by YAML Component:

Finally, there are other cases when the strings must be quoted, no matter if you're using single or double quotes:

  • When the string is true or false (otherwise, it would be treated as a boolean value);

...

So, in your example true is quoted and treated as string.

Upvotes: 2

dbrumann
dbrumann

Reputation: 17166

Bundles can define what kind of configuration they expect, including required values and restricting types. This is usually done in the bundle's DependencyInjection/Configuration.php. Here's a link to the configuration for FOSUserBundle: https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/DependencyInjection/Configuration.php

The TreeBuilder defines the structure and the configuration created from parsing the yaml (or other config format's) file is passed in and validated against the requirements. You can also check the Symfony docs on Defining and Processing Configuration Values

Upvotes: 0

Related Questions