初始化代码
This commit is contained in:
2
vendor/clagiordano/weblibs-configmanager/.gitignore
vendored
Normal file
2
vendor/clagiordano/weblibs-configmanager/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
composer.phar
|
||||
vendor
|
||||
17
vendor/clagiordano/weblibs-configmanager/.travis.yml
vendored
Normal file
17
vendor/clagiordano/weblibs-configmanager/.travis.yml
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
language: php
|
||||
|
||||
php:
|
||||
- 5.4
|
||||
- 5.5
|
||||
- 5.6
|
||||
- 7.0
|
||||
- 7.1
|
||||
- 7.2
|
||||
- 7.3
|
||||
- hhvm
|
||||
|
||||
before_script:
|
||||
- travis_retry composer self-update
|
||||
- travis_retry composer update --prefer-source --no-interaction --dev
|
||||
|
||||
script: phpunit
|
||||
130
vendor/clagiordano/weblibs-configmanager/README.md
vendored
Normal file
130
vendor/clagiordano/weblibs-configmanager/README.md
vendored
Normal file
@@ -0,0 +1,130 @@
|
||||
 
|
||||
|
||||
# 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.
|
||||
|
||||
[](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
|
||||
```
|
||||
<?php
|
||||
|
||||
return array (
|
||||
'app' => '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 <claudio.giordano@autistici.org>*
|
||||
36
vendor/clagiordano/weblibs-configmanager/composer.json
vendored
Normal file
36
vendor/clagiordano/weblibs-configmanager/composer.json
vendored
Normal file
@@ -0,0 +1,36 @@
|
||||
{
|
||||
"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-or-later",
|
||||
"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.4"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^4.8",
|
||||
"clagiordano/phpunit-result-printer": "^1"
|
||||
},
|
||||
"autoload-dev": {
|
||||
"psr-4": {
|
||||
"clagiordano\\weblibs\\configmanager\\tests\\": "tests/",
|
||||
"clagiordano\\weblibs\\configmanager\\testdata\\": "testdata/"
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
"test": "./vendor/bin/phpunit --no-coverage",
|
||||
"coverage": "./vendor/bin/phpunit"
|
||||
}
|
||||
}
|
||||
1235
vendor/clagiordano/weblibs-configmanager/composer.lock
generated
vendored
Normal file
1235
vendor/clagiordano/weblibs-configmanager/composer.lock
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
19
vendor/clagiordano/weblibs-configmanager/phpunit.xml
vendored
Normal file
19
vendor/clagiordano/weblibs-configmanager/phpunit.xml
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<phpunit backupGlobals="false"
|
||||
backupStaticAttributes="false"
|
||||
bootstrap="vendor/autoload.php"
|
||||
colors="true"
|
||||
convertErrorsToExceptions="true"
|
||||
convertNoticesToExceptions="true"
|
||||
convertWarningsToExceptions="true"
|
||||
processIsolation="false"
|
||||
stopOnFailure="false"
|
||||
verbose="true"
|
||||
printerClass="clagiordano\PhpunitResultPrinter\ResultPrinter"
|
||||
>
|
||||
<testsuites>
|
||||
<testsuite name="Package Test Suite">
|
||||
<directory suffix=".php">./tests/</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
</phpunit>
|
||||
148
vendor/clagiordano/weblibs-configmanager/src/ConfigManager.php
vendored
Normal file
148
vendor/clagiordano/weblibs-configmanager/src/ConfigManager.php
vendored
Normal file
@@ -0,0 +1,148 @@
|
||||
<?php
|
||||
|
||||
namespace clagiordano\weblibs\configmanager;
|
||||
|
||||
/**
|
||||
* Class ConfigManager, class for easily read and access to php config array file.
|
||||
* @package clagiordano\weblibs\configmanager
|
||||
*/
|
||||
class ConfigManager
|
||||
{
|
||||
/** @var array $configData */
|
||||
private $configData = null;
|
||||
/** @var string $configFilePath */
|
||||
private $configFilePath = null;
|
||||
|
||||
/**
|
||||
* Create config object, optionally automatic load config
|
||||
* from argument $configFilePath
|
||||
*
|
||||
* @param string $configFilePath
|
||||
* @return ConfigManager
|
||||
*/
|
||||
public function __construct($configFilePath = null)
|
||||
{
|
||||
return $this->loadConfig($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 = "<?php\n\n";
|
||||
$configFileContent .= "return ";
|
||||
$configFileContent .= var_export($this->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;
|
||||
}
|
||||
}
|
||||
113
vendor/clagiordano/weblibs-configmanager/tests/ConfigManagerTest.php
vendored
Normal file
113
vendor/clagiordano/weblibs-configmanager/tests/ConfigManagerTest.php
vendored
Normal file
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
|
||||
namespace clagiordano\weblibs\configmanager\tests;
|
||||
|
||||
use clagiordano\weblibs\configmanager\ConfigManager;
|
||||
|
||||
/**
|
||||
* Class ConfigManagerTest
|
||||
* @package clagiordano\weblibs\configmanager\tests
|
||||
*/
|
||||
class ConfigManagerTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/** @var ConfigManager $config */
|
||||
private $config = null;
|
||||
private $configFile = 'testsdata/sample_config_data.php';
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->config = 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');
|
||||
}
|
||||
}
|
||||
23
vendor/clagiordano/weblibs-configmanager/testsdata/sample_config_data.php
vendored
Normal file
23
vendor/clagiordano/weblibs-configmanager/testsdata/sample_config_data.php
vendored
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
return array (
|
||||
'app' => 'app_name',
|
||||
'db' =>
|
||||
array (
|
||||
'host' => 'localhost',
|
||||
'user' => 'sample_user',
|
||||
'pass' => 'sample_pass',
|
||||
'port' => 3306,
|
||||
),
|
||||
'other' =>
|
||||
array (
|
||||
'multi' =>
|
||||
array (
|
||||
'deep' =>
|
||||
array (
|
||||
'nested' => 'config_value',
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user