diff --git a/CHANGELOG.md b/CHANGELOG.md index d4c3e1b..ecdf28c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index e557095..8cf1502 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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' => [ @@ -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`. @@ -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, @@ -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', ]]); ``` @@ -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 @@ -694,6 +755,9 @@ $vars->toDots(); # ] ``` +##### `getGlobals()` + +Gets the values defined in `_globals` ##### `set($key, $value)` Set a config key: diff --git a/src/Cache/CacheProvider.php b/src/Cache/CacheProvider.php index 840f8e7..db23c30 100644 --- a/src/Cache/CacheProvider.php +++ b/src/Cache/CacheProvider.php @@ -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 * @copyright Copyright (c) Miles Croxford * @license http://github.com/m1/vars/blob/master/LICENSE diff --git a/src/Loader/AbstractLoader.php b/src/Loader/AbstractLoader.php index 5991e63..d2eb47c 100644 --- a/src/Loader/AbstractLoader.php +++ b/src/Loader/AbstractLoader.php @@ -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 * @copyright Copyright (c) Miles Croxford * @license http://github.com/m1/vars/blob/master/LICENSE diff --git a/src/Loader/DirectoryLoader.php b/src/Loader/DirectoryLoader.php index f3fd6bd..cc460d2 100644 --- a/src/Loader/DirectoryLoader.php +++ b/src/Loader/DirectoryLoader.php @@ -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 * @copyright Copyright (c) Miles Croxford * @license http://github.com/m1/vars/blob/master/LICENSE diff --git a/src/Loader/EnvLoader.php b/src/Loader/EnvLoader.php index 1bdaa05..42f6844 100644 --- a/src/Loader/EnvLoader.php +++ b/src/Loader/EnvLoader.php @@ -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 * @copyright Copyright (c) Miles Croxford * @license http://github.com/m1/vars/blob/master/LICENSE diff --git a/src/Loader/IniLoader.php b/src/Loader/IniLoader.php index fbd0f43..4f0cec1 100644 --- a/src/Loader/IniLoader.php +++ b/src/Loader/IniLoader.php @@ -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 * @copyright Copyright (c) Miles Croxford * @license http://github.com/m1/vars/blob/master/LICENSE diff --git a/src/Loader/JsonLoader.php b/src/Loader/JsonLoader.php index a1f738e..13fd624 100644 --- a/src/Loader/JsonLoader.php +++ b/src/Loader/JsonLoader.php @@ -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 * @copyright Copyright (c) Miles Croxford * @license http://github.com/m1/vars/blob/master/LICENSE diff --git a/src/Loader/LoaderProvider.php b/src/Loader/LoaderProvider.php index e7c1d4f..25ba5e6 100644 --- a/src/Loader/LoaderProvider.php +++ b/src/Loader/LoaderProvider.php @@ -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 * @copyright Copyright (c) Miles Croxford * @license http://github.com/m1/vars/blob/master/LICENSE diff --git a/src/Loader/PhpLoader.php b/src/Loader/PhpLoader.php index 50ca968..e949c8b 100644 --- a/src/Loader/PhpLoader.php +++ b/src/Loader/PhpLoader.php @@ -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 * @copyright Copyright (c) Miles Croxford * @license http://github.com/m1/vars/blob/master/LICENSE diff --git a/src/Loader/TomlLoader.php b/src/Loader/TomlLoader.php index a57134e..29145ce 100644 --- a/src/Loader/TomlLoader.php +++ b/src/Loader/TomlLoader.php @@ -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 * @copyright Copyright (c) Miles Croxford * @license http://github.com/m1/vars/blob/master/LICENSE diff --git a/src/Loader/XmlLoader.php b/src/Loader/XmlLoader.php index f2c2d63..705ee56 100644 --- a/src/Loader/XmlLoader.php +++ b/src/Loader/XmlLoader.php @@ -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 * @copyright Copyright (c) Miles Croxford * @license http://github.com/m1/vars/blob/master/LICENSE diff --git a/src/Loader/YamlLoader.php b/src/Loader/YamlLoader.php index fec7706..77e3971 100644 --- a/src/Loader/YamlLoader.php +++ b/src/Loader/YamlLoader.php @@ -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 * @copyright Copyright (c) Miles Croxford * @license http://github.com/m1/vars/blob/master/LICENSE diff --git a/src/Provider/Silex/VarsServiceProvider.php b/src/Provider/Silex/VarsServiceProvider.php index f2200d7..f063e97 100644 --- a/src/Provider/Silex/VarsServiceProvider.php +++ b/src/Provider/Silex/VarsServiceProvider.php @@ -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 * @copyright Copyright (c) Miles Croxford * @license http://github.com/m1/vars/blob/master/LICENSE @@ -46,7 +46,8 @@ class VarsServiceProvider implements ServiceProviderInterface 'cache_path', 'cache_expire', 'loaders', - 'replacements' + 'replacements', + 'merge_globals', ); /** @@ -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; + } + }); } /** @@ -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; } diff --git a/src/Resource/AbstractResource.php b/src/Resource/AbstractResource.php index 181735e..d8cfcab 100644 --- a/src/Resource/AbstractResource.php +++ b/src/Resource/AbstractResource.php @@ -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 * @copyright Copyright (c) Miles Croxford * @license http://github.com/m1/vars/blob/master/LICENSE diff --git a/src/Resource/FileResource.php b/src/Resource/FileResource.php index 1552a18..27b9e06 100644 --- a/src/Resource/FileResource.php +++ b/src/Resource/FileResource.php @@ -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 * @copyright Copyright (c) Miles Croxford * @license http://github.com/m1/vars/blob/master/LICENSE diff --git a/src/Resource/ResourceProvider.php b/src/Resource/ResourceProvider.php index 7c450b1..8b447e5 100644 --- a/src/Resource/ResourceProvider.php +++ b/src/Resource/ResourceProvider.php @@ -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 * @copyright Copyright (c) Miles Croxford * @license http://github.com/m1/vars/blob/master/LICENSE diff --git a/src/Traits/FileTrait.php b/src/Traits/FileTrait.php index 2f08579..498f400 100644 --- a/src/Traits/FileTrait.php +++ b/src/Traits/FileTrait.php @@ -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 * @copyright Copyright (c) Miles Croxford * @license http://github.com/m1/vars/blob/master/LICENSE diff --git a/src/Traits/PathTrait.php b/src/Traits/PathTrait.php index a31bf62..ade2408 100644 --- a/src/Traits/PathTrait.php +++ b/src/Traits/PathTrait.php @@ -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 * @copyright Copyright (c) Miles Croxford * @license http://github.com/m1/vars/blob/master/LICENSE diff --git a/src/Traits/ResourceFlagsTrait.php b/src/Traits/ResourceFlagsTrait.php index fdbe5de..b59e0fe 100644 --- a/src/Traits/ResourceFlagsTrait.php +++ b/src/Traits/ResourceFlagsTrait.php @@ -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 * @copyright Copyright (c) Miles Croxford * @license http://github.com/m1/vars/blob/master/LICENSE diff --git a/src/Traits/TransformerTrait.php b/src/Traits/TransformerTrait.php index 90b9d17..d7f25e3 100644 --- a/src/Traits/TransformerTrait.php +++ b/src/Traits/TransformerTrait.php @@ -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 * @copyright Copyright (c) Miles Croxford * @license http://github.com/m1/vars/blob/master/LICENSE @@ -25,7 +25,6 @@ */ trait TransformerTrait { - /** * Makes it so the content is available in getenv() */ @@ -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; } diff --git a/src/Variables/ReplacementStore.php b/src/Variables/ReplacementStore.php index e0f911b..57d0b84 100644 --- a/src/Variables/ReplacementStore.php +++ b/src/Variables/ReplacementStore.php @@ -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 * @copyright Copyright (c) Miles Croxford * @license http://github.com/m1/vars/blob/master/LICENSE diff --git a/src/Variables/VariableProvider.php b/src/Variables/VariableProvider.php index 2969c85..750c061 100644 --- a/src/Variables/VariableProvider.php +++ b/src/Variables/VariableProvider.php @@ -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 * @copyright Copyright (c) Miles Croxford * @license http://github.com/m1/vars/blob/master/LICENSE diff --git a/src/Variables/VariableStore.php b/src/Variables/VariableStore.php index e2ceac0..b1c9475 100644 --- a/src/Variables/VariableStore.php +++ b/src/Variables/VariableStore.php @@ -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 * @copyright Copyright (c) Miles Croxford * @license http://github.com/m1/vars/blob/master/LICENSE diff --git a/src/Vars.php b/src/Vars.php index 05bdcdf..93fdc34 100644 --- a/src/Vars.php +++ b/src/Vars.php @@ -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 * @copyright Copyright (c) Miles Croxford * @license http://github.com/m1/vars/blob/master/LICENSE @@ -60,9 +60,17 @@ class Vars extends AbstractResource 'cache' => true, 'cache_path' => null, 'cache_expire' => 300, // 5 minutes - 'loaders' => array('env', 'ini', 'json', 'php', 'toml', 'yaml', 'xml',) + 'loaders' => array('env', 'ini', 'json', 'php', 'toml', 'yaml', 'xml',), + 'merge_globals' => true, ); + /** + * The global file variables + * + * @var array globals + */ + private $globals = array(); + /** * The loaderProvider for Vars supplies the file loaders and the extensions that are supported * @@ -113,8 +121,7 @@ public function __construct($resource, $options = array()) $this->loadFromCache(); } else { $resource->mergeParentContent(); - $this->content = $resource->getContent(); - + $this->content = $this->mergeGlobals($resource->getContent(), $options); $this->cache->setTime(time()); $this->cache->makeCache($this); } @@ -130,7 +137,9 @@ public function __construct($resource, $options = array()) private function parseOptions(array $options) { $parsed_options = array_merge($this->default_options, $options); - $parsed_options['loaders'] = (isset($options['loaders'])) ? $options['loaders'] : $this->default_options['loaders']; + $parsed_options['loaders'] = (isset($options['loaders'])) ? + $options['loaders'] : $this->default_options['loaders']; + return $parsed_options; } @@ -238,6 +247,30 @@ public function pathsLoadedCheck($resource) } } + + /** + * Gets the _globals from the file and merges them if merge_globals is true + * + * @param array $content The unparsed content + * @param array $options The options being used for Vars + * + * @return array $content The parsed content + */ + private function mergeGlobals($content, $options) + { + if (array_key_exists('_globals', $content)) { + $this->globals = $content['_globals']; + + if ($options['merge_globals']) { + $content = array_replace_recursive($content, $content['_globals']); + } + + unset($content['_globals']); + } + + return $content; + } + /** * Adds a resource to $this->resources * @@ -291,7 +324,7 @@ public function resourceImported($resource) * * @param string $resource The resource to search for * - * @return bool Returns the resource if found + * @return \M1\Vars\Resource\FileResource|bool Returns the resource if found */ public function getResource($resource) { @@ -314,6 +347,16 @@ public function getResources() return $this->resources; } + /** + * Returns the imported resources + * + * @return array The Vars imported resources + */ + public function getGlobals() + { + return $this->globals; + } + /** * Returns the CacheProvider if set * diff --git a/tests/VarsTest.php b/tests/VarsTest.php index 703c495..563fe77 100644 --- a/tests/VarsTest.php +++ b/tests/VarsTest.php @@ -1015,6 +1015,40 @@ public function testDotNotationVarsUnset() $this->assertFalse(isset($vars['test_key_1.test_key_2'])); } + public function testGlobalsMerge() + { + $vars = new Vars( + __DIR__ . '/mocks/globals/globals.yml', + array( + 'cache' => false, + ) + ); + + $this->assertEquals($vars->getContent(), $this->basic_array); + } + + public function testGlobalsMergeFalse() + { + $expected = array( + 'test_key_2' => 'test_value_2' + ); + + $expected_globals = array( + 'test_key_1' => 'test_value_1' + ); + + $vars = new Vars( + __DIR__ . '/mocks/globals/globals.yml', + array( + 'cache' => false, + 'merge_globals' => false + ) + ); + + $this->assertEquals($vars->getContent(), $expected); + $this->assertEquals($vars->getGlobals(), $expected_globals); + } + public function testBasicSilexServiceProvider() { $app = new \Silex\Application(); @@ -1064,6 +1098,21 @@ public function testOptionsSilexServiceProvider() unlink(sprintf('%s/vars/%s', $cache_path, $cache_name)); } + public function testSilexGlobalsMerge() + { + $app = new \Silex\Application(); + + $app->register(new \M1\Vars\Provider\Silex\VarsServiceProvider(__DIR__ . '/mocks/globals/globals.yml'), array( + 'vars.options' => array( + 'cache' => false, + ), + )); + $app['vars.merge'](); + + $this->assertEquals('test_value_1', $app['test_key_1']); + $this->assertEquals('test_value_2', $app['vars.test_key_2']); + } + public function testDebugSilexServiceProvider() { $resource = __DIR__ . '/mocks/provider/test_1.yml'; diff --git a/tests/mocks/globals/globals.yml b/tests/mocks/globals/globals.yml new file mode 100644 index 0000000..8d106e9 --- /dev/null +++ b/tests/mocks/globals/globals.yml @@ -0,0 +1,3 @@ +_globals: + test_key_1: test_value_1 +test_key_2: test_value_2 \ No newline at end of file