FileReader.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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\Filesystem\DriverPool;
  11. /**
  12. * Allows to read configurations from different config files.
  13. *
  14. * @see Reader The reader for merged configurations
  15. */
  16. class FileReader
  17. {
  18. /**
  19. * The list of directories.
  20. *
  21. * @var DirectoryList
  22. */
  23. private $dirList;
  24. /**
  25. * The pool of config files.
  26. *
  27. * @var ConfigFilePool
  28. */
  29. private $configFilePool;
  30. /**
  31. * The pool of stream drivers.
  32. *
  33. * @var DriverPool
  34. */
  35. private $driverPool;
  36. /**
  37. * @param DirectoryList $dirList The list of directories
  38. * @param DriverPool $driverPool The pool of config files
  39. * @param ConfigFilePool $configFilePool The pool of stream drivers
  40. */
  41. public function __construct(
  42. DirectoryList $dirList,
  43. DriverPool $driverPool,
  44. ConfigFilePool $configFilePool
  45. ) {
  46. $this->dirList = $dirList;
  47. $this->configFilePool = $configFilePool;
  48. $this->driverPool = $driverPool;
  49. }
  50. /**
  51. * Loads the configuration file.
  52. *
  53. * @param string $fileKey The file key
  54. * @return array The configurations array
  55. * @throws FileSystemException If file can not be read
  56. * @throws \Exception If file key is not correct
  57. */
  58. public function load($fileKey)
  59. {
  60. $path = $this->dirList->getPath(DirectoryList::CONFIG);
  61. $fileDriver = $this->driverPool->getDriver(DriverPool::FILE);
  62. $filePath = $path . '/' . $this->configFilePool->getPath($fileKey);
  63. if ($fileDriver->isExists($filePath)) {
  64. return include $filePath;
  65. }
  66. return [];
  67. }
  68. }