ivoba / dotenv-service-provider
A dotenv ServiceProvider for Silex.
Installs: 3 618
Dependents: 0
Suggesters: 1
Security: 0
Stars: 1
Watchers: 2
Forks: 1
Open Issues: 2
Requires
- php: >=5.5.9
- silex/silex: ^2.0
- vlucas/phpdotenv: ^1.1
Requires (Dev)
- phpunit/phpunit: ^4.8
This package is auto-updated.
Last update: 2024-11-10 22:01:39 UTC
README
A dotenv ServiceProvider for Silex
Caution: this is highly overengineered!
This will set all Env vars that have a given prefix, default is SILEX_, to $app as parameters.
You can pass a function to detect if you want to run dotenv load as well to load vars from an .env file.
The functionality replaces mainly something like this:
$app['env'] = getenv('SILEX_ENV') ? getenv('SILEX_ENV') : 'dev'; if($app['env'] === 'dev'){ \Dotenv::load(); } $app['debug'] = getenv('SILEX_DEBUG') ? getenv('SILEX_DEBUG') : false; $app['this'] = getenv('this') ? getenv('this') : 'that'; //... \Dotenv::required();
About 10 LOC vs ca. 110 LOC + autoload, yiah
So its actually a bit overdressed for the party, but anyway ;)
Some goodies might be legit as getenv, $_ENV & $_SERVER support.
Usage
Register the Service:
$app->register(new \Ivoba\Silex\EnvProvider(), ['env.options' => ['prefix' => 'MYPREFIX', 'use_dotenv' => function () use ($app) { return $app['env'] === 'dev'; }, 'dotenv_dir' => __DIR__ . '/../../../..', 'var_config' => []] ]); $app['env.load'];
Yo can add default, required, allowed and typecast config options for each var.
$envOptions = ['env.options' => ['var_config' => [ 'hoo' => [EnvProvider::CONFIG_KEY_ALLOWED => 'this'], 'zack' => [EnvProvider::CONFIG_KEY_REQUIRED => true], 'dong' => [EnvProvider::CONFIG_KEY_CAST => EnvProvider::CAST_TYPE_BOOLEAN], 'zip' => [EnvProvider::CONFIG_KEY_DEFAULT => 'zippi']] ]]; $app->register(new \Ivoba\Silex\EnvProvider(), $envOptions); $app['env.load'];