Initial commit: CloudOps infrastructure platform
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace Mautic\ConfigBundle\Mapper;
|
||||
|
||||
use Mautic\ConfigBundle\Exception\BadFormConfigException;
|
||||
use Mautic\ConfigBundle\Mapper\Helper\ConfigHelper;
|
||||
use Mautic\ConfigBundle\Mapper\Helper\RestrictionHelper;
|
||||
use Mautic\CoreBundle\Helper\CoreParametersHelper;
|
||||
|
||||
class ConfigMapper
|
||||
{
|
||||
/**
|
||||
* @var mixed[]
|
||||
*/
|
||||
private array $restrictedParameters;
|
||||
|
||||
public function __construct(
|
||||
private CoreParametersHelper $parametersHelper,
|
||||
array $restrictedParameters = [],
|
||||
) {
|
||||
$this->restrictedParameters = RestrictionHelper::prepareRestrictions($restrictedParameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws BadFormConfigException
|
||||
*/
|
||||
public function bindFormConfigsWithRealValues(array $forms): array
|
||||
{
|
||||
foreach ($forms as $bundle => $config) {
|
||||
if (!isset($config['parameters'])) {
|
||||
throw new BadFormConfigException();
|
||||
}
|
||||
|
||||
$forms[$bundle]['parameters'] = $this->mergeWithLocalParameters($forms[$bundle]['parameters']);
|
||||
}
|
||||
|
||||
return $forms;
|
||||
}
|
||||
|
||||
/**
|
||||
* Merges default parameters from each subscribed bundle with the local (real) params.
|
||||
*/
|
||||
private function mergeWithLocalParameters(array $formParameters): array
|
||||
{
|
||||
$formParameters = RestrictionHelper::applyRestrictions($formParameters, $this->restrictedParameters);
|
||||
|
||||
// All config values are stored at root level of the config
|
||||
foreach ($formParameters as $formKey => $defaultValue) {
|
||||
$configValue = $this->parametersHelper->get($formKey);
|
||||
|
||||
if (null === $configValue) {
|
||||
// Nothing has been locally configured so keep default
|
||||
continue;
|
||||
}
|
||||
|
||||
// Form field is a collection of parameters
|
||||
if (is_array($configValue)) {
|
||||
// Apply nested restrictions to nested config values
|
||||
$configValue = RestrictionHelper::applyRestrictions($configValue, $this->restrictedParameters, $formKey);
|
||||
|
||||
// Bind configured values with defaults
|
||||
$formParameters[$formKey] = ConfigHelper::bindNestedConfigValues($configValue, $defaultValue);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
// Form field
|
||||
$formParameters[$formKey] = $configValue;
|
||||
}
|
||||
|
||||
return $formParameters;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace Mautic\ConfigBundle\Mapper\Helper;
|
||||
|
||||
class ConfigHelper
|
||||
{
|
||||
/**
|
||||
* Map local config values with form fields.
|
||||
*
|
||||
* @param mixed $defaults
|
||||
*/
|
||||
public static function bindNestedConfigValues(array $configValues, $defaults): array
|
||||
{
|
||||
if (!is_array($defaults)) {
|
||||
// Return all config values
|
||||
return $configValues;
|
||||
}
|
||||
|
||||
foreach ($defaults as $key => $defaultValue) {
|
||||
if (isset($configValues[$key]) && is_array($configValues[$key])) {
|
||||
$configValues[$key] = self::bindNestedConfigValues($configValues[$key], $defaultValue);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$configValues[$key] ??= $defaultValue;
|
||||
}
|
||||
|
||||
return $configValues;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace Mautic\ConfigBundle\Mapper\Helper;
|
||||
|
||||
class RestrictionHelper
|
||||
{
|
||||
/**
|
||||
* Ensure that the array has string indexes for congruency with a nested array similar to ['db_host', 'monitored_email' => ['EmailBundle_bounces'];.
|
||||
*/
|
||||
public static function prepareRestrictions(array $restrictedParameters): array
|
||||
{
|
||||
$prepared = [];
|
||||
foreach ($restrictedParameters as $key => $value) {
|
||||
$newKey = (is_numeric($key)) ? $value : $key;
|
||||
$prepared[$newKey] = (is_array($value)) ? self::prepareRestrictions($value) : $value;
|
||||
}
|
||||
|
||||
return $prepared;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove fields that are restricted.
|
||||
*/
|
||||
public static function applyRestrictions(array $configParameters, array $restrictedParameters, $restrictedParentKey = null): array
|
||||
{
|
||||
if ($restrictedParentKey) {
|
||||
if (!isset($restrictedParameters[$restrictedParentKey])) {
|
||||
// No restrictions
|
||||
return $configParameters;
|
||||
}
|
||||
|
||||
$restrictedParameters = $restrictedParameters[$restrictedParentKey];
|
||||
}
|
||||
|
||||
foreach ($configParameters as $key => $value) {
|
||||
// The entire form type is restricted
|
||||
if (isset($restrictedParameters[$key]) && !is_array($restrictedParameters[$key])) {
|
||||
unset($configParameters[$key]);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
// A sub type of the form type is restricted
|
||||
if (is_array($value)) {
|
||||
$configParameters[$key] = self::applyRestrictions($value, $restrictedParameters, $key);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
// Otherwise no restrictions are in place
|
||||
}
|
||||
|
||||
return $configParameters;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user