-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
242 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' => '', | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"password": "s3(r37p455w0rd" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']}"; | ||
}; | ||
}; |