Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 - Defining boolean parameter in parameters.ini

Tags:

php

symfony

I'm having problems to define a boolean parameter in parameters.ini file. This is the definition:

aParameter = true

Then, in config.yml I do:

aParameter: %aParameter%

But I'm getting this error:

InvalidTypeException: Invalid type for path "myService.aParameter". Expected boolean, but got string.

This error disappears when I replace %aParameter% with true. What am I doing wrong?

like image 613
Manolo Avatar asked May 05 '14 09:05

Manolo


2 Answers

Default Symfony2 import parameters in YAML format, so one of first line should be:

imports:
    - { resource: parameters.yml }

And in parameters.yml use:

aParameter: true

I dont use INI files, so i dont know how it works.

like image 165
omelkes Avatar answered Nov 14 '22 22:11

omelkes


Symfony 2 uses parse_ini_file() function and it does not seem to return the right type for boolean value.

<?php

file_put_contents('test.ini', "test1=on\ntest2=true\ntest3=1");

var_dump(parse_ini_file('test.ini'));

will output

array(3) {
  'test1' => string(1) "1"
  'test2' => string(1) "1"
  'test3' => string(1) "1"
}

You might want to implement your own ini parser taking Symfony\Component\DependencyInjection\Loader\IniFileLoader as an example.

Example of lexer that supports booleans can be found in comments at php doc for parse_ini_file()

An alternative would be to use the yaml format for your configuration file.

like image 43
rolebi Avatar answered Nov 14 '22 23:11

rolebi