1

On the server I need to deploy my symfony 5.2.4 application, the database configuration is defined in an ini file for which the path is set as an environment variable.

The way I have done it right now is to run composer dump-env dev then in .env.local.php, add some code to load the inifile, parse it, then construct my database url and set DATABASE_URL to that value like that:

$inifile = parse_ini_file($_SERVER["DB_INI_PATH"]);
$databaseurl = 'mysql://'.$inifile["user"].':'.$inifile["password"].'@'.$inifile["host"].':'.$inifile["port"].'/'.$inifile["db"];

But this means that I have some code in this file that can't be versioned (because it also contains my APP_SECRET value), and anytime I need to redump my env, I will need to readd that custom code.

I have not found a proper place to add this ini file decoding process in my symfony app, so I am looking for any advice on the proper way to do that, in a way that would be versionable.

gp_sflover
  • 3,460
  • 5
  • 38
  • 48
Sylvain
  • 318
  • 2
  • 10
  • Please, don't add answers inside your question. If the Arleigh's anwser is not complete (is not a comment), invite him to complete it, otherwise you can add your solution in the answer section and retract your vote to the Arleight's answer to then mark your own as accepted. – gp_sflover Mar 19 '21 at 10:31
  • thanks @gp_sflover I didn't know this, I will comment on Arleigh's answer – Sylvain Mar 19 '21 at 14:36

1 Answers1

1

You can write your doctrine config in php and you will have access to environment variables. You can add your logic in "config/packages/prod/doctrine.php". The example in the docs shows how to set doctrine.dbal.url go here and click on the php tab for the example code: https://symfony.com/doc/current/configuration.html#configuration-based-on-environment-variables

Arleigh Hix
  • 9,990
  • 1
  • 14
  • 31
  • Thanks Arleigh, I had tried replacing the services.yaml to services.php but didn't find that it was possible de do it for doctrine.yaml. – Sylvain Mar 19 '21 at 09:03
  • To complete this answer, one needs to add to config/services.yaml the following lines in case you can to keep default config in doctrine.yaml + use doctrine.php just for the url part. At least this is what I have been obliged to add, otherwise doctrine.php would not load, and I didn't want to rewrite all the doctrine.yaml into doctrine.php. imports: - { resource: 'packages/doctrine.php' } – Sylvain Mar 19 '21 at 14:38