Merge branch 'master' of ssh://172.20.0.1/var/git/espo/backend
This commit is contained in:
@@ -15,3 +15,4 @@
|
||||
/client
|
||||
/test.php
|
||||
/main.html
|
||||
/tests/testData/Utils/Config/config.php
|
||||
|
||||
@@ -63,6 +63,12 @@ class Settings extends \Espo\Core\Controllers\Base
|
||||
throw new Error('Cannot save settings');
|
||||
}
|
||||
|
||||
/** Rebuild for Currency Settings */
|
||||
if (isset($data['baseCurrency']) || isset($data['currencyRates'])) {
|
||||
$this->getContainer()->get('dataManager')->rebuildDatabase(array());
|
||||
}
|
||||
/** END Rebuild for Currency Settings */
|
||||
|
||||
return $this->getConfigData();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,6 +56,7 @@ class Config
|
||||
private $data;
|
||||
|
||||
private $changedData = array();
|
||||
private $removeData = array();
|
||||
|
||||
private $fileManager;
|
||||
|
||||
@@ -122,6 +123,23 @@ class Config
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove an option in config
|
||||
*
|
||||
* @param string $name
|
||||
* @return bool | null - null if an option doesn't exist
|
||||
*/
|
||||
public function remove($name)
|
||||
{
|
||||
if (array_key_exists($name, $this->data)) {
|
||||
unset($this->data[$name]);
|
||||
$this->removeData[] = $name;
|
||||
return true;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function save()
|
||||
{
|
||||
$values = $this->changedData;
|
||||
@@ -130,9 +148,12 @@ class Config
|
||||
$values = array_merge($this->updateCacheTimestamp(true), $values);
|
||||
}
|
||||
|
||||
$result = $this->getFileManager()->mergeContentsPHP($this->configPath, $values, true);
|
||||
$removeData = empty($this->removeData) ? null : $this->removeData;
|
||||
|
||||
$result = $this->getFileManager()->mergeContentsPHP($this->configPath, $values, 1, $removeData);
|
||||
if ($result) {
|
||||
$this->changedData = array();
|
||||
$this->removeData = array();
|
||||
$this->loadConfig(true);
|
||||
}
|
||||
|
||||
|
||||
@@ -225,22 +225,33 @@ class Manager
|
||||
* @param string | array $path
|
||||
* @param string $content JSON string
|
||||
* @param bool $isJSON
|
||||
* @param array $mergeOptions
|
||||
* @param string | array $mergeOptions
|
||||
* @param string | array $removeOptions - List of unset keys from content
|
||||
* @param bool $isReturn - Is result to be returned or stored
|
||||
*
|
||||
* @return bool
|
||||
* @return bool | array
|
||||
*/
|
||||
public function mergeContents($path, $content, $isJSON = false, $mergeOptions = null)
|
||||
public function mergeContents($path, $content, $isJSON = false, $mergeOptions = null, $removeOptions = null, $isReturn = false)
|
||||
{
|
||||
$fileContent = $this->getContents($path);
|
||||
|
||||
$savedDataArray = Utils\Json::getArrayData($fileContent);
|
||||
$newDataArray = Utils\Json::getArrayData($content);
|
||||
|
||||
if (isset($removeOptions)) {
|
||||
$savedDataArray = Utils\Util::unsetInArray($savedDataArray, $removeOptions);
|
||||
$newDataArray = Utils\Util::unsetInArray($newDataArray, $removeOptions);
|
||||
}
|
||||
|
||||
$data = Utils\Util::merge($savedDataArray, $newDataArray, $mergeOptions);
|
||||
if ($isJSON) {
|
||||
$data = Utils\Json::encode($data, JSON_PRETTY_PRINT);
|
||||
}
|
||||
|
||||
if ($isReturn) {
|
||||
return $data;
|
||||
}
|
||||
|
||||
return $this->putContents($path, $data);
|
||||
}
|
||||
|
||||
@@ -248,26 +259,14 @@ class Manager
|
||||
* Merge PHP content and save it to a file
|
||||
*
|
||||
* @param string | array $path
|
||||
* @param string $content
|
||||
* @param bool $onlyFirstLevel - Merge only first level. Ex. current: array('test'=>array('item1', 'item2')). $content= array('test'=>array('item1'),). Result will be array('test'=>array('item1')).
|
||||
*
|
||||
* @param string $content JSON string
|
||||
* @param string | array $mergeOptions
|
||||
* @param string | array $removeOptions - List of unset keys from content
|
||||
* @return bool
|
||||
*/
|
||||
public function mergeContentsPHP($path, $content, $onlyFirstLevel = false, $mergeOptions = null)
|
||||
public function mergeContentsPHP($path, $content, $mergeOptions = null, $removeOptions = null)
|
||||
{
|
||||
$fileContent = $this->getContents($path);
|
||||
|
||||
$savedDataArray = Utils\Json::getArrayData($fileContent);
|
||||
$newDataArray = Utils\Json::getArrayData($content);
|
||||
|
||||
if ($onlyFirstLevel) {
|
||||
foreach($newDataArray as $key => $val) {
|
||||
$setVal = is_array($val) ? array() : '';
|
||||
$savedDataArray[$key] = $setVal;
|
||||
}
|
||||
}
|
||||
|
||||
$data = Utils\Util::merge($savedDataArray, $newDataArray, $mergeOptions);
|
||||
$data = $this->mergeContents($path, $content, false, $mergeOptions, $removeOptions, true);
|
||||
|
||||
return $this->putContentsPHP($path, $data);
|
||||
}
|
||||
|
||||
@@ -359,11 +359,17 @@ class Util
|
||||
if (!empty($unsetSett)){
|
||||
$keyItems = explode('.', $unsetSett);
|
||||
$currVal = isset($content[$rootKey]) ? "\$content['{$rootKey}']" : "\$content";
|
||||
|
||||
$lastKey = array_pop($keyItems);
|
||||
foreach($keyItems as $keyItem){
|
||||
$currVal .= "['{$keyItem}']";
|
||||
}
|
||||
|
||||
$currVal = "if (isset({$currVal})) unset({$currVal});";
|
||||
$unsetElem = $currVal . "['{$lastKey}']";
|
||||
$currVal = "
|
||||
if (isset({$unsetElem}) || array_key_exists({$lastKey}, {$currVal})) {
|
||||
unset({$unsetElem});
|
||||
} ";
|
||||
eval($currVal);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,39 +15,39 @@ class ConfigTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
protected $systemConfigPath = 'tests/testData/Utils/Config/systemConfig.php';
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
protected function setUp()
|
||||
{
|
||||
$this->objects['fileManager'] = new \Espo\Core\Utils\File\Manager();
|
||||
|
||||
$this->object = new \Espo\Core\Utils\Config($this->objects['fileManager']);
|
||||
$this->object = new \Espo\Core\Utils\Config($this->objects['fileManager']);
|
||||
|
||||
$this->reflection = new ReflectionHelper($this->object);
|
||||
$this->reflection = new ReflectionHelper($this->object);
|
||||
|
||||
$this->reflection->setProperty('configPath', $this->configPath);
|
||||
$this->reflection->setProperty('systemConfigPath', $this->systemConfigPath);
|
||||
}
|
||||
$this->reflection->setProperty('configPath', $this->configPath);
|
||||
$this->reflection->setProperty('systemConfigPath', $this->systemConfigPath);
|
||||
}
|
||||
|
||||
protected function tearDown()
|
||||
{
|
||||
$this->object = NULL;
|
||||
}
|
||||
protected function tearDown()
|
||||
{
|
||||
$this->object = NULL;
|
||||
}
|
||||
|
||||
|
||||
public function testLoadConfig()
|
||||
public function testLoadConfig()
|
||||
{
|
||||
$this->assertArrayHasKey('database', $this->reflection->invokeMethod('loadConfig', array()));
|
||||
|
||||
$this->assertArrayHasKey('dateFormat', $this->reflection->invokeMethod('loadConfig', array()));
|
||||
}
|
||||
|
||||
public function testGet()
|
||||
public function testGet()
|
||||
{
|
||||
$result = array(
|
||||
'driver' => 'pdo_mysql',
|
||||
'host' => 'localhost',
|
||||
'dbname' => 'espocrm',
|
||||
'user' => 'root',
|
||||
'password' => '',
|
||||
'host' => 'localhost',
|
||||
'dbname' => 'espocrm',
|
||||
'user' => 'root',
|
||||
'password' => '',
|
||||
);
|
||||
$this->assertEquals($result, $this->object->get('database'));
|
||||
|
||||
@@ -62,17 +62,17 @@ class ConfigTest extends \PHPUnit_Framework_TestCase
|
||||
}
|
||||
|
||||
|
||||
public function testSet()
|
||||
public function testSet()
|
||||
{
|
||||
$setKey= 'testOption';
|
||||
$setKey= 'testOption';
|
||||
$setValue= 'Test';
|
||||
|
||||
$this->object->set($setKey, $setValue);
|
||||
$this->assertTrue($this->object->save());
|
||||
$this->assertEquals($setValue, $this->object->get($setKey));
|
||||
$this->assertTrue($this->object->save());
|
||||
$this->assertEquals($setValue, $this->object->get($setKey));
|
||||
|
||||
$this->object->set($setKey, 'Another Wrong Value');
|
||||
$this->assertTrue($this->object->save());
|
||||
$this->object->set($setKey, 'Another Wrong Value');
|
||||
$this->assertTrue($this->object->save());
|
||||
}
|
||||
|
||||
public function testSetArray()
|
||||
@@ -83,15 +83,28 @@ class ConfigTest extends \PHPUnit_Framework_TestCase
|
||||
);
|
||||
|
||||
$this->object->set($values);
|
||||
$this->assertTrue($this->object->save());
|
||||
$this->assertEquals('Test', $this->object->get('testOption'));
|
||||
$this->assertEquals('Test2', $this->object->get('testOption2'));
|
||||
$this->assertTrue($this->object->save());
|
||||
$this->assertEquals('Test', $this->object->get('testOption'));
|
||||
$this->assertEquals('Test2', $this->object->get('testOption2'));
|
||||
|
||||
$wrongArray = array(
|
||||
'testOption' => 'Another Wrong Value',
|
||||
);
|
||||
$this->object->set($wrongArray);
|
||||
$this->assertTrue($this->object->save());
|
||||
$wrongArray = array(
|
||||
'testOption' => 'Another Wrong Value',
|
||||
);
|
||||
$this->object->set($wrongArray);
|
||||
$this->assertTrue($this->object->save());
|
||||
}
|
||||
|
||||
public function testRemove()
|
||||
{
|
||||
$optKey = 'removeOption';
|
||||
$optValue = 'Test';
|
||||
|
||||
$this->object->set($optKey, $optValue);
|
||||
$this->assertTrue($this->object->save());
|
||||
|
||||
$this->assertTrue($this->object->remove($optKey));
|
||||
|
||||
$this->assertNull($this->object->get($optKey));
|
||||
}
|
||||
|
||||
public function testSystemConfigMerge()
|
||||
|
||||
Reference in New Issue
Block a user