Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Writer.php
Go to the documentation of this file.
1 <?php
8 
15 
20 class Writer
21 {
27  private $reader;
28 
34  private $filesystem;
35 
41  private $formatter;
42 
46  private $configFilePool;
47 
51  private $deploymentConfig;
52 
58  private $commentParser;
59 
68  public function __construct(
69  Reader $reader,
70  Filesystem $filesystem,
71  ConfigFilePool $configFilePool,
72  DeploymentConfig $deploymentConfig,
73  Writer\FormatterInterface $formatter = null,
74  CommentParser $commentParser = null
75  ) {
76  $this->reader = $reader;
77  $this->filesystem = $filesystem;
78  $this->configFilePool = $configFilePool;
79  $this->deploymentConfig = $deploymentConfig;
80  $this->formatter = $formatter ?: new Writer\PhpFormatter();
81  $this->commentParser = $commentParser ?: new CommentParser($filesystem, $configFilePool);
82  }
83 
89  public function checkIfWritable()
90  {
91  $configDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::CONFIG);
92  foreach ($this->reader->getFiles() as $file) {
93  if (!$configDirectory->isWritable($file)) {
94  return false;
95  }
96  }
97  return true;
98  }
99 
124  public function saveConfig(array $data, $override = false, $pool = null, array $comments = [])
125  {
126  foreach ($data as $fileKey => $config) {
127  $paths = $this->configFilePool->getPaths();
128 
129  if (isset($paths[$fileKey])) {
130  $currentData = $this->reader->load($fileKey);
131  $currentComments = $this->commentParser->execute($paths[$fileKey]);
132 
133  if ($currentData) {
134  if ($override) {
135  $config = array_merge($currentData, $config);
136  } else {
137  $config = array_replace_recursive($currentData, $config);
138  }
139  }
140 
141  $comments = array_merge($currentComments, $comments);
142 
143  $contents = $this->formatter->format($config, $comments);
144  try {
145  $writeFilePath = $paths[$fileKey];
146  $this->filesystem->getDirectoryWrite(DirectoryList::CONFIG)->writeFile($writeFilePath, $contents);
147  } catch (FileSystemException $e) {
148  throw new FileSystemException(
149  new Phrase('The "%1" deployment config file isn\'t writable.', [$paths[$fileKey]])
150  );
151  }
152  if (function_exists('opcache_invalidate')) {
153  opcache_invalidate(
154  $this->filesystem->getDirectoryRead(DirectoryList::CONFIG)->getAbsolutePath($paths[$fileKey])
155  );
156  }
157  }
158  }
159  $this->deploymentConfig->resetData();
160  }
161 }
$contents
Definition: website.php:14
__construct(Reader $reader, Filesystem $filesystem, ConfigFilePool $configFilePool, DeploymentConfig $deploymentConfig, Writer\FormatterInterface $formatter=null, CommentParser $commentParser=null)
Definition: Writer.php:68
$config
Definition: fraud_order.php:17
$deploymentConfig
$paths
Definition: _bootstrap.php:83
$filesystem
saveConfig(array $data, $override=false, $pool=null, array $comments=[])
Definition: Writer.php:124