Last active
December 24, 2018 20:25
-
-
Save anoriar/a5d4abf572e21c1dcc2bf44b1259d0fc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Sprint\Migration; | |
use Aeroidea\Settings\Options\ConfigurationTable; | |
use Aeroidea\Settings\Options\OptionTable; | |
use Aeroidea\Settings\Options\ValuesTable; | |
use Bitrix\Main\Application; | |
class Version20181224133132 extends Version | |
{ | |
protected $description = "Апдейт всех конфигураций"; | |
protected $existConfigs; | |
protected $existOptions; | |
protected $existValues; | |
protected $server; | |
protected $defaultConfig = [ | |
"elastic" => | |
[ | |
'OPTIONS' => [ | |
"host" => [ | |
'CODE' => 'host', | |
'VALUE' => '172.17.0.16:9200' | |
], | |
"chunk_size" => [ | |
'CODE' => 'chunk_size', | |
'VALUE' => '500' | |
] | |
] | |
], | |
"rest" => | |
[ | |
'OPTIONS' => [ | |
"access_token_work_time" => [ | |
'CODE' => 'access_token_work_time', | |
'VALUE' => '21600' | |
], | |
"chunk_size" => [ | |
'CODE' => 'log_path', | |
'VALUE' => '../log/full-rest.log' | |
] | |
] | |
] | |
]; | |
protected $serverConfig = [ | |
"dev1.paolaray.aeroidea.ru" => [ | |
"rest" => | |
[ | |
'OPTIONS' => [ | |
"access_token_work_time" => [ | |
'VALUE' => '20' | |
], | |
] | |
] | |
] | |
]; | |
public function up() | |
{ | |
$this->server = Application::getInstance()->getContext()->getServer()->getHttpHost(); | |
$this->setAllExistsConfigs(); | |
$this->setAllExistsOptions(); | |
$this->setAllExistsValues(); | |
if ($this->serverConfig[$this->server]) { | |
$this->migrate($this->arrayMergeRecursiveDistinct($this->defaultConfig, | |
$this->serverConfig[$this->server])); | |
} else { | |
$this->migrate($this->defaultConfig); | |
} | |
} | |
public function down() | |
{ | |
} | |
protected function setAllExistsConfigs() | |
{ | |
$this->existConfigs = []; | |
$query = ConfigurationTable::getList([]); | |
while ($config = $query->fetch()) { | |
$this->existConfigs[$config["ID"]] = $config; | |
} | |
} | |
protected function setAllExistsOptions() | |
{ | |
$this->existOptions = []; | |
$query = OptionTable::getList([]); | |
while ($option = $query->fetch()) { | |
$this->existOptions[$option["CODE"]] = $option; | |
} | |
} | |
protected function setAllExistsValues() | |
{ | |
$this->existValues = []; | |
$query = ValuesTable::getList([]); | |
while ($value = $query->fetch()) { | |
$this->existValues[$value["ID"]] = $value; | |
} | |
} | |
/** | |
* Полная миграция всех конфигураций | |
* @param $configs | |
*/ | |
protected function migrate($configs) | |
{ | |
foreach ($configs as $code => $config) { | |
if ($this->isConfigExists($code)) { | |
foreach ($config["OPTIONS"] as $option) { | |
if ($this->isOptionExists($option)) { | |
$this->updateOptionValue($option); | |
} | |
} | |
} | |
} | |
} | |
protected function updateOptionValue($option) | |
{ | |
$optionId = $this->existOptions[$option["CODE"]]["ID"]; | |
if ($optionId) { | |
$value = $this->getValueByOptionId($optionId); | |
if ($value["VALUE"] != $option["VALUE"]) { | |
$value["VALUE"] = $option['VALUE']; | |
ValuesTable::update($value["ID"], $value); | |
} | |
} | |
} | |
protected function isOptionExists($option) | |
{ | |
if ($this->existOptions[$option["CODE"]]) { | |
return true; | |
} | |
return false; | |
} | |
protected function isConfigExists($configCode) | |
{ | |
if ($this->getConfigIdByCode($configCode)) { | |
return true; | |
} | |
return false; | |
} | |
protected function getConfigIdByCode($code) | |
{ | |
foreach ($this->existConfigs as $id => $config) { | |
if ($config["CODE"] == $code) { | |
return $id; | |
} | |
} | |
return false; | |
} | |
protected function getValueByOptionId($optionId) | |
{ | |
foreach ($this->existValues as $id => $value) { | |
if ($optionId == $value["OPTION_ID"]) { | |
return $value; | |
} | |
} | |
return false; | |
} | |
protected function arrayMergeRecursiveDistinct(array &$array1, array &$array2) | |
{ | |
$merged = $array1; | |
foreach ($array2 as $key => &$value) { | |
if (is_array($value) && isset ($merged [$key]) && is_array($merged [$key])) { | |
$merged [$key] = $this->arrayMergeRecursiveDistinct($merged [$key], $value); | |
} else { | |
$merged [$key] = $value; | |
} | |
} | |
return $merged; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment