Reader.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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\Filesystem\DirectoryList;
  8. use Magento\Framework\Config\File\ConfigFilePool;
  9. use Magento\Framework\Exception\FileSystemException;
  10. use Magento\Framework\Exception\RuntimeException;
  11. use Magento\Framework\Filesystem\DriverPool;
  12. use Magento\Framework\Phrase;
  13. /**
  14. * Deployment configuration reader.
  15. * Loads the merged configuration from config files.
  16. *
  17. * @see FileReader The reader for specific configuration file
  18. */
  19. class Reader
  20. {
  21. /**
  22. * @var DirectoryList
  23. */
  24. private $dirList;
  25. /**
  26. * @var ConfigFilePool
  27. */
  28. private $configFilePool;
  29. /**
  30. * @var DriverPool
  31. */
  32. private $driverPool;
  33. /**
  34. * Configuration file names
  35. *
  36. * @var array
  37. */
  38. private $files;
  39. /**
  40. * Constructor
  41. *
  42. * @param DirectoryList $dirList
  43. * @param DriverPool $driverPool
  44. * @param ConfigFilePool $configFilePool
  45. * @param null|string $file
  46. * @throws \InvalidArgumentException
  47. */
  48. public function __construct(
  49. DirectoryList $dirList,
  50. DriverPool $driverPool,
  51. ConfigFilePool $configFilePool,
  52. $file = null
  53. ) {
  54. $this->dirList = $dirList;
  55. $this->configFilePool = $configFilePool;
  56. $this->driverPool = $driverPool;
  57. if (null !== $file) {
  58. if (!preg_match('/^[a-z\d\.\-]+\.php$/i', $file)) {
  59. throw new \InvalidArgumentException("Invalid file name: {$file}");
  60. }
  61. $this->files = [$file];
  62. } else {
  63. $this->files = $this->configFilePool->getPaths();
  64. }
  65. }
  66. /**
  67. * Gets the file name
  68. *
  69. * @return array
  70. */
  71. public function getFiles()
  72. {
  73. return $this->files;
  74. }
  75. /**
  76. * Method loads merged configuration within all configuration files.
  77. * To retrieve specific file configuration, use FileReader.
  78. * $fileKey option is deprecated since version 2.2.0.
  79. *
  80. * @param string $fileKey The file key (deprecated)
  81. * @return array
  82. * @throws FileSystemException If file can not be read
  83. * @throws RuntimeException If file is invalid
  84. * @throws \Exception If file key is not correct
  85. * @see FileReader
  86. */
  87. public function load($fileKey = null)
  88. {
  89. $path = $this->dirList->getPath(DirectoryList::CONFIG);
  90. $fileDriver = $this->driverPool->getDriver(DriverPool::FILE);
  91. $result = [];
  92. if ($fileKey) {
  93. $filePath = $path . '/' . $this->configFilePool->getPath($fileKey);
  94. if ($fileDriver->isExists($filePath)) {
  95. $result = include $filePath;
  96. if (!is_array($result)) {
  97. throw new RuntimeException(new Phrase("Invalid configuration file: '%1'", [$filePath]));
  98. }
  99. }
  100. } else {
  101. $configFiles = $this->configFilePool->getPaths();
  102. $allFilesData = [];
  103. $result = [];
  104. foreach (array_keys($configFiles) as $fileKey) {
  105. $configFile = $path . '/' . $this->configFilePool->getPath($fileKey);
  106. if ($fileDriver->isExists($configFile)) {
  107. $fileData = include $configFile;
  108. if (!is_array($fileData)) {
  109. throw new RuntimeException(new Phrase("Invalid configuration file: '%1'", [$configFile]));
  110. }
  111. } else {
  112. continue;
  113. }
  114. $allFilesData[$configFile] = $fileData;
  115. if ($fileData) {
  116. $result = array_replace_recursive($result, $fileData);
  117. }
  118. }
  119. }
  120. return $result ?: [];
  121. }
  122. /**
  123. * Loads the configuration file.
  124. *
  125. * @param string $fileKey The file key
  126. * @param string $pathConfig The path config
  127. * @param bool $ignoreInitialConfigFiles Whether ignore custom pools
  128. * @return array
  129. * @deprecated 101.0.0 Magento does not support custom config file pools since 2.2.0 version
  130. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  131. */
  132. public function loadConfigFile($fileKey, $pathConfig, $ignoreInitialConfigFiles = false)
  133. {
  134. return $this->load($fileKey);
  135. }
  136. }