Skip to content

Commit

Permalink
Initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
f3ath committed Mar 2, 2017
1 parent a31e90b commit 2229c02
Show file tree
Hide file tree
Showing 14 changed files with 242 additions and 4 deletions.
5 changes: 1 addition & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
composer.phar
/vendor/

# Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
# composer.lock
composer.lock
13 changes: 13 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
language: php
php:
- '7.0'
- '7.1'
- hhvm
- nightly

before_script:
- composer install

script:
- vendor/bin/phpcs
- vendor/bin/phpunit
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,16 @@
# silex-config
Simple multi-environment configuration for Silex with secret storage support
## Install
```
composer install f3ath/silex-config
```
## Usage
```php
<?php
$app = new \Silex\Application\Application();
$env = 'prod';
$path = '/path-to-config';
(new \F3\SilexConfig\Config($path))->configure($app, $env);
```

For more examples see the [unit test](test/ConfigTest.php).
31 changes: 31 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "f3ath/silex-config",
"description": "Simple multi-environment configuration for Silex with secret storage support",
"type": "library",
"require-dev": {
"silex/silex": "^2",
"phpunit/phpunit": "^6.0",
"squizlabs/php_codesniffer": "^2.8"
},
"license": "MIT",
"authors": [
{
"name": "Alexey Karapetov",
"email": "[email protected]"
}
],
"require": {},
"config": {
"platform": {
"php": "7"
}
},
"suggest": {
"silex/silex": "Silex Framework"
},
"autoload": {
"psr-4": {
"F3\\SilexConfig\\": "src/"
}
}
}
6 changes: 6 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0"?>
<ruleset>
<file>src</file>
<file>test</file>
<rule ref="PSR2"/>
</ruleset>
21 changes: 21 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8" ?>
<phpunit
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnFailure="false"
verbose="true"
syntaxCheck="true">
<testsuites>
<testsuite name="Main">
<directory>./test</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">./src/</directory>
</whitelist>
</filter>
</phpunit>
58 changes: 58 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
namespace F3\SilexConfig;

use Silex\Application;

class Config
{
const KEY_SERVICES = 'services';
const KEY_SECRET_JSON = 'secret_json';
private $dir;

public function __construct(string $dir)
{
$this->dir = $dir;
}

public function configure(Application $app, string $env = 'prod')
{
$config = array_replace_recursive(
$this->getDefaultConfig(),
$this->getConfig($env)
);
if ($config[self::KEY_SECRET_JSON]) {
$config = array_replace_recursive(
$config,
$this->getSecretConfig($config[self::KEY_SECRET_JSON])
);
}
array_map(
function ($service) use ($app, $config, $env) {
(require $service)($app, $config, $env);
},
$config[self::KEY_SERVICES]
);
}

private function getConfig(string $env): array
{
$file = "{$this->dir}/{$env}.php";
if (file_exists($file)) {
return include $file;
}
throw new \InvalidArgumentException("Configuration not found for $env");
}

private function getDefaultConfig(): array
{
return [
self::KEY_SERVICES => [],
self::KEY_SECRET_JSON => null,
];
}

private function getSecretConfig(string $file): array
{
return json_decode(file_get_contents($file), true);
}
}
60 changes: 60 additions & 0 deletions test/ConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
namespace F3\SilexConfig;

use PHPUnit\Framework\TestCase;
use Silex\Application;

class ConfigTest extends TestCase
{
public function testExampleProd()
{
$app = new Application();
(new Config(__DIR__ . '/example'))->configure($app, 'prod');

$this->assertEquals(
'prod',
$app['env'],
'Environment name is set correctly'
);
$this->assertFalse(
$app['debug'],
'Debug is disabled in prod'
);
$this->assertEquals(
'foo is common_foo, bar is prod_bar, password is s3(r37p455w0rd',
$app['hello'],
'Hello service returns the correct string'
);
}

public function testExampleDev()
{
$app = new Application();
(new Config(__DIR__ . '/example'))->configure($app, 'dev');

$this->assertEquals(
'dev',
$app['env'],
'Environment name is set correctly'
);
$this->assertTrue(
$app['debug'],
'Debug is enabled in dev'
);
$this->assertEquals(
'foo is dev_foo, bar is common_bar, password is dev_password',
$app['hello'],
'Hello service returns the correct string'
);
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Configuration not found for stage
*/
public function testInvalidEnvName()
{
$app = new Application();
(new Config(__DIR__ . '/example'))->configure($app, 'stage');
}
}
11 changes: 11 additions & 0 deletions test/example/common.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
return [
'services' => [ // services are executed in this order
'app' => __DIR__ . '/services/app.php',
'hello' => __DIR__ . '/services/hello.php',
],
'debug' => false,
'foo' => 'common_foo',
'bar' => 'common_bar',
'password' => '',
];
6 changes: 6 additions & 0 deletions test/example/dev.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
return array_replace_recursive(require __DIR__ . '/common.php', [ // inherit from common config
'debug' => true,
'foo' => 'dev_foo', // override this key
'password' => 'dev_password',
]);
5 changes: 5 additions & 0 deletions test/example/prod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
return array_replace_recursive(require __DIR__ . '/common.php', [ // inherit from common config
'secret_json' => __DIR__.'/secret.json',
'bar' => 'prod_bar', // override this key
]);
3 changes: 3 additions & 0 deletions test/example/secret.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"password": "s3(r37p455w0rd"
}
6 changes: 6 additions & 0 deletions test/example/services/app.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
// Generic application settings
return function (\Silex\Application $app, array $config, string $env) {
$app['env'] = $env;
$app['debug'] = $config['debug'];
};
7 changes: 7 additions & 0 deletions test/example/services/hello.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
// Configure the hello service
return function (\Silex\Application $app, array $config) {
$app['hello'] = function () use ($config) {
return "foo is {$config['foo']}, bar is {$config['bar']}, password is {$config['password']}";
};
};

0 comments on commit 2229c02

Please sign in to comment.