PHP extension that parses environment file on module startup and populates
getenv()
with this information.
This allows you to easily propagate configuration values to all PHP executions, following the 12factor app on Config.
Settings are loaded once, during PHP module startup. There is no additional
file parsing necessary during requests, because data is put into the web
servers environment variables using setenv
.
EXPERIMENTAL This extension is not tested in production yet. It currently works with PHP 5.5, 5.6 and PHP 7 on Non-ZTS builds of PHP.
Working SAPIs:
- CLI
- Apache2
- PHP Builtin Server
Not Working SAPIs:
- PHP-FPM
Regular PHP extension installation procedure:
$ phpize
$ ./configure
$ make
$ sudo make install
You configure a global configuration file that is used for all invocations of PHP through the CLI or any other SAPI (Apache, FPM) for every request.
env.file=/etc/php5/.env
With an env.file=/etc/php5/.env
of the following contents:
FOO=BAR
You can access the information during a PHP request with:
<?php
echo getenv('FOO');
You can integrate this extension into the build process of a Symfony 2 Dependency Injection Container
in your AppKernel
:
<?php
use Symfony\Component\HttpKernel\Kernel;
class AppKernel extends Kernel
{
// other methods...
protected function getEnvParameters()
{
$parameters = parent::getEnvParameters();
$parameters['DATABASE_DSN'] = $this->getenv('DATABASE_DSN');
return $parameters;
}
private function getenv($name)
{
$value = getenv($name);
if ($value === false) {
throw new \RuntimeException(sprintf("Cannot build application: %s environment variable is missing.", $name));
}
return $value;
}
}
Inspiration and parts of the code are taken from laruences yaconf extension.
MIT License