Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ConfigModel.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Setup\Model;
8 
14 
16 {
20  protected $collector;
21 
25  protected $writer;
26 
32  private $filePermissions;
33 
37  protected $deploymentConfig;
38 
47  public function __construct(
51  FilePermissions $filePermissions
52  ) {
53  $this->collector = $collector;
54  $this->writer = $writer;
55  $this->filePermissions = $filePermissions;
56  $this->deploymentConfig = $deploymentConfig;
57  }
58 
64  public function getAvailableOptions()
65  {
67  $optionCollection = [];
68  $optionLists = $this->collector->collectOptionsLists();
69 
70  foreach ($optionLists as $optionList) {
71  $optionCollection = array_merge($optionCollection, $optionList->getOptions());
72  }
73 
74  foreach ($optionCollection as $option) {
75  $currentValue = $this->deploymentConfig->get($option->getConfigPath());
76  if ($currentValue !== null) {
77  $option->setDefault();
78  }
79  }
80 
81  return $optionCollection;
82  }
83 
91  public function process($inputOptions)
92  {
93  $this->checkInstallationFilePermissions();
94 
95  $options = $this->collector->collectOptionsLists();
96 
97  foreach ($options as $moduleName => $option) {
98  $configData = $option->createConfig($inputOptions, $this->deploymentConfig);
99 
100  foreach ($configData as $config) {
101  $fileConfigStorage = [];
102  if (!$config instanceof ConfigData) {
103  throw new \Exception(
104  'In module : '
105  . $moduleName
106  . 'ConfigOption::createConfig should return an array of ConfigData instances'
107  );
108  }
109 
110  if (isset($fileConfigStorage[$config->getFileKey()])) {
111  $fileConfigStorage[$config->getFileKey()] = array_replace_recursive(
112  $fileConfigStorage[$config->getFileKey()],
113  $config->getData()
114  );
115  } else {
116  $fileConfigStorage[$config->getFileKey()] = $config->getData();
117  }
118  $this->writer->saveConfig($fileConfigStorage, $config->isOverrideWhenSave());
119  }
120  }
121  }
122 
129  public function validate(array $inputOptions)
130  {
131  $errors = [];
132 
133  //Basic types validation
134  $options = $this->getAvailableOptions();
135  foreach ($options as $option) {
136  try {
137  if ($inputOptions[$option->getName()] !== null) {
138  $option->validate($inputOptions[$option->getName()]);
139  }
140  } catch (\InvalidArgumentException $e) {
141  $errors[] = $e->getMessage();
142  }
143  }
144 
145  // validate ConfigOptionsList
146  $options = $this->collector->collectOptionsLists();
147 
148  foreach ($options as $option) {
149  $errors = array_merge($errors, $option->validate($inputOptions, $this->deploymentConfig));
150  }
151 
152  return $errors;
153  }
154 
161  private function checkInstallationFilePermissions()
162  {
163  $results = $this->filePermissions->getMissingWritablePathsForInstallation();
164  if ($results) {
165  $errorMsg = "Missing write permissions to the following paths:" . PHP_EOL . implode(PHP_EOL, $results);
166  throw new \Exception($errorMsg);
167  }
168  }
169 }
$results
Definition: popup.phtml:13
$config
Definition: fraud_order.php:17
__construct(ConfigOptionsListCollector $collector, Writer $writer, DeploymentConfig $deploymentConfig, FilePermissions $filePermissions)
Definition: ConfigModel.php:47
$errors
Definition: overview.phtml:9
validate(array $inputOptions)