Read.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 Read implements ReadInterface
  10. {
  11. /**
  12. * Full path to file
  13. *
  14. * @var string
  15. */
  16. protected $path;
  17. /**
  18. * Mode to open the file
  19. *
  20. * @var string
  21. */
  22. protected $mode = 'r';
  23. /**
  24. * Opened file resource
  25. *
  26. * @var resource
  27. */
  28. protected $resource;
  29. /**
  30. * @var \Magento\Framework\Filesystem\DriverInterface
  31. */
  32. protected $driver;
  33. /**
  34. * Constructor
  35. *
  36. * @param string $path
  37. * @param DriverInterface $driver
  38. */
  39. public function __construct($path, DriverInterface $driver)
  40. {
  41. $this->path = $path;
  42. $this->driver = $driver;
  43. $this->open();
  44. }
  45. /**
  46. * Open file
  47. *
  48. * @return $this
  49. */
  50. protected function open()
  51. {
  52. $this->assertValid();
  53. $this->resource = $this->driver->fileOpen($this->path, $this->mode);
  54. return $this;
  55. }
  56. /**
  57. * Assert file existence
  58. *
  59. * @return bool
  60. * @throws \Magento\Framework\Exception\FileSystemException
  61. */
  62. protected function assertValid()
  63. {
  64. if (!$this->driver->isExists($this->path)) {
  65. throw new FileSystemException(
  66. new \Magento\Framework\Phrase('The "%1" file doesn\'t exist.', [$this->path])
  67. );
  68. }
  69. return true;
  70. }
  71. /**
  72. * Reads the specified number of bytes from the current position.
  73. *
  74. * @param int $length The number of bytes to read
  75. * @return string
  76. */
  77. public function read($length)
  78. {
  79. return $this->driver->fileRead($this->resource, $length);
  80. }
  81. /**
  82. * Return file content
  83. *
  84. * @param string|null $flag
  85. * @param resource|null $context
  86. * @return string
  87. */
  88. public function readAll($flag = null, $context = null)
  89. {
  90. return $this->driver->fileGetContents($this->path, $flag, $context);
  91. }
  92. /**
  93. * Reads the line with specified number of bytes from the current position.
  94. *
  95. * @param int $length The number of bytes to read
  96. * @param string $ending [optional]
  97. * @return string
  98. */
  99. public function readLine($length, $ending = null)
  100. {
  101. return $this->driver->fileReadLine($this->resource, $length, $ending);
  102. }
  103. /**
  104. * Reads one CSV row from the file
  105. *
  106. * @param int $length [optional]
  107. * @param string $delimiter [optional]
  108. * @param string $enclosure [optional]
  109. * @param string $escape [optional]
  110. * @return array|bool|null
  111. */
  112. public function readCsv($length = 0, $delimiter = ',', $enclosure = '"', $escape = '\\')
  113. {
  114. return $this->driver->fileGetCsv($this->resource, $length, $delimiter, $enclosure, $escape);
  115. }
  116. /**
  117. * Returns the current cursor position
  118. *
  119. * @return int
  120. */
  121. public function tell()
  122. {
  123. return $this->driver->fileTell($this->resource);
  124. }
  125. /**
  126. * Seeks to the specified offset
  127. *
  128. * @param int $offset
  129. * @param int $whence
  130. * @return int
  131. */
  132. public function seek($offset, $whence = SEEK_SET)
  133. {
  134. return $this->driver->fileSeek($this->resource, $offset, $whence);
  135. }
  136. /**
  137. * Checks if the current position is the end-of-file
  138. *
  139. * @return bool
  140. */
  141. public function eof()
  142. {
  143. return $this->driver->endOfFile($this->resource);
  144. }
  145. /**
  146. * Closes the file.
  147. *
  148. * @return bool
  149. */
  150. public function close()
  151. {
  152. return $this->driver->fileClose($this->resource);
  153. }
  154. /**
  155. * {@inheritDoc}
  156. *
  157. * @return array
  158. */
  159. public function stat()
  160. {
  161. return $this->driver->stat($this->path);
  162. }
  163. }