diff --git a/README.md b/README.md index c6cd7da..1faea6d 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,76 @@ -# silex-config -Simple multi-environment configuration for Silex with secret storage support +## Simple multi-environment configuration for Pimple/Silex with secret storage support ## Install ``` -composer install f3ath/silex-config +composer install f3ath/pimple-config ``` -## Usage +## Configuration structure +### Environment-specific config +A config is an `.php` file which returns an array: +```php + true, + 'foo' => [ + 'bar' => 'baz' + ] +]; +``` +To reduce duplication, here is some sort of "inheritance": +```php + false, +]); +``` +### Secret config +It is a healthy practice to store sensitive data like passwords outside of the repository. The simplest implementation +would be to store such files right on the server. These files may be edited directly, so they should not be php scripts, +since there is a good chance to accidentally remove the ` '/etc/my_application/secret.json', + 'debug' => false, +]); +``` +### Services +Pimple services are configured in the set of files in the `services` directory. In your configuration you define: +```php + [ + 'application' => __DIR__ . '/services/application.php', + 'storage' => __DIR__ . '/services/storage.php', + 'controllers' => __DIR__ . '/services/controllers.php', + ], +]; +``` +A service config is a php script which returns a special function: +```php +configure($app, $env); + $pimple = new \Pimple\Container(); + $env_name = 'prod'; + $config_root = '/path-to-config'; + $pimple->register(new \F3\PimpleConfig\Config($config_root, $env_name)); ``` -For more examples see the [unit test](test/ConfigTest.php). \ No newline at end of file +For more examples see the [unit test](test/ConfigTest.php). + +### Contribution +Please do! diff --git a/composer.json b/composer.json index 4fa76f3..c3b91c5 100644 --- a/composer.json +++ b/composer.json @@ -1,12 +1,7 @@ { - "name": "f3ath/silex-config", - "description": "Simple multi-environment configuration for Silex with secret storage support", + "name": "f3ath/pimple-config", + "description": "Simple multi-environment configuration for Pimple with secret storage support", "type": "library", - "require-dev": { - "silex/silex": "^2", - "phpunit/phpunit": "^6.0", - "squizlabs/php_codesniffer": "^2.8" - }, "license": "MIT", "authors": [ { @@ -14,18 +9,24 @@ "email": "karapetov@gmail.com" } ], - "require": {}, "config": { "platform": { "php": "7" } }, - "suggest": { - "silex/silex": "Silex Framework" + "require": { + "pimple/pimple": "^3" + }, + "require-dev": { + "phpunit/phpunit": "^6", + "squizlabs/php_codesniffer": "^2.8" }, "autoload": { "psr-4": { - "F3\\SilexConfig\\": "src/" + "F3\\PimpleConfig\\": "src/" } + }, + "scripts": { + "test": "vendor/bin/phpunit" } } diff --git a/src/Config.php b/src/Config.php index 9e500e1..76e8906 100644 --- a/src/Config.php +++ b/src/Config.php @@ -1,58 +1,61 @@ dir = $dir; + $this->dir = $config_root; + $this->env = $environment_name; } - public function configure(Application $app, string $env = 'prod') + public function register(Container $pimple) { - $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] - ); + $config = $this->getConfig(); + $config = $this->applySecretConfig($config); + $this->configureServices($pimple, $config); } - private function getConfig(string $env): array + protected function getConfig(): array { - $file = "{$this->dir}/{$env}.php"; + $file = "{$this->dir}/{$this->env}.php"; if (file_exists($file)) { return include $file; } - throw new \InvalidArgumentException("Configuration not found for $env"); + throw new \InvalidArgumentException("Configuration not found for {$this->env}"); + } + + protected function applySecretConfig($config): array + { + return array_replace_recursive( + $config, + $this->getSecretConfig($config) + ); } - private function getDefaultConfig(): array + protected function getSecretConfig(array $config): array { - return [ - self::KEY_SERVICES => [], - self::KEY_SECRET_JSON => null, - ]; + $secret_json = $config['secret_json'] ?? null; + if ($secret_json) { + return json_decode(file_get_contents($secret_json), true); + } + return []; } - private function getSecretConfig(string $file): array + protected function configureServices(Container $container, array $config) { - return json_decode(file_get_contents($file), true); + array_map( + function ($service) use ($container, $config) { + (require $service)($container, $config, $this->env); + }, + $config['services'] + ); } + } diff --git a/test/ConfigTest.php b/test/ConfigTest.php index 036d400..b22a93d 100644 --- a/test/ConfigTest.php +++ b/test/ConfigTest.php @@ -1,49 +1,49 @@ configure($app, 'prod'); + $container = new Container(); + $container->register(new Config(__DIR__ . '/example', 'prod')); $this->assertEquals( 'prod', - $app['env'], + $container['env'], 'Environment name is set correctly' ); $this->assertFalse( - $app['debug'], + $container['debug'], 'Debug is disabled in prod' ); $this->assertEquals( 'foo is common_foo, bar is prod_bar, password is s3(r37p455w0rd', - $app['hello'], + $container['hello'], 'Hello service returns the correct string' ); } public function testExampleDev() { - $app = new Application(); - (new Config(__DIR__ . '/example'))->configure($app, 'dev'); + $container = new Container(); + $container->register(new Config(__DIR__ . '/example', 'dev')); $this->assertEquals( 'dev', - $app['env'], + $container['env'], 'Environment name is set correctly' ); $this->assertTrue( - $app['debug'], + $container['debug'], 'Debug is enabled in dev' ); $this->assertEquals( 'foo is dev_foo, bar is common_bar, password is dev_password', - $app['hello'], + $container['hello'], 'Hello service returns the correct string' ); } @@ -54,7 +54,7 @@ public function testExampleDev() */ public function testInvalidEnvName() { - $app = new Application(); - (new Config(__DIR__ . '/example'))->configure($app, 'stage'); + $container = new Container(); + $container->register(new Config(__DIR__ . '/example', 'stage')); } } diff --git a/test/example/services/app.php b/test/example/services/app.php index 9479d17..2ee62a2 100644 --- a/test/example/services/app.php +++ b/test/example/services/app.php @@ -1,6 +1,6 @@