zozo zozo - 2 months ago 9
PHP Question

composer - dynamically set parameters variable

I have the following setup:

  • symfony 2.7 classic structure

  • composer for dependency management

What I need to do is set a variable in parameters.yml with the timestamp when composer was ran.

For this I tried the following solution:


[bla bla bla]
ran_timestamp: ~

[bla bla bla]
"scripts": {
"pre-install-cmd": [
"export SYMFONY_APP_DATE=$(date +\"%s\")"
"extra": {
"incenteev-parameters": {
"file": "app/config/parameters.yml",
"env-map": {
"ran_timestamp": "SYMFONY_APP_DATE"

The part where the variable is set inside parameters.yml works fine (the parameter is created with the value from SYMFONY_APP_DATE env variable).

The problem is that the env variable is not updated when composer is ran. Can anyone help me with that pls?

Additional info:

  • If I run the command from pre-install-cmd in cli by hand it works fine (so command itself I think is ok)

  • I see the command being run in composer after it starts install so I think it is executed (output below):

$composer install

export SYMFONY_APP_DATE=$(date +"%s")

Loading composer repositories with package information [bla bla bla]

  • No errors are reported

  • I'm assuming maybe composer doesn't have rights to set env variables? - nope, it isn't this. It is related with variable scope.


The problem apparently is that you're setting env parameter in child process (which is created for each script), but it's not possible to redefine env parameter for parent process from child (i.e. to set env value for composer itself from one of its scripts)

I think you need to extend \Incenteev\ParameterHandler\ScriptHandler::buildParameters to make it happen.

UPD: I've found a way to make it happen

Define a special block only for build-params in composer.json

"scripts": { "build-params": [ "Incenteev\\ParameterHandler\\ScriptHandler::buildParameters" ],

and than in post-install-cmd block instead of Incenteev\\ParameterHandler\\ScriptHandler::buildParameters make it

"export SYMFONY_APP_DATE=$(date +\"%s\") && composer run-script build-params"

That will create env var and building parameteres in same process