diff --git a/README.md b/README.md index 6e461f3..8cd6434 100644 --- a/README.md +++ b/README.md @@ -23,14 +23,16 @@ 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: @@ -51,6 +53,37 @@ 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..4bf242d 100644 --- a/src/DerAlex/Silex/YamlConfigServiceProvider.php +++ b/src/DerAlex/Silex/YamlConfigServiceProvider.php @@ -1,4 +1,5 @@ * * * @@ -26,37 +27,45 @@ 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; + $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 +78,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; + } +}