diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..b7f19c8 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,12 @@ +root = true + +[*] +charset = utf-8 +end_of_line = lf +indent_style = space +indent_size = 4 +insert_final_newline = true +trim_trailing_whitespace = true + +[*.json] +indent_size = 2 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cad3511 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +vendor + +composer.lock +coverage.xml +.php_cs.cache diff --git a/.php_cs b/.php_cs new file mode 100644 index 0000000..c712efe --- /dev/null +++ b/.php_cs @@ -0,0 +1,26 @@ + + +This source file is subject to the MIT license that is bundled +with this source code in the file LICENSE.md. +EOF; + +use PhpCsFixer\Config; +use PhpCsFixer\Finder; + +return Config::create() + ->setFinder( + Finder::create() + ->in(__DIR__ . '/src') + ->in(__DIR__ . '/tests') + ) + ->setRules(array( + '@PSR2' => true, + 'header_comment' => array('header' => $header), + 'array_syntax' => true, + )) + ->setUsingCache(true); diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..4126301 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,24 @@ +after_success: bash <(curl -s https://codecov.io/bash) + +before_install: travis_retry composer self-update + +branches: + only: master + +cache: + directories: $HOME/.composer/cache + +install: travis_retry composer install --dev --no-interaction --prefer-dist + +language: php + +php: + - 5.3 + - 5.4 + - 5.5 + - 5.6 + - 7.0 + - 7.1 + - hhvm + +script: vendor/bin/phpunit diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..3fb5cfc --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,7 @@ +Copyright (c) 2016 Vaibhav Pandey + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..71b2d6f --- /dev/null +++ b/README.md @@ -0,0 +1,81 @@ +# vaibhavpandeyvpz/kunfig +Helper library to easily merge & use multiple configuration files. + +[![Build status][build-status-image]][build-status-url] +[![Code Coverage][code-coverage-image]][code-coverage-url] +[![Latest Version][latest-version-image]][latest-version-url] +[![Downloads][downloads-image]][downloads-url] +[![PHP Version][php-version-image]][php-version-url] +[![License][license-image]][license-url] + +[![SensioLabsInsight][insights-image]][insights-url] + +Install +------- +```bash +composer require vaibhavpandeyvpz/kunfig +``` + +Usage +----- +```php + array( + 'host' => 'localhost', + 'port' => 3306, + 'charset' => 'utf8mb4', + ), + 'debug' => false, +)); + +// Get a value +$host = $config->database->host; + +// Set a value +$config->database->host = 'xxxxxxxxxxxxx.xxxxxxxxxxxxx.us-west-2.rds.amazonaws.com'; + +$override = new Kunfig\Config(array( + 'database' => array( + 'name' => 'test', + 'user' => 'root', + 'password' => null, + ), +)); + +/** + * @desc You can mix two Kunfig\ConfigInterface; the latter one + * will override values in the original one. + */ +$config->mix($override); + +$pdo = new PDO( + "mysql:host={$config->database->host}:{$config->database->port};dbname={$config->database->name}", + $config->database->user, + $config->database->password +); +``` + +License +------- +See [LICENSE.md][license-url] file. + +[build-status-image]: https://img.shields.io/travis/vaibhavpandeyvpz/kunfig.svg?style=flat-square +[build-status-url]: https://travis-ci.org/vaibhavpandeyvpz/kunfig +[code-coverage-image]: https://img.shields.io/codecov/c/github/vaibhavpandeyvpz/kunfig.svg?style=flat-square +[code-coverage-url]: https://codecov.io/gh/vaibhavpandeyvpz/kunfig +[latest-version-image]: https://img.shields.io/github/release/vaibhavpandeyvpz/kunfig.svg?style=flat-square +[latest-version-url]: https://github.com/vaibhavpandeyvpz/kunfig/releases +[downloads-image]: https://img.shields.io/packagist/dt/vaibhavpandeyvpz/kunfig.svg?style=flat-square +[downloads-url]: https://packagist.org/packages/vaibhavpandeyvpz/kunfig +[php-version-image]: http://img.shields.io/badge/php-5.3+-8892be.svg?style=flat-square +[php-version-url]: https://packagist.org/packages/vaibhavpandeyvpz/kunfig +[license-image]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square +[license-url]: LICENSE.md +[insights-image]: https://insight.sensiolabs.com/projects/2606a5ca-43c2-4db9-ba3f-007e13e31362/small.png +[insights-url]: https://insight.sensiolabs.com/projects/2606a5ca-43c2-4db9-ba3f-007e13e31362 diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..edcefbd --- /dev/null +++ b/composer.json @@ -0,0 +1,28 @@ +{ + "authors": [{ + "name": "Vaibhav Pandey", + "email": "contact@vaibhavpandey.com" + }], + "autoload": { + "psr-4": { + "Kunfig\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "Kunfig\\": "src/" + } + }, + "description": "Helper library to easily merge & use multiple configuration files.", + "homepage": "http://vaibhavpandeyvpz.github.io/kunfig", + "keywords": ["php", "multiple", "configuration", "manager"], + "license": "MIT", + "name" : "vaibhavpandeyvpz/kunfig", + "require": { + "php": "^5.3 || ^7.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.0 || ^5.0" + }, + "type": "library" +} diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..a0ff39f --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,15 @@ + + + + src + + + + + + + + tests + + + diff --git a/src/Config.php b/src/Config.php new file mode 100644 index 0000000..4254172 --- /dev/null +++ b/src/Config.php @@ -0,0 +1,129 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE.md. + */ + +namespace Kunfig; + +/** + * Class Config + * @package Kunfig + */ +class Config extends ConfigAbstract +{ + /** + * @var array + */ + protected $values = array(); + + /** + * Config constructor. + * @param array $values + */ + public function __construct(array $values = array()) + { + foreach ($values as $key => $value) { + $this->set($key, $value); + } + } + + /** + * {@inheritdoc} + */ + public function all() + { + $values = array(); + foreach ($this->values as $key => $value) { + if ($value instanceof ConfigInterface) { + $values[$key] = $value->all(); + } else { + $values[$key] = $value; + } + } + return $values; + } + + /** + * {@inheritdoc} + */ + public function count() + { + return count($this->values); + } + + /** + * {@inheritdoc} + */ + public function has($key) + { + return array_key_exists($key, $this->values); + } + + /** + * {@inheritdoc} + */ + public function get($key, $fallback = null) + { + return $this->has($key) ? $this->values[$key] : $fallback; + } + + /** + * {@inheritdoc} + */ + public function getIterator() + { + return new \ArrayIterator($this->values); + } + + /** + * {@inheritdoc} + */ + public function mix(ConfigInterface $config) + { + foreach ($config as $key => $value) { + if ($this->has($key)) { + $preset = $this->get($key); + if (($preset instanceof ConfigInterface) && ($value instanceof ConfigInterface)) { + $preset->mix($value); + continue; + } + } + $this->set($key, $value); + } + } + + /** + * {@inheritdoc} + */ + public function set($key, $value) + { + $this->values[$key] = is_array($value) ? new self($value) : $value; + } + + /** + * {@inheritdoc} + */ + public function remove($key) + { + unset($this->values[$key]); + } + + // + + /** + * @param array $data + * @return static + */ + public static function __set_state(array $data = array()) + { + return new self($data); + } + + // +} diff --git a/src/ConfigAbstract.php b/src/ConfigAbstract.php new file mode 100644 index 0000000..5798160 --- /dev/null +++ b/src/ConfigAbstract.php @@ -0,0 +1,94 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE.md. + */ + +namespace Kunfig; + +/** + * Class ConfigAbstract + * @package Kunfig + */ +abstract class ConfigAbstract implements ConfigInterface +{ + // + + /** + * {@inheritdoc} + */ + public function offsetExists($offset) + { + return $this->has($offset); + } + + /** + * {@inheritdoc} + */ + public function offsetGet($offset) + { + return $this->get($offset); + } + + /** + * {@inheritdoc} + */ + public function offsetSet($offset, $value) + { + $this->set($offset, $value); + } + + /** + * {@inheritdoc} + */ + public function offsetUnset($offset) + { + $this->remove($offset); + } + + // + + // + + /** + * @param string $name + * @return mixed + */ + public function __get($name) + { + return $this->get($name); + } + + /** + * @param string $name + * @return bool + */ + public function __isset($name) + { + return $this->has($name); + } + + /** + * @param string $name + * @param mixed $value + */ + public function __set($name, $value) + { + $this->set($name, $value); + } + + /** + * @param string $name + */ + public function __unset($name) + { + return $this->remove($name); + } + + // +} diff --git a/src/ConfigInterface.php b/src/ConfigInterface.php new file mode 100644 index 0000000..4f53cd2 --- /dev/null +++ b/src/ConfigInterface.php @@ -0,0 +1,53 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE.md. + */ + +namespace Kunfig; + +/** + * Interface ConfigInterface + * @package Kunfig + */ +interface ConfigInterface extends \ArrayAccess, \Countable, \IteratorAggregate +{ + /** + * @return array + */ + public function all(); + + /** + * @param string $key + * @return boolean + */ + public function has($key); + + /** + * @param string $key + * @param mixed $fallback + * @return ConfigInterface|mixed + */ + public function get($key, $fallback = null); + + /** + * @param string $key + * @param mixed $value + */ + public function set($key, $value); + + /** + * @param string $key + */ + public function remove($key); + + /** + * @param ConfigInterface $config + */ + public function mix(ConfigInterface $config); +} diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php new file mode 100644 index 0000000..bd3f86c --- /dev/null +++ b/tests/ConfigTest.php @@ -0,0 +1,125 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE.md. + */ + +namespace Kunfig; + +/** + * Class ConfigTest + * @package Kunfig\Tests + */ +class ConfigTest extends \PHPUnit_Framework_TestCase +{ + public function testAll() + { + $config = new Config(array( + $key1 = 'somekey' => $value1 = 'somevalue', + $key2 = 'otherkey' => $value2 = 'othervalue', + $key3 = 'lastkey' => array('hi' => 'namaste'), + )); + $values = $config->all(); + $this->assertInternalType('array', $values); + $this->assertArrayHasKey($key1, $values); + $this->assertEquals($value1, $values[$key1]); + $this->assertArrayHasKey($key2, $values); + $this->assertEquals($value2, $values[$key2]); + $this->assertArrayHasKey($key3, $values); + $this->assertInternalType('array', $values[$key3]); + } + + public function testCount() + { + $config = new Config(array( + 'somekey' => 'somevalue', + 'otherkey' => 'othervalue', + )); + $this->assertCount(2, $config); + } + + public function testGet() + { + $config = new Config(array($key = 'somekey' => $value = 'somevalue')); + $this->assertEquals($value, $config->get($key)); + } + + public function testHas() + { + $config = new Config(array($key = 'somekey' => $value = 'somevalue')); + $this->assertTrue($config->has($key)); + $this->assertFalse($config->has('otherkey')); + } + + public function testMix() + { + $config = new Config(array( + 'somekey' => 'somevalue', + 'otherkey' => 'othervalue', + 'arraykey' => array('arrayvalue'), + 'mixedkey' => array('mixedvalue', 123), + )); + $override = new Config(array( + 'somekey' => 'othervalue', + 'otherkey' => array('arrayvalue'), + 'arraykey' => 'somevalue', + 'mixedkey' => array('mixedvalue', 1234), + )); + $config->mix($override); + $this->assertEquals('othervalue', $config->get('somekey')); + $this->assertInstanceOf('Kunfig\\ConfigInterface', $config->get('otherkey')); + $this->assertEquals('somevalue', $config->get('arraykey')); + $this->assertInstanceOf('Kunfig\\ConfigInterface', $config->get('mixedkey')); + } + + public function testSet() + { + $config = new Config(); + $this->assertFalse($config->has('somekey')); + $config->set($key = 'somekey', $value = 'somevalue'); + $this->assertTrue($config->has('somekey')); + $this->assertEquals($value, $config->get('somekey')); + } + + public function testRemove() + { + $config = new Config(array($key = 'somekey' => $value = 'somevalue')); + $this->assertTrue($config->has($key)); + $config->remove($key); + $this->assertFalse($config->has($key)); + } + + public function testSetState() + { + $config = Config::__set_state(array($key = 'somekey' => $value = 'somevalue')); + $this->assertTrue($config->has($key)); + $this->assertEquals($value, $config->get($key)); + } + + public function testArrayAccess() + { + $config = new Config(array($key = 'somekey' => $value = 'somevalue')); + $this->assertTrue(isset($config[$key])); + $this->assertEquals($value, $config[$key]); + $config[$key] = $value = 'othervalue'; + $this->assertEquals($value, $config[$key]); + unset($config[$key]); + $this->assertFalse(isset($config[$key])); + } + + public function testPropertyAccess() + { + $config = new Config(array($key = 'somekey' => $value = 'somevalue')); + $this->assertTrue(isset($config->{$key})); + $this->assertEquals($value, $config->{$key}); + $config->{$key} = $value = 'othervalue'; + $this->assertEquals($value, $config->{$key}); + unset($config->{$key}); + $this->assertFalse(isset($config->{$key})); + } +}