Skip to content
This repository was archived by the owner on Jan 3, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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 <[email protected]>
Expand Down
66 changes: 49 additions & 17 deletions src/DerAlex/Silex/YamlConfigServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/******************************************************************************
* Copyright (c) 2013 Alexander Kluth <[email protected]> *
* *
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}
}