From ca15228026dfec346db0b3e74e59dbb75c294deb Mon Sep 17 00:00:00 2001 From: m1 Date: Fri, 4 Sep 2015 16:55:37 +0100 Subject: [PATCH 1/5] Added option for replacement variables. Added docs for imports. Cleaned up a bit --- README.md | 37 +++++++++- .../Silex/YamlConfigServiceProvider.php | 68 ++++++++++++++----- 2 files changed, 85 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 6e461f3..57460a4 100644 --- a/README.md +++ b/README.md @@ -23,18 +23,20 @@ Usage -------------- Include following line of code somewhere in your initial Silex file (index.php or whatever): - $app->register(new DerAlex\Silex\YamlConfigServiceProvider(PATH_TO_CONFIG)); + $app->register(new DerAlex\Silex\YamlConfigServiceProvider(PATH_TO_CONFIG [, ARRAY_OF_REPLACEMENTS ])); Now you have access to all of your configuration variables through `$app['config']`. -Example +Examples --------------- +### Basic example + config.yml: database: - host: localhost + host: %mypath%/localhost user: myuser password: mypassword @@ -51,6 +53,35 @@ index.php: echo $app['config']['database']['host']; ... + +### Import example + +You can import other config files into one file, for example if you had `parameters.yml`, `security.yml`, you could import these with one call to `config.yml` + +config.yml: + imports: + - { resource: parameters.yml } + - { resource: security.yml } + +### Replacement variable example + +You can pass a array of variables to be replaced as the second parameter: + +parameters.yml: + + twig.path: + - %basepath%/app/views, + - %basepath%/vendor/example/views + +index.php: + + ... + $app->register(new DerAlex\Silex\YamlConfigServiceProvider('config.yml', [ + 'basepath' => realpath(__DIR__.'/../..') + ])); + ... + + License ---------------- Copyright (c) 2013 Alexander Kluth diff --git a/src/DerAlex/Silex/YamlConfigServiceProvider.php b/src/DerAlex/Silex/YamlConfigServiceProvider.php index 23055a0..6ee19a2 100644 --- a/src/DerAlex/Silex/YamlConfigServiceProvider.php +++ b/src/DerAlex/Silex/YamlConfigServiceProvider.php @@ -1,4 +1,5 @@ * * * @@ -26,37 +27,47 @@ use Silex\ServiceProviderInterface; use Symfony\Component\Yaml\Yaml; - class YamlConfigServiceProvider implements ServiceProviderInterface { protected $file; + protected $variables; - public function __construct($file) { + public function __construct($file, array $variables = []) + { $this->file = $file; + + if ($variables) { + foreach ($variables as $variable => $value) { + $this->variables['%' . $variable . '%'] = $value; + } + } } - public function register(Application $app) { + public function register(Application $app) + { $config = Yaml::parse(file_get_contents($this->file)); if (is_array($config)) { $this->importSearch($config, $app); if (isset($app['config']) && is_array($app['config'])) { - $app['config'] = array_replace_recursive($app['config'], $config); - } else { - $app['config'] = $config; + var_dump($app['config']); + + $config = array_replace_recursive($app['config'], $config); } - } + if ($this->variables) { + foreach ($config as $k => $v) { + $config[$k] = $this->replaceVariables($v); + } + } + + $app['config'] = $config; + } } - /** - * Looks for import directives.. - * - * @param array $config - * The result of Yaml::parse(). - */ - public function importSearch(&$config, $app) { + public function importSearch(&$config, $app) + { foreach ($config as $key => $value) { if ($key == 'imports') { foreach ($value as $resource) { @@ -69,11 +80,34 @@ public function importSearch(&$config, $app) { } } - public function boot(Application $app) { + public function replaceVariables($value) + { + if (is_array($value)) { + foreach ($value as $k => $v) { + $value[$k] = $this->replaceVariables($v); + } + + return $value; + } + + if (is_string($value)) { + return strtr($value, $this->variables); + } + + return $value; } - public function getConfigFile() { + public function boot(Application $app) + { + } + + public function getConfigFile() + { return $this->file; } -} + public function getVariables() + { + return $this->variables; + } +} From b4ebf1259a16ff25f0eceff4aecb5ce75caec5c7 Mon Sep 17 00:00:00 2001 From: m1 Date: Fri, 4 Sep 2015 16:57:29 +0100 Subject: [PATCH 2/5] Fixed spacing in README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 57460a4..f4de5be 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,7 @@ config.yml: - { resource: parameters.yml } - { resource: security.yml } + ### Replacement variable example You can pass a array of variables to be replaced as the second parameter: From 4a2989523defdec4f1a37219939c5bf163a1cce0 Mon Sep 17 00:00:00 2001 From: m1 Date: Fri, 4 Sep 2015 16:58:08 +0100 Subject: [PATCH 3/5] Removed error from README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f4de5be..0c5c66f 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ Examples config.yml: database: - host: %mypath%/localhost + host: localhost user: myuser password: mypassword From 81f0aa590437bc654ab3da7f489c4a6e3d52e52f Mon Sep 17 00:00:00 2001 From: m1 Date: Fri, 4 Sep 2015 16:59:27 +0100 Subject: [PATCH 4/5] Fixed code in README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 0c5c66f..8cd6434 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,7 @@ index.php: You can import other config files into one file, for example if you had `parameters.yml`, `security.yml`, you could import these with one call to `config.yml` config.yml: + imports: - { resource: parameters.yml } - { resource: security.yml } From bb01fa44ada54f6e2cbe81d83c15b8a2fca7e159 Mon Sep 17 00:00:00 2001 From: m1 Date: Fri, 4 Sep 2015 17:00:28 +0100 Subject: [PATCH 5/5] Remove debug message --- src/DerAlex/Silex/YamlConfigServiceProvider.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/DerAlex/Silex/YamlConfigServiceProvider.php b/src/DerAlex/Silex/YamlConfigServiceProvider.php index 6ee19a2..4bf242d 100644 --- a/src/DerAlex/Silex/YamlConfigServiceProvider.php +++ b/src/DerAlex/Silex/YamlConfigServiceProvider.php @@ -51,8 +51,6 @@ public function register(Application $app) $this->importSearch($config, $app); if (isset($app['config']) && is_array($app['config'])) { - var_dump($app['config']); - $config = array_replace_recursive($app['config'], $config); }