Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Read.php
Go to the documentation of this file.
1 <?php
7 
10 
15 class Read implements ReadInterface
16 {
22  protected $path;
23 
29  protected $fileFactory;
30 
36  protected $driver;
37 
41  private $pathValidator;
42 
51  public function __construct(
54  $path,
55  ?PathValidatorInterface $pathValidator = null
56  ) {
57  $this->fileFactory = $fileFactory;
58  $this->driver = $driver;
59  $this->setPath($path);
60  $this->pathValidator = $pathValidator;
61  }
62 
71  protected function validatePath(
72  ?string $path,
73  ?string $scheme = null,
74  bool $absolutePath = false
75  ): void {
76  if ($path && $this->pathValidator) {
77  $this->pathValidator->validate(
78  $this->path,
79  $path,
80  $scheme,
81  $absolutePath
82  );
83  }
84  }
85 
92  protected function setPath($path)
93  {
94  if (!empty($path)) {
95  $this->path = rtrim(str_replace('\\', '/', $path), '/') . '/';
96  }
97  }
98 
108  public function getAbsolutePath($path = null, $scheme = null)
109  {
110  $this->validatePath($path, $scheme);
111 
112  return $this->driver->getAbsolutePath($this->path, $path, $scheme);
113  }
114 
122  public function getRelativePath($path = null)
123  {
124  $this->validatePath(
125  $path,
126  null,
127  $path && $path[0] === DIRECTORY_SEPARATOR
128  );
129 
130  return $this->driver->getRelativePath($this->path, $path);
131  }
132 
140  public function read($path = null)
141  {
142  $this->validatePath($path);
143 
144  $files = $this->driver->readDirectory($this->driver->getAbsolutePath($this->path, $path));
145  $result = [];
146  foreach ($files as $file) {
147  $result[] = $this->getRelativePath($file);
148  }
149  return $result;
150  }
151 
159  public function readRecursively($path = null)
160  {
161  $this->validatePath($path);
162 
163  $result = [];
164  $paths = $this->driver->readDirectoryRecursively($this->driver->getAbsolutePath($this->path, $path));
166  foreach ($paths as $file) {
167  $result[] = $this->getRelativePath($file);
168  }
169  sort($result);
170  return $result;
171  }
172 
181  public function search($pattern, $path = null)
182  {
183  $this->validatePath($path);
184 
185  if ($path) {
186  $absolutePath = $this->driver->getAbsolutePath($this->path, $this->getRelativePath($path));
187  } else {
188  $absolutePath = $this->path;
189  }
190 
191  $files = $this->driver->search($pattern, $absolutePath);
192  $result = [];
193  foreach ($files as $file) {
194  $result[] = $this->getRelativePath($file);
195  }
196  return $result;
197  }
198 
207  public function isExist($path = null)
208  {
209  $this->validatePath($path);
210 
211  return $this->driver->isExists($this->driver->getAbsolutePath($this->path, $path));
212  }
213 
222  public function stat($path)
223  {
224  $this->validatePath($path);
225 
226  return $this->driver->stat($this->driver->getAbsolutePath($this->path, $path));
227  }
228 
237  public function isReadable($path = null)
238  {
239  $this->validatePath($path);
240 
241  return $this->driver->isReadable($this->driver->getAbsolutePath($this->path, $path));
242  }
243 
252  public function openFile($path)
253  {
254  $this->validatePath($path);
255 
256  return $this->fileFactory->create(
257  $this->driver->getAbsolutePath($this->path, $path),
259  );
260  }
261 
272  public function readFile($path, $flag = null, $context = null)
273  {
274  $this->validatePath($path);
275 
276  $absolutePath = $this->driver->getAbsolutePath($this->path, $path);
277  return $this->driver->fileGetContents($absolutePath, $flag, $context);
278  }
279 
287  public function isFile($path)
288  {
289  $this->validatePath($path);
290 
291  return $this->driver->isFile($this->driver->getAbsolutePath($this->path, $path));
292  }
293 
301  public function isDirectory($path = null)
302  {
303  $this->validatePath($path);
304 
305  return $this->driver->isDirectory($this->driver->getAbsolutePath($this->path, $path));
306  }
307 }
__construct(\Magento\Framework\Filesystem\File\ReadFactory $fileFactory, \Magento\Framework\Filesystem\DriverInterface $driver, $path, ?PathValidatorInterface $pathValidator=null)
Definition: Read.php:51
$pattern
Definition: website.php:22
readFile($path, $flag=null, $context=null)
Definition: Read.php:272
validatePath(?string $path, ?string $scheme=null, bool $absolutePath=false)
Definition: Read.php:71
getAbsolutePath($path=null, $scheme=null)
Definition: Read.php:108
$paths
Definition: _bootstrap.php:83
foreach($appDirs as $dir) $files