Skip to content

Commit

Permalink
Replaced ConfigServiceProvider with Dotenv
Browse files Browse the repository at this point in the history
  • Loading branch information
wjzijderveld committed Jul 2, 2015
1 parent c703c02 commit 0cac75e
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 23 deletions.
4 changes: 4 additions & 0 deletions .env.dist
@@ -0,0 +1,4 @@
TOGGLE__DEBUG=true
TOGGLE__ALLOWED_ORIGINS='["127.0.0.1"]'
TOGGLE__REDIS_DSN="tcp://127.0.0.1:6379"
TOGGLE__PREFIX="feature_toggle"
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -3,3 +3,4 @@ composer.lock
composer.phar
/vendor/
/config.json
.env
4 changes: 3 additions & 1 deletion .travis.yml
Expand Up @@ -14,6 +14,8 @@ matrix:
allow_failures:
- php: hhvm-nightly

before_script: composer install --dev
before_script:
- composer install --dev
- cp .env.dist .env

script: phpunit
11 changes: 11 additions & 0 deletions CHANGELOG.md
@@ -0,0 +1,11 @@
# Changelog for Qandidate toggle API

## 0.3.x

- Replaced `igorw/config-service-provider` with `vlucas/phpdotenv`. With this method there is [more flexibility to deploy] the API
`.env.dist` gives you the same configuration as `config.json.dist` gave you before.
But you will need to recreate your config if you adjusted the configuration.
Note that `TOGGLE__ALLOWED_ORIGINS` should be a JSON string and the name for the redis connection changed
from `uri` to `dsn` (`TOGGLE__REDIS_DSN`).

[more flexibility to deploy]: http://12factor.net/config
7 changes: 4 additions & 3 deletions README.md
Expand Up @@ -19,10 +19,11 @@ Install the dependencies with composer:
$ composer install
```

The default configuration is mainly for local development. For running production you should create your own configuration file.

Copy the `config.json.dist` to `config.json` and adjust where needed.
Configuration is determined based on environment variables. Copy `.env.dist` to `.env` and adjust where needed.
You can override the values in the file with environment values.
The default configuration is mainly for local development.

The environment variable `TOGGLE__ALLOWED_ORIGINS` should be valid JSON. This is to allow arrays.

## Running the tests

Expand Down
23 changes: 17 additions & 6 deletions app/bootstrap.php
Expand Up @@ -23,12 +23,23 @@

$app = new Application();

$configFile = __DIR__ . '/../config.json';
if (! file_exists($configFile)) {
$configFile = __DIR__ . '/../config.json.dist';
}
$env = new \Dotenv\Dotenv(__DIR__ . '/../');
$env->load();
$env->required(array(
'TOGGLE__DEBUG',
'TOGGLE__REDIS_DSN',
'TOGGLE__ALLOWED_ORIGINS',
'TOGGLE__PREFIX',
));

$app->register(new ConfigServiceProvider($configFile));
$app['debug'] = getenv('TOGGLE__DEBUG');
$app['redis_dsn'] = getenv('TOGGLE__REDIS_DSN');
$app['toggle.manager.prefix'] = getenv('TOGGLE__PREFIX');
$app['allowed_origins'] = json_decode(getenv('TOGGLE__ALLOWED_ORIGINS'));

if (JSON_ERROR_NONE !== json_last_error()) {
throw new RuntimeException('Failed to json_decode TOGGLE__ALLOWED_ORIGINS');
}

$app['env'] = getenv('env') ?: 'dev';

Expand All @@ -37,7 +48,7 @@
}

$app->register(new Predis\Silex\PredisServiceProvider(), array(
'predis.parameters' => $app['redis_uri'],
'predis.parameters' => $app['redis_dsn'],
));

$app['toggle.manager.collection'] = $app->share(function ($app) {
Expand Down
11 changes: 5 additions & 6 deletions composer.json
Expand Up @@ -23,15 +23,14 @@
],
"require": {
"php": ">=5.3.2",
"silex/silex": "1.*",
"qandidate/toggle": "~0.1",
"predis/service-provider": "0.4.*",
"asm89/stack-cors": "~0.2.1",
"igorw/config-service-provider": "~1.2"
"silex/silex": "^1.0",
"qandidate/toggle": "^0.2@dev",
"predis/service-provider": "^0.4@dev",
"asm89/stack-cors": "^0.2.1@dev",
"vlucas/phpdotenv": "^2.0"
},
"require-dev": {
"symfony/browser-kit": "~2.3"
},
"minimum-stability": "dev",
"prefer-stable": true
}
6 changes: 0 additions & 6 deletions config.json.dist

This file was deleted.

2 changes: 1 addition & 1 deletion web/index.php
Expand Up @@ -17,7 +17,7 @@
$app = require_once __DIR__ . '/../app/bootstrap.php';

$stackedApp = new Cors($app, array(
'allowedOrigins' => $app['allowedOrigins'],
'allowedOrigins' => $app['allowed_origins'],
'allowedMethods' => array('DELETE', 'GET', 'PUT', 'POST'),
'allowedHeaders' => array('accept', 'content-type', 'origin', 'x-requested-with'),
));
Expand Down

0 comments on commit 0cac75e

Please sign in to comment.