Config.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\MediaStorage\Model\File\Storage;
  7. use Magento\Framework\App\Filesystem\DirectoryList;
  8. use Magento\Framework\Filesystem\Directory\WriteInterface as DirectoryWrite;
  9. use Magento\Framework\Filesystem\File\Write;
  10. use Magento\Framework\Exception\FileSystemException;
  11. class Config
  12. {
  13. /**
  14. * Config cache file path
  15. *
  16. * @var string
  17. */
  18. protected $cacheFilePath;
  19. /**
  20. * Loaded config
  21. *
  22. * @var array
  23. */
  24. protected $config;
  25. /**
  26. * File stream handler
  27. *
  28. * @var DirectoryWrite
  29. */
  30. protected $rootDirectory;
  31. /**
  32. * @param \Magento\MediaStorage\Model\File\Storage $storage
  33. * @param \Magento\Framework\Filesystem $filesystem
  34. * @param string $cacheFile
  35. */
  36. public function __construct(
  37. \Magento\MediaStorage\Model\File\Storage $storage,
  38. \Magento\Framework\Filesystem $filesystem,
  39. $cacheFile
  40. ) {
  41. $this->config = $storage->getScriptConfig();
  42. $this->rootDirectory = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
  43. $this->cacheFilePath = $cacheFile;
  44. }
  45. /**
  46. * Retrieve media directory
  47. *
  48. * @return string
  49. */
  50. public function getMediaDirectory()
  51. {
  52. return $this->config['media_directory'];
  53. }
  54. /**
  55. * Retrieve list of allowed resources
  56. *
  57. * @return array
  58. */
  59. public function getAllowedResources()
  60. {
  61. return $this->config['allowed_resources'];
  62. }
  63. /**
  64. * Save config in cache file
  65. *
  66. * @return void
  67. */
  68. public function save()
  69. {
  70. /** @var Write $file */
  71. $file = $this->rootDirectory->openFile($this->rootDirectory->getRelativePath($this->cacheFilePath), 'w');
  72. try {
  73. $file->lock();
  74. $file->write(json_encode($this->config));
  75. $file->unlock();
  76. $file->close();
  77. } catch (FileSystemException $e) {
  78. $file->close();
  79. }
  80. }
  81. }