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

Public Member Functions

 __construct (Reader $reader, Filesystem $filesystem, ConfigFilePool $configFilePool, DeploymentConfig $deploymentConfig, Writer\FormatterInterface $formatter=null, CommentParser $commentParser=null)
 
 checkIfWritable ()
 
 saveConfig (array $data, $override=false, $pool=null, array $comments=[])
 

Detailed Description

Deployment configuration writer to files: env.php, config.php. @SuppressWarnings(PHPMD.CouplingBetweenObjects)

Definition at line 20 of file Writer.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( Reader  $reader,
Filesystem  $filesystem,
ConfigFilePool  $configFilePool,
DeploymentConfig  $deploymentConfig,
Writer\FormatterInterface  $formatter = null,
CommentParser  $commentParser = null 
)
Parameters
Reader$reader
Filesystem$filesystem
ConfigFilePool$configFilePool
DeploymentConfig$deploymentConfig
Writer\FormatterInterface$formatter
CommentParser$commentParserThe parser of comments from configuration files

Definition at line 68 of file Writer.php.

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  }

Member Function Documentation

◆ checkIfWritable()

checkIfWritable ( )

Check if configuration file is writable

Returns
bool

Definition at line 89 of file Writer.php.

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  }

◆ saveConfig()

saveConfig ( array  $data,
  $override = false,
  $pool = null,
array  $comments = [] 
)

Saves config in specified file. $pool option is deprecated since version 2.2.0.

Usage:

[
ConfigFilePool::APP_ENV => ['some' => 'value'],
],
true,
null,
[]
)
Parameters
array$dataThe data to be saved
bool$overrideWhether values should be overridden
string$poolThe file pool (deprecated)
array$commentsThe array of comments
Returns
void
Exceptions
FileSystemException@SuppressWarnings(PHPMD.UnusedFormalParameter)

Definition at line 124 of file Writer.php.

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  }
$contents
Definition: website.php:14
$config
Definition: fraud_order.php:17
$paths
Definition: _bootstrap.php:83

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