Skip to content

Commit

Permalink
New command (config:oxid62:modules-configuration) to create modules…
Browse files Browse the repository at this point in the history
… configration files in the OXID version 6.1 in preparation to use them later in 6.2
  • Loading branch information
TumTum committed Feb 18, 2021
1 parent 1242506 commit 6a5cf59
Showing 1 changed file with 213 additions and 0 deletions.
213 changes: 213 additions & 0 deletions src/Oxrun/Command/Config/Oxid62ModulesConfigrationCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
<?php
/**
* Created by PhpStorm.
* Autor: Tobias Matthaiou
* Date: 18.02.21
* Time: 10:54
*/

namespace Oxrun\Command\Config;

use OxidEsales\Eshop\Core\DatabaseProvider;
use OxidEsales\Eshop\Core\Registry;
use Oxrun\Traits\NeedDatabase;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Yaml\Yaml;
use Webmozart\PathUtil\Path;

/**
* Class Oxid62ModulesConfigrationCommand
* @package Oxrun\Command\Config
*/
class Oxid62ModulesConfigrationCommand extends Command implements \Oxrun\Command\EnableInterface
{
use NeedDatabase;

/**
* @var string[]
*/
private $optionNames = ['production', 'staging', 'development', 'testing'];

/**
* @var array
*/
private $moduleConfigrations;

/**
* @var InputInterface
*/
private $input;

/**
* @var ConsoleOutput
*/
private $output;

/**
* @var \SplFileInfo
*/
private $envDir;

/**
* @inheritDoc
*/
protected function configure()
{
$this
->setName('config:oxid62:modules-configuration')
->setDescription('Creates the modules configurations for OXID eSale v6.2.x. Ideal for upgrade')
->setHelp(
'With this command modules-configuration can be created.. ' . PHP_EOL .
'Which will be needed later when updating to >6.2' . PHP_EOL .
'Otherwise the settings will be lost.' . PHP_EOL .
'See https://docs.oxid-esales.com/developer/en/6.2/development/modules_components_themes/project/module_configuration/modules_configuration.html'
);

array_map(function ($name) {
$this->addOption($name, '', InputOption::VALUE_NONE, 'Valid for "' . $name . '" system');
}, $this->optionNames);
}

/**
* @inheritDoc
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->input = $input;
$this->output = $output;
$enviromentOptions = $this->getEnviromentOptions();

if (empty($enviromentOptions)) {
$this->output->getErrorOutput()->writeln('<error>Please use one of this option --' . join(' --', $this->optionNames) . '</error>');
return 1;
}
$this->initEnviromentDir();

$this->loadModuleConfigrations();

return $this->saveEnviroments($enviromentOptions) ? 0 : 1;
}

/**
* @return array
*/
public function getEnviromentOptions(): array
{
return array_filter($this->optionNames, function ($name) {
return $this->input->getOption($name);
});
}

/**
* @see https://docs.oxid-esales.com/developer/en/6.2/development/modules_components_themes/project/module_configuration/modules_configuration.html
*/
protected function initEnviromentDir()
{
/** @var \Oxrun\Application $application */
$application = $this->getApplication();
$shopDir = $application->getShopDir();

$this->envDir = new \SplFileInfo(Path::join($shopDir, '..', 'var', 'configuration', 'environment'));

if (!$this->envDir->isDir()) {
@mkdir($this->envDir->getPathname(), 0775, true);
}
}

protected function loadModuleConfigrations()
{
$decodeValueQuery = Registry::getConfig()->getDecodeValueQuery('oxvarvalue');

$result = DatabaseProvider::getDb(DatabaseProvider::FETCH_MODE_ASSOC)->select(
"SELECT OXSHOPID, OXMODULE, OXVARTYPE, OXVARNAME, $decodeValueQuery as 'OXVARVALUE' FROM oxconfig WHERE OXMODULE LIKE 'module:%' ORDER BY OXSHOPID, OXMODULE, OXVARNAME"
)->fetchAll();

foreach ($result as $rowconfig) {
$moduleId = $this->convertModuleId($rowconfig['OXMODULE']);
$variableValue = $this->valueConvert($rowconfig['OXVARTYPE'], $rowconfig['OXVARVALUE']);
$this->addModuleConfigration($rowconfig['OXSHOPID'], $moduleId, $rowconfig['OXVARNAME'], $variableValue);
}
}

/**
* @param $rowModuleId
* @return mixed|string
*/
protected function convertModuleId($rowModuleId)
{
list($devnull, $moduleId) = explode(':', $rowModuleId, 2);

return $moduleId;
}

/**
* @param $rawVartype
* @param $rawVarvalue
*/
protected function valueConvert($rawVartype, $rawVarvalue)
{
$value = $rawVarvalue;

switch ($rawVartype) {
case 'arr':
case 'aarr':
$value = unserialize($rawVarvalue);
break;
case 'bool':
$value = (bool)($value == 'true' || $value == '1');
break;
default:
if (strpos($rawVarvalue, 'a:') === 0) {
$value = unserialize($rawVarvalue);
}
}

return $value;
}

/**
* @param $shopId
* @param $moduleId
* @param $variableName
* @param $variableValue
*/
public function addModuleConfigration($shopId, $moduleId, $variableName, $variableValue)
{
if (!isset($this->moduleConfigrations[$shopId])) {
$this->moduleConfigrations[$shopId] = ['modules' => []];
}

$this->moduleConfigrations[$shopId]['modules'][$moduleId]['moduleSettings'][$variableName]['value'] = $variableValue;
}

/**
* @param array $enviromentOptions
*/
public function saveEnviroments($enviromentOptions)
{
$success = true;

foreach ($this->moduleConfigrations as $shopId => $shopConfig) {
$yamlContent = Yaml::dump($shopConfig, 6, 2);
foreach ($enviromentOptions as $environment) {
$yamlPath = Path::join($this->envDir->getPathname(), sprintf('%s.%s.yaml', $environment, $shopId));
$saved = file_put_contents(
$yamlPath,
$yamlContent
);
if ($saved) {
$this->output->writeln("<info>{$yamlPath} is saved</info>");
} else {
$this->output->getErrorOutput()->writeln("<comment>Could not be saved: {$yamlPath}</comment>");
$success = false;
}
}
}

return $success;
}
}

0 comments on commit 6a5cf59

Please sign in to comment.