.gitignore000064400000000025146412134720006535 0ustar00composer.phar vendor .travis.yml000064400000000315146412134720006660 0ustar00language: php php: - 5.3 - 5.4 - 5.5 - 5.6 - hhvm before_script: - travis_retry composer self-update - travis_retry composer install --prefer-source --no-interaction --dev script: phpunit README.md000064400000005567146412134720006044 0ustar00![BuildStatus](https://travis-ci.org/clagiordano/weblibs-configmanager.svg?branch=master) ![License](https://img.shields.io/github/license/clagiordano/weblibs-configmanager.svg) # weblibs-configmanager weblibs-configmanager is a tool library for easily read and access to php config array file and direct read/write configuration file / object. [![SensioLabsInsight](https://insight.sensiolabs.com/projects/54c4e80c-ff15-4235-8bec-a4c71bbe3ba5/big.png)](https://insight.sensiolabs.com/projects/54c4e80c-ff15-4235-8bec-a4c71bbe3ba5) ## Why use weblibs-configmanager ? The purpose of this project is to propose a simple and lightweight library to manage php hierarchical configuration files. ## Installation The recommended way to install weblibs-configmanager is through [Composer](https://getcomposer.org). ```bash composer require clagiordano/weblibs-configmanager ``` ## Usage examples ### Write a sample config file like this ``` 'app_name', 'db' => array ( 'host' => 'localhost', 'user' => 'sample_user', 'pass' => 'sample_pass', 'port' => 3306, ), 'other' => array ( 'multi' => array ( 'deep' => array ( 'nested' => 'config_value', ), ), ), ); ``` ### Instance ConfigManager object ```php use clagiordano\weblibs\configmanager\ConfigManager; /** * Instance object to read argument file */ $config = new ConfigManager("configfile.php"); ``` ### Check if a value exists into config file ```php /** * Check if a value exists into config file */ $value = $config->existValue('app'); ``` ### Read a simple element from config file ```php /** * Read a simple element from config file */ $value = $config->getValue('app'); ``` ### Access to a nested element from config ```php /** * Access to a nested element from config */ $nestedValue = $config->getValue('other.multi.deep.nested'); ``` ### Change config value at runtime ```php /** * Change config value at runtime */ $this->config->setValue('other.multi.deep.nested', "SUPERNESTED"); ``` ### Save config file with original name (OVERWRITE) ```php /** * Save config file with original name (OVERWRITE) */ $this->config->saveConfigFile(); ``` ### Or save config file with a different name ```php /** * Save config file with original name (OVERWRITE) */ $this->config->saveConfigFile('/new/file/name/or/path/test.php'); ``` ### Optionally you can also reload config file from disk after save ```php /** * Optionally you can also reload config file from disk after save */ $this->config->saveConfigFile('/new/file/name/or/path/test.php', true); ``` ### Load another configuration file without reinstance ConfigManager ```php /** * Load another configuration file without reinstance ConfigManager */ $this->config->loadConfig('another_config_file.php'); ``` ## Legal *Copyright (C) Claudio Giordano * composer.json000064400000001654146412134720007300 0ustar00{ "name": "clagiordano/weblibs-configmanager", "description": "weblibs-configmanager is a tool library for easily read and access to php config array file and direct read/write configuration file / object", "type": "library", "license": "LGPL-3.0", "keywords": ["clagiordano", "weblibs", "configuration", "manager", "tool"], "authors": [ { "name": "Claudio Giordano", "email": "claudio.giordano@autistici.org", "role": "Developer" } ], "autoload": { "psr-4": { "clagiordano\\weblibs\\configmanager\\": "src/" } }, "require": { "php": ">=5.3" }, "require-dev": { "phpunit/phpunit": "*" }, "autoload-dev": { "psr-4": { "clagiordano\\weblibs\\configmanager\\tests\\": "tests/", "clagiordano\\weblibs\\configmanager\\testdata\\": "testdata/" } } } phpunit.xml000064400000001132146412134720006756 0ustar00 ./tests/ src/ConfigManager.php000064400000007165146412134720010561 0ustar00loadConfig($configFilePath); } /** * Load config data from file and store it into internal property * * @param null|string $configFilePath * * @return ConfigManager */ public function loadConfig($configFilePath = null) { if (!is_null($configFilePath)) { $this->configFilePath = $configFilePath; if (file_exists($configFilePath)) { $this->configData = require $configFilePath; } } return $this; } /** * Prepare and write config file on disk * * @param null|string $configFilePath * @param bool $autoReloadConfig * * @return ConfigManager * @throws \RuntimeException */ public function saveConfigFile($configFilePath = null, $autoReloadConfig = false) { if (is_null($configFilePath)) { $configFilePath = $this->configFilePath; } $configFileContent = "configData, true); $configFileContent .= ";\n\n"; try { file_put_contents($configFilePath, $configFileContent); } catch (\Exception $exc) { throw new \RuntimeException( __METHOD__ . ": Failed to write config file to path '{$configFilePath}'" ); } if ($autoReloadConfig) { $this->loadConfig($configFilePath); } return $this; } /** * Get value pointer from config for get/set value * * @param string $configPath * * @return mixed */ private function & getValuePointer($configPath) { $configData =& $this->configData; $parts = explode('.', $configPath); $length = count($parts); for ($i = 0; $i < $length; $i++) { if (!isset($configData[ $parts[ $i ] ])) { $configData[ $parts[ $i ] ] = ($i === $length) ? [] : null; } $configData = &$configData[ $parts[ $i ] ]; } return $configData; } /** * Get value from config data throught keyValue path * * @param string $configPath * @param mixed $defaultValue * * @return mixed */ public function getValue($configPath, $defaultValue = null) { $stored = $this->getValuePointer($configPath); return is_null($stored) ? $defaultValue : $stored; } /** * Check if exist required config for keyValue * * @param string $keyValue * * @return mixed */ public function existValue($keyValue) { return !is_null($this->getValue($keyValue)); } /** * Set value in config path * * @param string $configPath * @param mixed $newValue * * @return ConfigManager */ public function setValue($configPath, $newValue) { $configData = &$this->getValuePointer($configPath); $configData = $newValue; return $this; } } tests/ConfigManagerTest.php000064400000005417146412134720011772 0ustar00config = new ConfigManager("TestConfigData.php"); $this->assertInstanceOf('clagiordano\weblibs\configmanager\ConfigManager', $this->config); $this->assertFileExists($this->configFile); $this->config->loadConfig($this->configFile); } public function testBasicUsage() { $this->assertNotNull( $this->config->getValue('app') ); } public function testFastUsage() { $this->assertNotNull( $this->config->getValue('app') ); } public function testFastInvalidKey() { $this->assertNull( $this->config->getValue('invalidKey') ); } public function testFastInvalidKeyWithDefault() { $this->assertEquals( $this->config->getValue('invalidKey', 'defaultValue'), 'defaultValue' ); } public function testFastNestedConfig() { $this->assertNotNull( $this->config->getValue('other.multi.deep.nested') ); } public function testCheckExistConfig() { $this->assertTrue( $this->config->existValue('other.multi.deep.nested') ); } public function testCheckNotExistConfig() { $this->assertFalse( $this->config->existValue('invalid.config.path') ); } public function testSetValue() { $this->config->setValue('other.multi.deep.nested', __FUNCTION__); $this->assertEquals( $this->config->getValue('other.multi.deep.nested'), __FUNCTION__ ); } public function testFailedSaveConfig() { $this->setExpectedException('Exception'); $this->config->saveConfigFile('/invalid/path'); } public function testSuccessSaveConfigOnTempAndReload() { $this->config->setValue('other.multi.deep.nested', "SUPERNESTED"); $this->config->saveConfigFile("/tmp/testconfig.php", true); $this->assertEquals( $this->config->getValue('other.multi.deep.nested'), "SUPERNESTED" ); } public function testOverwriteSameConfigFile() { $this->config->saveConfigFile(); } public function testFailWriteConfig() { $this->setExpectedException('\RuntimeException'); $this->config->saveConfigFile('/invalid/path/test.php'); } }testsdata/sample_config_data.php000064400000000500146412134720013047 0ustar00 'app_name', 'db' => array ( 'host' => 'localhost', 'user' => 'sample_user', 'pass' => 'sample_pass', 'port' => 3306, ), 'other' => array ( 'multi' => array ( 'deep' => array ( 'nested' => 'config_value', ), ), ), );