Read.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Filesystem\Directory;
  7. use Magento\Framework\Exception\FileSystemException;
  8. use Magento\Framework\Exception\ValidatorException;
  9. /**
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class Read implements ReadInterface
  14. {
  15. /**
  16. * Directory path
  17. *
  18. * @var string
  19. */
  20. protected $path;
  21. /**
  22. * File factory
  23. *
  24. * @var \Magento\Framework\Filesystem\File\ReadFactory
  25. */
  26. protected $fileFactory;
  27. /**
  28. * Filesystem driver
  29. *
  30. * @var \Magento\Framework\Filesystem\DriverInterface
  31. */
  32. protected $driver;
  33. /**
  34. * @var PathValidatorInterface|null
  35. */
  36. private $pathValidator;
  37. /**
  38. * Constructor. Set properties.
  39. *
  40. * @param \Magento\Framework\Filesystem\File\ReadFactory $fileFactory
  41. * @param \Magento\Framework\Filesystem\DriverInterface $driver
  42. * @param string $path
  43. * @param PathValidatorInterface|null $pathValidator
  44. */
  45. public function __construct(
  46. \Magento\Framework\Filesystem\File\ReadFactory $fileFactory,
  47. \Magento\Framework\Filesystem\DriverInterface $driver,
  48. $path,
  49. ?PathValidatorInterface $pathValidator = null
  50. ) {
  51. $this->fileFactory = $fileFactory;
  52. $this->driver = $driver;
  53. $this->setPath($path);
  54. $this->pathValidator = $pathValidator;
  55. }
  56. /**
  57. * @param null|string $path
  58. * @param null|string $scheme
  59. * @param bool $absolutePath
  60. * @throws ValidatorException
  61. *
  62. * @return void
  63. * @since 101.0.7
  64. */
  65. protected function validatePath(
  66. ?string $path,
  67. ?string $scheme = null,
  68. bool $absolutePath = false
  69. ): void {
  70. if ($path && $this->pathValidator) {
  71. $this->pathValidator->validate(
  72. $this->path,
  73. $path,
  74. $scheme,
  75. $absolutePath
  76. );
  77. }
  78. }
  79. /**
  80. * Sets base path
  81. *
  82. * @param string $path
  83. * @return void
  84. */
  85. protected function setPath($path)
  86. {
  87. if (!empty($path)) {
  88. $this->path = rtrim(str_replace('\\', '/', $path), '/') . '/';
  89. }
  90. }
  91. /**
  92. * Retrieves absolute path
  93. * E.g.: /var/www/application/file.txt
  94. *
  95. * @param string $path
  96. * @param string $scheme
  97. * @throws ValidatorException
  98. * @return string
  99. */
  100. public function getAbsolutePath($path = null, $scheme = null)
  101. {
  102. $this->validatePath($path, $scheme);
  103. return $this->driver->getAbsolutePath($this->path, $path, $scheme);
  104. }
  105. /**
  106. * Retrieves relative path
  107. *
  108. * @param string $path
  109. * @throws ValidatorException
  110. * @return string
  111. */
  112. public function getRelativePath($path = null)
  113. {
  114. $this->validatePath(
  115. $path,
  116. null,
  117. $path && $path[0] === DIRECTORY_SEPARATOR
  118. );
  119. return $this->driver->getRelativePath($this->path, $path);
  120. }
  121. /**
  122. * Retrieve list of all entities in given path
  123. *
  124. * @param string|null $path
  125. * @throws ValidatorException
  126. * @return string[]
  127. */
  128. public function read($path = null)
  129. {
  130. $this->validatePath($path);
  131. $files = $this->driver->readDirectory($this->driver->getAbsolutePath($this->path, $path));
  132. $result = [];
  133. foreach ($files as $file) {
  134. $result[] = $this->getRelativePath($file);
  135. }
  136. return $result;
  137. }
  138. /**
  139. * Read recursively
  140. *
  141. * @param null $path
  142. * @throws ValidatorException
  143. * @return string[]
  144. */
  145. public function readRecursively($path = null)
  146. {
  147. $this->validatePath($path);
  148. $result = [];
  149. $paths = $this->driver->readDirectoryRecursively($this->driver->getAbsolutePath($this->path, $path));
  150. /** @var \FilesystemIterator $file */
  151. foreach ($paths as $file) {
  152. $result[] = $this->getRelativePath($file);
  153. }
  154. sort($result);
  155. return $result;
  156. }
  157. /**
  158. * Search all entries for given regex pattern
  159. *
  160. * @param string $pattern
  161. * @param string $path [optional]
  162. * @throws ValidatorException
  163. * @return string[]
  164. */
  165. public function search($pattern, $path = null)
  166. {
  167. $this->validatePath($path);
  168. if ($path) {
  169. $absolutePath = $this->driver->getAbsolutePath($this->path, $this->getRelativePath($path));
  170. } else {
  171. $absolutePath = $this->path;
  172. }
  173. $files = $this->driver->search($pattern, $absolutePath);
  174. $result = [];
  175. foreach ($files as $file) {
  176. $result[] = $this->getRelativePath($file);
  177. }
  178. return $result;
  179. }
  180. /**
  181. * Check a file or directory exists
  182. *
  183. * @param string $path [optional]
  184. * @return bool
  185. * @throws \Magento\Framework\Exception\FileSystemException
  186. * @throws ValidatorException
  187. */
  188. public function isExist($path = null)
  189. {
  190. $this->validatePath($path);
  191. return $this->driver->isExists($this->driver->getAbsolutePath($this->path, $path));
  192. }
  193. /**
  194. * Gathers the statistics of the given path
  195. *
  196. * @param string $path
  197. * @return array
  198. * @throws \Magento\Framework\Exception\FileSystemException
  199. * @throws ValidatorException
  200. */
  201. public function stat($path)
  202. {
  203. $this->validatePath($path);
  204. return $this->driver->stat($this->driver->getAbsolutePath($this->path, $path));
  205. }
  206. /**
  207. * Check permissions for reading file or directory
  208. *
  209. * @param string $path [optional]
  210. * @return bool
  211. * @throws \Magento\Framework\Exception\FileSystemException
  212. * @throws ValidatorException
  213. */
  214. public function isReadable($path = null)
  215. {
  216. $this->validatePath($path);
  217. return $this->driver->isReadable($this->driver->getAbsolutePath($this->path, $path));
  218. }
  219. /**
  220. * Open file in read mode
  221. *
  222. * @param string $path
  223. * @throws ValidatorException
  224. *
  225. * @return \Magento\Framework\Filesystem\File\ReadInterface
  226. */
  227. public function openFile($path)
  228. {
  229. $this->validatePath($path);
  230. return $this->fileFactory->create(
  231. $this->driver->getAbsolutePath($this->path, $path),
  232. $this->driver
  233. );
  234. }
  235. /**
  236. * Retrieve file contents from given path
  237. *
  238. * @param string $path
  239. * @param string|null $flag
  240. * @param resource|null $context
  241. * @return string
  242. * @throws FileSystemException
  243. * @throws ValidatorException
  244. */
  245. public function readFile($path, $flag = null, $context = null)
  246. {
  247. $this->validatePath($path);
  248. $absolutePath = $this->driver->getAbsolutePath($this->path, $path);
  249. return $this->driver->fileGetContents($absolutePath, $flag, $context);
  250. }
  251. /**
  252. * Check whether given path is file
  253. *
  254. * @param string $path
  255. * @throws ValidatorException
  256. * @return bool
  257. */
  258. public function isFile($path)
  259. {
  260. $this->validatePath($path);
  261. return $this->driver->isFile($this->driver->getAbsolutePath($this->path, $path));
  262. }
  263. /**
  264. * Check whether given path is directory
  265. *
  266. * @param string $path [optional]
  267. * @throws ValidatorException
  268. * @return bool
  269. */
  270. public function isDirectory($path = null)
  271. {
  272. $this->validatePath($path);
  273. return $this->driver->isDirectory($this->driver->getAbsolutePath($this->path, $path));
  274. }
  275. }