Write.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Filesystem\File;
  7. use Magento\Framework\Filesystem\DriverInterface;
  8. use Magento\Framework\Exception\FileSystemException;
  9. class Write extends Read implements WriteInterface
  10. {
  11. /**
  12. * Constructor
  13. *
  14. * @param string $path
  15. * @param DriverInterface $driver
  16. * @param string $mode
  17. */
  18. public function __construct($path, DriverInterface $driver, $mode)
  19. {
  20. $this->mode = $mode;
  21. parent::__construct($path, $driver);
  22. }
  23. /**
  24. * Assert file existence for proper mode
  25. *
  26. * @return void
  27. * @throws \Magento\Framework\Exception\FileSystemException
  28. */
  29. protected function assertValid()
  30. {
  31. $fileExists = $this->driver->isExists($this->path);
  32. if (!$fileExists && preg_match('/r/', $this->mode)) {
  33. throw new FileSystemException(
  34. new \Magento\Framework\Phrase('The "%1" file doesn\'t exist.', [$this->path])
  35. );
  36. } elseif ($fileExists && preg_match('/x/', $this->mode)) {
  37. throw new FileSystemException(new \Magento\Framework\Phrase('The file "%1" already exists', [$this->path]));
  38. }
  39. }
  40. /**
  41. * Writes the data to file.
  42. *
  43. * @param string $data
  44. * @return int
  45. * @throws FileSystemException
  46. */
  47. public function write($data)
  48. {
  49. try {
  50. return $this->driver->fileWrite($this->resource, $data);
  51. } catch (FileSystemException $e) {
  52. throw new FileSystemException(
  53. new \Magento\Framework\Phrase('Cannot write to the "%1" file. %2', [$this->path, $e->getMessage()])
  54. );
  55. }
  56. }
  57. /**
  58. * Writes one CSV row to the file.
  59. *
  60. * @param array $data
  61. * @param string $delimiter
  62. * @param string $enclosure
  63. * @return int
  64. * @throws FileSystemException
  65. */
  66. public function writeCsv(array $data, $delimiter = ',', $enclosure = '"')
  67. {
  68. try {
  69. return $this->driver->filePutCsv($this->resource, $data, $delimiter, $enclosure);
  70. } catch (FileSystemException $e) {
  71. throw new FileSystemException(
  72. new \Magento\Framework\Phrase('Cannot write to the "%1" file. %2', [$this->path, $e->getMessage()])
  73. );
  74. }
  75. }
  76. /**
  77. * Flushes the output.
  78. *
  79. * @return bool
  80. * @throws FileSystemException
  81. */
  82. public function flush()
  83. {
  84. try {
  85. return $this->driver->fileFlush($this->resource);
  86. } catch (FileSystemException $e) {
  87. throw new FileSystemException(
  88. new \Magento\Framework\Phrase('Cannot flush the "%1" file. %2', [$this->path, $e->getMessage()])
  89. );
  90. }
  91. }
  92. /**
  93. * Portable advisory file locking
  94. *
  95. * @param int $lockMode
  96. * @return bool
  97. */
  98. public function lock($lockMode = \LOCK_EX)
  99. {
  100. return $this->driver->fileLock($this->resource, $lockMode);
  101. }
  102. /**
  103. * File unlocking
  104. *
  105. * @return bool
  106. */
  107. public function unlock()
  108. {
  109. return $this->driver->fileUnlock($this->resource);
  110. }
  111. }