Parse config and replace values when instantiating
The config object is parsed with values replaced in three different spots.
The first parsing is done when the Config class is instantiated and it replaces placeholders with environment variables:
https://github.com/cakephp/phinx/blob/dcc3e5135f7c093e8a53f90008a5266b87d69b17/src/Phinx/Config/Config.php#L51-L55
The second parsing is when getting a specific environment. This parses a DSN string if provided as well as deals with the special case of {"adapter": "sqlite", "memory": true}:
https://github.com/cakephp/phinx/blob/dcc3e5135f7c093e8a53f90008a5266b87d69b17/src/Phinx/Config/Config.php#L161-L183
The third parsing happens in Migration/Manager/Environment.php where it handles the special case of if connection key is set to PDO object (and handles throwing exception if adapter is not set):
https://github.com/cakephp/phinx/blob/dcc3e5135f7c093e8a53f90008a5266b87d69b17/src/Phinx/Migration/Manager/Environment.php#L336-L347
I think it would be good to condense all of this into one parse layer when the config object is initialized such that it:
- replaces environment variables
- parses DSN
- handles special case of sqlite + memory
- parses special case of connection
We could also make config throw an error as well if passed invalid environments as well, which would solve the problem of mixing validation and outputting as discussed in #1773. An upside of this is that now getEnvironments() and getEnvironment($environment) will now not return different things for a specific environment.
Related to this, it would also be good to pull the name automatically out of the connection attribute (#1309 requested as much). The devil in the details here is just that have to do different things depending on adapter:
if ($adapter === 'sqlite') {
// pragma database_list;
} elseif ($adapter === 'pgsql') {
// SELECT current_database();
} elseif ($adapter === 'mysql') {
// SELECT DATABASE();
} elseif ($adapter === 'sqlsrv') {
// SELECT DB_NAME()
}