Issue
I want to provide a script that can be installed with PHP's package manager composer
and that must be configurable.
With composer, we can easily define vendor binaries.
However, I don't see any possibility to configure them.
One could include a configuration file from within the package. However, name and location of the vendor directory are configurable, so this will not be very reliable.
For comparison: With Python's package manager pip
, we can use environment variables for the configuration. We can set the environment variables while activating the virtual environment, e.g. by using tools like the virtualenvwrapper
.
There must be at least experiments with similar approaches in the PHP community.
Addendum: The story behind
I have scripts that synchronize the databases and user generated files for test versions of websites.
For Django sites, I'm always using the same scripts, they rely on environment variables that I define with the virtualenvwrapper.
For Drupal sites, one can achieve a lot with drush.
But for Wordpress, I cannot find a simple and clean tool. It should
- live outside the folders available to the public
- be installable with composer, eventually in combination with other general-purpose-PHP-tools
- use a simple and robust way to detect the settings.
To be honest, I'm missing the virtualenvwrapper in PHP. The virtualenvwrapper is a linux script that basically does two things:
- It activates a Python virtualenvironment. Translated to PHP that would mean that everything that follows uses the autoloader of a certain composer package.
- It executes a script when activating or deactivating the virtualenvironment. There, one has the possibility to define environment variables a do any other useful things.
Solution
I'm not sure what you're trying to achieve, but there are plenty of options if you want to use env vars in Php.
https://github.com/symfony/dotenv is one of them and well maintained.
<?php
use Symfony\Component\Dotenv\Dotenv;
$dotenv = new Dotenv();
$dotenv->load(__DIR__.'/path/to/.env');
Then you can use those env var the way you want for configuration purposes. E.g. for DB config:
$someSettings = [
// Db config
DB_CONNECTION => [
'host' => getenv('DB_HOST'),
'username' => getenv('DB_USERNAME'),
'password' => getenv('DB_PASSWORD'),
'database' => getenv('DB_DATABASE')
]
];
Answered By - Raph
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.