Skip to content

Commit

Permalink
1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
m1 committed Feb 5, 2016
1 parent 2e44721 commit 8d7757c
Show file tree
Hide file tree
Showing 27 changed files with 230 additions and 39 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

All Notable changes to `Vars` will be documented in this file

## 1.1.0 - 2016-02-05

### Added
- Support for `_globals`
- Support to merge globals into `$app` for Silex

## 1.0.0 - 2016-01-19

### Added
Expand Down
68 changes: 66 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Sometimes you're forced to use different formats for config files and one of Var
by supporting the most common config formats so you don't have to switch libraries to deal with the different formats.

Another aim is to support different frameworks so again you don't have to switch libraries when dealing with different frameworks.
Currently only supporting Silex with support for Laravel and Symfony to follow shortly.
Currently only supporting Silex using a service provider, support for Laravel and Symfony to follow shortly.

With a simple API and intuitive loading options, Vars tries to make config loading and providing as easy as possible for you.

Expand Down Expand Up @@ -293,6 +293,9 @@ $vars = new Vars(__DIR__.'/config/config.yml', [
'foo' => 'bar',
'foobar' => 'barfoo'
],
// Merge globals -- see globals section for more detail
'merge_globals' => true,
// The file loaders to load the configs -- see loader section for more detail
'loaders' => [
Expand Down Expand Up @@ -465,6 +468,38 @@ Will be accessed by:
getenv('test_key_1.test_key_2'); // value
```

#### Globals

`Globals` in `Vars` refer to variables defined as such:

```yaml
_globals:
test_key_1: test_value_1
```

Basically they are just encapsulated in an `_globals` array -- the use of these are so you can access them from `getGlobals()` from `Vars`

The default action is to merge them into the other file contents, so that:

```yaml
_globals:
test_key_1: test_value_1
test_key_2: test_value_2
```

Becomes:
```php
[
'test_key_1' => 'test_value_1',
'test_key_2' => 'test_value_2',
]
```
But you can override this by changing `merge_globals` to `false` via the options.

If this doesn't make sense then you probably won't need to use globals at all, but they're useful for working with framesworks
which encapsulate everything under say `$app` and you want to be able to access some key => values like so: `$app['test_key_1']`.
See the Silex provider section for more examples.

#### Caching

Vars automatically caches the resources for 5 minutes, you can turn this off by setting the `cache` option to `false`.
Expand Down Expand Up @@ -554,7 +589,7 @@ $vars = new Vars(__DIR__.'/config/config.yml', [

It's pretty straightforward to use this library with Silex, just register it when you register other service providers:
```php
$app->register(new M1\Vars\Provider\Silex\VarsServiceProvider(__DIR__.'/../../app/config/example.yml'), [
$app->register(new M1\Vars\Provider\Silex\VarsServiceProvider('example.yml'), [
'vars.path' => __DIR__.'/../../app/config/test/',
'vars.options' => [
'cache' => true,
Expand All @@ -567,6 +602,7 @@ $app->register(new M1\Vars\Provider\Silex\VarsServiceProvider(__DIR__.'/../../ap
'yml',
'json'
],
'merge_globals' => true,
'replacements' => __DIR__.'/../../app/config/replacements.json',
]]);
```
Expand All @@ -588,6 +624,31 @@ $app['vars']['test_key_1.test_key_2']; // value
$app['vars']['test_key_3']; // value
```

You can also merge globals into `$app` like so:

```yaml
# example.yml
_globals:
monolog.logfile: log.log
test_key_1: test_value_2
```

```php
$app->register(new M1\Vars\Provider\Silex\VarsServiceProvider('example.yml'));
// register monolog here and other service providers
$app['vars.merge']();
```

Note the `$app['vars.merge']()` -- This overrides the service provider defaults so in this example `monolog` will use
the log file defined in the vars config.

You must call `vars.merge` after you've called the service providers you provide config values for in your config.

You can also access `test_key_1` via `$app['vars.test_key_1']` and similary if you want, you can access globals like so
`$app['monolog.logfile']`.

## Public API

### Vars
Expand Down Expand Up @@ -694,6 +755,9 @@ $vars->toDots();
# ]
```

##### `getGlobals()`

Gets the values defined in `_globals`

##### `set($key, $value)`
Set a config key:
Expand Down
2 changes: 1 addition & 1 deletion src/Cache/CacheProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*
* @package m1/vars
* @version 1.0.0
* @version 1.1.0
* @author Miles Croxford <[email protected]>
* @copyright Copyright (c) Miles Croxford <[email protected]>
* @license http://github.com/m1/vars/blob/master/LICENSE
Expand Down
2 changes: 1 addition & 1 deletion src/Loader/AbstractLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*
* @package m1/vars
* @version 1.0.0
* @version 1.1.0
* @author Miles Croxford <[email protected]>
* @copyright Copyright (c) Miles Croxford <[email protected]>
* @license http://github.com/m1/vars/blob/master/LICENSE
Expand Down
2 changes: 1 addition & 1 deletion src/Loader/DirectoryLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*
* @package m1/vars
* @version 1.0.0
* @version 1.1.0
* @author Miles Croxford <[email protected]>
* @copyright Copyright (c) Miles Croxford <[email protected]>
* @license http://github.com/m1/vars/blob/master/LICENSE
Expand Down
2 changes: 1 addition & 1 deletion src/Loader/EnvLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*
* @package m1/vars
* @version 1.0.0
* @version 1.1.0
* @author Miles Croxford <[email protected]>
* @copyright Copyright (c) Miles Croxford <[email protected]>
* @license http://github.com/m1/vars/blob/master/LICENSE
Expand Down
2 changes: 1 addition & 1 deletion src/Loader/IniLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*
* @package m1/vars
* @version 1.0.0
* @version 1.1.0
* @author Miles Croxford <[email protected]>
* @copyright Copyright (c) Miles Croxford <[email protected]>
* @license http://github.com/m1/vars/blob/master/LICENSE
Expand Down
2 changes: 1 addition & 1 deletion src/Loader/JsonLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*
* @package m1/vars
* @version 1.0.0
* @version 1.1.0
* @author Miles Croxford <[email protected]>
* @copyright Copyright (c) Miles Croxford <[email protected]>
* @license http://github.com/m1/vars/blob/master/LICENSE
Expand Down
2 changes: 1 addition & 1 deletion src/Loader/LoaderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*
* @package m1/vars
* @version 1.0.0
* @version 1.1.0
* @author Miles Croxford <[email protected]>
* @copyright Copyright (c) Miles Croxford <[email protected]>
* @license http://github.com/m1/vars/blob/master/LICENSE
Expand Down
2 changes: 1 addition & 1 deletion src/Loader/PhpLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*
* @package m1/vars
* @version 1.0.0
* @version 1.1.0
* @author Miles Croxford <[email protected]>
* @copyright Copyright (c) Miles Croxford <[email protected]>
* @license http://github.com/m1/vars/blob/master/LICENSE
Expand Down
2 changes: 1 addition & 1 deletion src/Loader/TomlLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*
* @package m1/vars
* @version 1.0.0
* @version 1.1.0
* @author Miles Croxford <[email protected]>
* @copyright Copyright (c) Miles Croxford <[email protected]>
* @license http://github.com/m1/vars/blob/master/LICENSE
Expand Down
2 changes: 1 addition & 1 deletion src/Loader/XmlLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*
* @package m1/vars
* @version 1.0.0
* @version 1.1.0
* @author Miles Croxford <[email protected]>
* @copyright Copyright (c) Miles Croxford <[email protected]>
* @license http://github.com/m1/vars/blob/master/LICENSE
Expand Down
2 changes: 1 addition & 1 deletion src/Loader/YamlLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*
* @package m1/vars
* @version 1.0.0
* @version 1.1.0
* @author Miles Croxford <[email protected]>
* @copyright Copyright (c) Miles Croxford <[email protected]>
* @license http://github.com/m1/vars/blob/master/LICENSE
Expand Down
25 changes: 23 additions & 2 deletions src/Provider/Silex/VarsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*
* @package m1/vars
* @version 1.0.0
* @version 1.1.0
* @author Miles Croxford <[email protected]>
* @copyright Copyright (c) Miles Croxford <[email protected]>
* @license http://github.com/m1/vars/blob/master/LICENSE
Expand Down Expand Up @@ -46,7 +46,8 @@ class VarsServiceProvider implements ServiceProviderInterface
'cache_path',
'cache_expire',
'loaders',
'replacements'
'replacements',
'merge_globals',
);

/**
Expand All @@ -69,6 +70,22 @@ public function register(Application $app)
$app['vars'] = function ($app) {
return new Vars($this->entity, $this->createOptions($app));
};

$app['vars.merge'] = $app->protect(function () use ($app) {
static $initialized = false;
if ($initialized) {
return;
}
$initialized = true;

foreach ($app['vars']->getGlobals() as $key => $value) {
$app[$key] = $value;
}

foreach ($app['vars']->toDots(false) as $key => $value) {
$app['vars.'.$key] = $value;
}
});
}

/**
Expand All @@ -90,6 +107,10 @@ private function createOptions($app)
$options = $this->createKeyedOptions($options, $app['vars.options']);
}

if (!isset($options['merge_globals']) || is_null($options['merge_globals'])) {
$options['merge_globals'] = false;
}

if (isset($app['debug']) && $app['debug']) {
$options['cache'] = false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Resource/AbstractResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*
* @package m1/vars
* @version 1.0.0
* @version 1.1.0
* @author Miles Croxford <[email protected]>
* @copyright Copyright (c) Miles Croxford <[email protected]>
* @license http://github.com/m1/vars/blob/master/LICENSE
Expand Down
2 changes: 1 addition & 1 deletion src/Resource/FileResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*
* @package m1/vars
* @version 1.0.0
* @version 1.1.0
* @author Miles Croxford <[email protected]>
* @copyright Copyright (c) Miles Croxford <[email protected]>
* @license http://github.com/m1/vars/blob/master/LICENSE
Expand Down
2 changes: 1 addition & 1 deletion src/Resource/ResourceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*
* @package m1/vars
* @version 1.0.0
* @version 1.1.0
* @author Miles Croxford <[email protected]>
* @copyright Copyright (c) Miles Croxford <[email protected]>
* @license http://github.com/m1/vars/blob/master/LICENSE
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/FileTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*
* @package m1/vars
* @version 1.0.0
* @version 1.1.0
* @author Miles Croxford <[email protected]>
* @copyright Copyright (c) Miles Croxford <[email protected]>
* @license http://github.com/m1/vars/blob/master/LICENSE
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/PathTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*
* @package m1/vars
* @version 1.0.0
* @version 1.1.0
* @author Miles Croxford <[email protected]>
* @copyright Copyright (c) Miles Croxford <[email protected]>
* @license http://github.com/m1/vars/blob/master/LICENSE
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/ResourceFlagsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*
* @package m1/vars
* @version 1.0.0
* @version 1.1.0
* @author Miles Croxford <[email protected]>
* @copyright Copyright (c) Miles Croxford <[email protected]>
* @license http://github.com/m1/vars/blob/master/LICENSE
Expand Down
23 changes: 14 additions & 9 deletions src/Traits/TransformerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*
* @package m1/vars
* @version 1.0.0
* @version 1.1.0
* @author Miles Croxford <[email protected]>
* @copyright Copyright (c) Miles Croxford <[email protected]>
* @license http://github.com/m1/vars/blob/master/LICENSE
Expand All @@ -25,7 +25,6 @@
*/
trait TransformerTrait
{

/**
* Makes it so the content is available in getenv()
*/
Expand All @@ -43,28 +42,34 @@ public function toEnv()
/**
* Converts the array into a flat dot notation array
*
* @param bool $flatten_array Flatten arrays into none existent keys
*
* @return array The dot notation array
*/
public function toDots()
public function toDots($flatten_array = true)
{
return (!is_null($this->content)) ? $this->dotTransformer($this->content) : $this->content;
return (!is_null($this->content)) ? $this->dotTransformer($this->content, $flatten_array) : $this->content;
}

/**
* Converts the array into a flat dot notation array
*
* @param array $content The content array
* @param string $prefix The prefix for the key
* @param array $content The content array
* @param bool $flatten_array Flatten arrays into none existent keys
* @param string $prefix The prefix for the key
*
* @return array The dot notation array
*/
private function dotTransformer($content, $prefix = '')
private function dotTransformer($content, $flatten_array, $prefix = '')
{
$parsed = array();

foreach ($content as $arr_k => $arr_v) {
if (is_array($arr_v)) {
$parsed = array_merge($parsed, $this->dotTransformer($arr_v, $prefix.$arr_k."."));
if (!$flatten_array) {
$parsed[$prefix.$arr_k] = $arr_v;
}

$parsed = array_merge($parsed, $this->dotTransformer($arr_v, $flatten_array, $prefix.$arr_k."."));
} else {
$parsed[$prefix.$arr_k] = $arr_v;
}
Expand Down
Loading

0 comments on commit 8d7757c

Please sign in to comment.