123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Framework\Filesystem\Directory;
- use Magento\Framework\Exception\FileSystemException;
- use Magento\Framework\Exception\ValidatorException;
- /**
- * @api
- * @since 100.0.2
- */
- class Read implements ReadInterface
- {
- /**
- * Directory path
- *
- * @var string
- */
- protected $path;
- /**
- * File factory
- *
- * @var \Magento\Framework\Filesystem\File\ReadFactory
- */
- protected $fileFactory;
- /**
- * Filesystem driver
- *
- * @var \Magento\Framework\Filesystem\DriverInterface
- */
- protected $driver;
- /**
- * @var PathValidatorInterface|null
- */
- private $pathValidator;
- /**
- * Constructor. Set properties.
- *
- * @param \Magento\Framework\Filesystem\File\ReadFactory $fileFactory
- * @param \Magento\Framework\Filesystem\DriverInterface $driver
- * @param string $path
- * @param PathValidatorInterface|null $pathValidator
- */
- public function __construct(
- \Magento\Framework\Filesystem\File\ReadFactory $fileFactory,
- \Magento\Framework\Filesystem\DriverInterface $driver,
- $path,
- ?PathValidatorInterface $pathValidator = null
- ) {
- $this->fileFactory = $fileFactory;
- $this->driver = $driver;
- $this->setPath($path);
- $this->pathValidator = $pathValidator;
- }
- /**
- * @param null|string $path
- * @param null|string $scheme
- * @param bool $absolutePath
- * @throws ValidatorException
- *
- * @return void
- * @since 101.0.7
- */
- protected function validatePath(
- ?string $path,
- ?string $scheme = null,
- bool $absolutePath = false
- ): void {
- if ($path && $this->pathValidator) {
- $this->pathValidator->validate(
- $this->path,
- $path,
- $scheme,
- $absolutePath
- );
- }
- }
- /**
- * Sets base path
- *
- * @param string $path
- * @return void
- */
- protected function setPath($path)
- {
- if (!empty($path)) {
- $this->path = rtrim(str_replace('\\', '/', $path), '/') . '/';
- }
- }
- /**
- * Retrieves absolute path
- * E.g.: /var/www/application/file.txt
- *
- * @param string $path
- * @param string $scheme
- * @throws ValidatorException
- * @return string
- */
- public function getAbsolutePath($path = null, $scheme = null)
- {
- $this->validatePath($path, $scheme);
- return $this->driver->getAbsolutePath($this->path, $path, $scheme);
- }
- /**
- * Retrieves relative path
- *
- * @param string $path
- * @throws ValidatorException
- * @return string
- */
- public function getRelativePath($path = null)
- {
- $this->validatePath(
- $path,
- null,
- $path && $path[0] === DIRECTORY_SEPARATOR
- );
- return $this->driver->getRelativePath($this->path, $path);
- }
- /**
- * Retrieve list of all entities in given path
- *
- * @param string|null $path
- * @throws ValidatorException
- * @return string[]
- */
- public function read($path = null)
- {
- $this->validatePath($path);
- $files = $this->driver->readDirectory($this->driver->getAbsolutePath($this->path, $path));
- $result = [];
- foreach ($files as $file) {
- $result[] = $this->getRelativePath($file);
- }
- return $result;
- }
- /**
- * Read recursively
- *
- * @param null $path
- * @throws ValidatorException
- * @return string[]
- */
- public function readRecursively($path = null)
- {
- $this->validatePath($path);
- $result = [];
- $paths = $this->driver->readDirectoryRecursively($this->driver->getAbsolutePath($this->path, $path));
- /** @var \FilesystemIterator $file */
- foreach ($paths as $file) {
- $result[] = $this->getRelativePath($file);
- }
- sort($result);
- return $result;
- }
- /**
- * Search all entries for given regex pattern
- *
- * @param string $pattern
- * @param string $path [optional]
- * @throws ValidatorException
- * @return string[]
- */
- public function search($pattern, $path = null)
- {
- $this->validatePath($path);
- if ($path) {
- $absolutePath = $this->driver->getAbsolutePath($this->path, $this->getRelativePath($path));
- } else {
- $absolutePath = $this->path;
- }
- $files = $this->driver->search($pattern, $absolutePath);
- $result = [];
- foreach ($files as $file) {
- $result[] = $this->getRelativePath($file);
- }
- return $result;
- }
- /**
- * Check a file or directory exists
- *
- * @param string $path [optional]
- * @return bool
- * @throws \Magento\Framework\Exception\FileSystemException
- * @throws ValidatorException
- */
- public function isExist($path = null)
- {
- $this->validatePath($path);
- return $this->driver->isExists($this->driver->getAbsolutePath($this->path, $path));
- }
- /**
- * Gathers the statistics of the given path
- *
- * @param string $path
- * @return array
- * @throws \Magento\Framework\Exception\FileSystemException
- * @throws ValidatorException
- */
- public function stat($path)
- {
- $this->validatePath($path);
- return $this->driver->stat($this->driver->getAbsolutePath($this->path, $path));
- }
- /**
- * Check permissions for reading file or directory
- *
- * @param string $path [optional]
- * @return bool
- * @throws \Magento\Framework\Exception\FileSystemException
- * @throws ValidatorException
- */
- public function isReadable($path = null)
- {
- $this->validatePath($path);
- return $this->driver->isReadable($this->driver->getAbsolutePath($this->path, $path));
- }
- /**
- * Open file in read mode
- *
- * @param string $path
- * @throws ValidatorException
- *
- * @return \Magento\Framework\Filesystem\File\ReadInterface
- */
- public function openFile($path)
- {
- $this->validatePath($path);
- return $this->fileFactory->create(
- $this->driver->getAbsolutePath($this->path, $path),
- $this->driver
- );
- }
- /**
- * Retrieve file contents from given path
- *
- * @param string $path
- * @param string|null $flag
- * @param resource|null $context
- * @return string
- * @throws FileSystemException
- * @throws ValidatorException
- */
- public function readFile($path, $flag = null, $context = null)
- {
- $this->validatePath($path);
- $absolutePath = $this->driver->getAbsolutePath($this->path, $path);
- return $this->driver->fileGetContents($absolutePath, $flag, $context);
- }
- /**
- * Check whether given path is file
- *
- * @param string $path
- * @throws ValidatorException
- * @return bool
- */
- public function isFile($path)
- {
- $this->validatePath($path);
- return $this->driver->isFile($this->driver->getAbsolutePath($this->path, $path));
- }
- /**
- * Check whether given path is directory
- *
- * @param string $path [optional]
- * @throws ValidatorException
- * @return bool
- */
- public function isDirectory($path = null)
- {
- $this->validatePath($path);
- return $this->driver->isDirectory($this->driver->getAbsolutePath($this->path, $path));
- }
- }
|