Serhii Smirnov
Serhii Smirnov

Reputation: 1357

External environment variable as array

How to set external environment variable as array?

If I have environment variable

SYMFONY__NSQLOOKUPD__HOSTS=["localhost:4161"]

and in config.yml:

socloz_nsq:
  lookupd_hosts: %nsqlookupd.hosts% 

Then I got an error:

Invalid type for path "socloz_nsq.lookupd_hosts". Expected array, but got string 

Upvotes: 6

Views: 5339

Answers (2)

jake stayman
jake stayman

Reputation: 1808

You can use a built-in "json" environment variable processor to decode a JSON string into an array:

SYMFONY__NSQLOOKUPD__HOSTS='["localhost:4161"]'

$nsqlookupdhosts: '%env(json:SYMFONY__NSQLOOKUPD__HOSTS)%'

Upvotes: 1

Serhii Smirnov
Serhii Smirnov

Reputation: 1357

I've found solution. Here it is:

in config.yml add to the imports section:

imports:
    - { resource: parameters.php }

then create parameters.php file at the same directory where config.yml exists, and look at the following example:

<?php

$nsqlookupdhosts = getenv('SYMFONY__NSQLOOKUPD__HOSTS');
$nsqdhosts = getenv('SYMFONY__NSQD__HOSTS');

$container->setParameter('nsqlookupd.hosts.parsed', explode(',', $nsqlookupdhosts));
$container->setParameter('nsqd.hosts.parsed', explode(',', $nsqdhosts));

use comma as delimiter in environment variable (you are not restricted to comma, use any)

SYMFONY__NSQLOOKUPD__HOSTS=localhost:4161,some.server:2222

Upvotes: 2

Related Questions