Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ConfigWriter.php
Go to the documentation of this file.
1 <?php
7 
16 
21 {
25  private $writer;
26 
30  private $arrayManager;
31 
37  private $preparedValueFactory;
38 
44  public function __construct(
45  Writer $writer,
46  ArrayManager $arrayManager,
47  PreparedValueFactory $valueFactory = null
48  ) {
49  $this->writer = $writer;
50  $this->arrayManager = $arrayManager;
51  $this->preparedValueFactory = $valueFactory ?: ObjectManager::getInstance()->get(PreparedValueFactory::class);
52  }
53 
62  public function save(array $values, $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeCode = null)
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  }
92 
102  private function setConfig(array $config, $configPath, $configValue)
103  {
104  if ($configValue === null) {
105  return $config;
106  }
107 
108  $config = $this->arrayManager->set(
109  $configPath,
110  $config,
111  $configValue
112  );
113 
114  return $config;
115  }
116 
126  private function getPathPrefix($scope, $scopeCode)
127  {
128  $pathPrefixes = [System::CONFIG_TYPE, $scope];
130  && !empty($scopeCode)
131  ) {
132  $pathPrefixes[] = $scopeCode;
133  }
134 
135  return implode('/', $pathPrefixes) . '/';
136  }
137 }
$config
Definition: fraud_order.php:17
$values
Definition: options.phtml:88
save(array $values, $scope=ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeCode=null)
__construct(Writer $writer, ArrayManager $arrayManager, PreparedValueFactory $valueFactory=null)
$value
Definition: gender.phtml:16