Writer.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\App\DeploymentConfig;
  7. use Magento\Framework\App\DeploymentConfig;
  8. use Magento\Framework\App\Filesystem\DirectoryList;
  9. use Magento\Framework\Config\File\ConfigFilePool;
  10. use Magento\Framework\Exception\FileSystemException;
  11. use Magento\Framework\Filesystem;
  12. use Magento\Framework\Phrase;
  13. /**
  14. * Deployment configuration writer to files: env.php, config.php.
  15. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  16. */
  17. class Writer
  18. {
  19. /**
  20. * Deployment config reader
  21. *
  22. * @var Reader
  23. */
  24. private $reader;
  25. /**
  26. * Application filesystem
  27. *
  28. * @var Filesystem
  29. */
  30. private $filesystem;
  31. /**
  32. * Formatter
  33. *
  34. * @var Writer\FormatterInterface
  35. */
  36. private $formatter;
  37. /**
  38. * @var ConfigFilePool
  39. */
  40. private $configFilePool;
  41. /**
  42. * @var DeploymentConfig
  43. */
  44. private $deploymentConfig;
  45. /**
  46. * The parser of comments from configuration files.
  47. *
  48. * @var CommentParser
  49. */
  50. private $commentParser;
  51. /**
  52. * @param Reader $reader
  53. * @param Filesystem $filesystem
  54. * @param ConfigFilePool $configFilePool
  55. * @param DeploymentConfig $deploymentConfig
  56. * @param Writer\FormatterInterface $formatter
  57. * @param CommentParser $commentParser The parser of comments from configuration files
  58. */
  59. public function __construct(
  60. Reader $reader,
  61. Filesystem $filesystem,
  62. ConfigFilePool $configFilePool,
  63. DeploymentConfig $deploymentConfig,
  64. Writer\FormatterInterface $formatter = null,
  65. CommentParser $commentParser = null
  66. ) {
  67. $this->reader = $reader;
  68. $this->filesystem = $filesystem;
  69. $this->configFilePool = $configFilePool;
  70. $this->deploymentConfig = $deploymentConfig;
  71. $this->formatter = $formatter ?: new Writer\PhpFormatter();
  72. $this->commentParser = $commentParser ?: new CommentParser($filesystem, $configFilePool);
  73. }
  74. /**
  75. * Check if configuration file is writable
  76. *
  77. * @return bool
  78. */
  79. public function checkIfWritable()
  80. {
  81. $configDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::CONFIG);
  82. foreach ($this->reader->getFiles() as $file) {
  83. if (!$configDirectory->isWritable($file)) {
  84. return false;
  85. }
  86. }
  87. return true;
  88. }
  89. /**
  90. * Saves config in specified file.
  91. * $pool option is deprecated since version 2.2.0.
  92. *
  93. * Usage:
  94. * ```php
  95. * saveConfig(
  96. * [
  97. * ConfigFilePool::APP_ENV => ['some' => 'value'],
  98. * ],
  99. * true,
  100. * null,
  101. * []
  102. * )
  103. * ```
  104. *
  105. * @param array $data The data to be saved
  106. * @param bool $override Whether values should be overridden
  107. * @param string $pool The file pool (deprecated)
  108. * @param array $comments The array of comments
  109. * @return void
  110. * @throws FileSystemException
  111. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  112. */
  113. public function saveConfig(array $data, $override = false, $pool = null, array $comments = [])
  114. {
  115. foreach ($data as $fileKey => $config) {
  116. $paths = $this->configFilePool->getPaths();
  117. if (isset($paths[$fileKey])) {
  118. $currentData = $this->reader->load($fileKey);
  119. $currentComments = $this->commentParser->execute($paths[$fileKey]);
  120. if ($currentData) {
  121. if ($override) {
  122. $config = array_merge($currentData, $config);
  123. } else {
  124. $config = array_replace_recursive($currentData, $config);
  125. }
  126. }
  127. $comments = array_merge($currentComments, $comments);
  128. $contents = $this->formatter->format($config, $comments);
  129. try {
  130. $writeFilePath = $paths[$fileKey];
  131. $this->filesystem->getDirectoryWrite(DirectoryList::CONFIG)->writeFile($writeFilePath, $contents);
  132. } catch (FileSystemException $e) {
  133. throw new FileSystemException(
  134. new Phrase('The "%1" deployment config file isn\'t writable.', [$paths[$fileKey]])
  135. );
  136. }
  137. if (function_exists('opcache_invalidate')) {
  138. opcache_invalidate(
  139. $this->filesystem->getDirectoryRead(DirectoryList::CONFIG)->getAbsolutePath($paths[$fileKey])
  140. );
  141. }
  142. }
  143. }
  144. $this->deploymentConfig->resetData();
  145. }
  146. }