Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions
ConfigWriter Class Reference

Public Member Functions

 __construct (Writer $writer, ArrayManager $arrayManager, PreparedValueFactory $valueFactory=null)
 
 save (array $values, $scope=ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeCode=null)
 

Detailed Description

Class ConfigWriter. Save configuration values into config file.

Definition at line 20 of file ConfigWriter.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( Writer  $writer,
ArrayManager  $arrayManager,
PreparedValueFactory  $valueFactory = null 
)
Parameters
Writer$writer
ArrayManager$arrayManager
PreparedValueFactory | null$valueFactoryCreates a prepared instance of Value

Definition at line 44 of file ConfigWriter.php.

48  {
49  $this->writer = $writer;
50  $this->arrayManager = $arrayManager;
51  $this->preparedValueFactory = $valueFactory ?: ObjectManager::getInstance()->get(PreparedValueFactory::class);
52  }

Member Function Documentation

◆ save()

save ( array  $values,
  $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
  $scopeCode = null 
)

Save given list of configuration values into config file.

Parameters
array$valuesthe configuration values (path-value pairs) to be saved.
string$scopescope in which configuration would be saved.
string | null$scopeCode
Returns
void

Definition at line 62 of file ConfigWriter.php.

63  {
64  $config = [];
65  $pathPrefix = $this->getPathPrefix($scope, $scopeCode);
66 
67  $values = array_filter(
68  $values,
69  function ($value) {
70  return $value !== null;
71  }
72  );
73 
74  foreach ($values as $configPath => $configValue) {
75  $fullConfigPath = $pathPrefix . $configPath;
76  $backendModel = $this->preparedValueFactory->create($configPath, $configValue, $scope, $scopeCode);
77 
78  if ($backendModel instanceof Value) {
79  $backendModel->validateBeforeSave();
80  $backendModel->beforeSave();
81  $configValue = $backendModel->getValue();
82  $backendModel->afterSave();
83  }
84 
85  $config = $this->setConfig($config, $fullConfigPath, $configValue);
86  }
87 
88  $this->writer->saveConfig(
90  );
91  }
$config
Definition: fraud_order.php:17
$values
Definition: options.phtml:88
$value
Definition: gender.phtml:16

The documentation for this class was generated from the following file: