Check that a given file is readable

Check that the file exists and that we can read from it.
Otherwise throw an exception with a reason.

@throws RuntimeException
@return void
351646
protected function checkFileReadable($file)
    {
        if (!file_exists($file)) {
            throw new \RuntimeException("File [$file] does not exist");
        }
        if (!is_file($file)) {
            throw new \RuntimeException("File [$file] is not a file");
        }
        if (!is_readable($file)) {
            throw new \RuntimeException("File [$file] is not readable");
        }
    }
38394
public function canRead()
    {
        $fs = FileSystem::getFileSystem();

        if ($fs->checkAccess($this)) {
            return (boolean) @is_link($this->getAbsolutePath()) || @is_readable($this->getAbsolutePath());
        }

        return false;
    }
1352773
public function write($file)
    {
        if (file_exists($file) && !is_writable($file)) {
            throw new RuntimeException("$file is not writable");
        }
        $fp = fopen($file, 'w');
        foreach ($this->lines as $line) {
            fwrite($fp, $line['data']);
        }
        fclose($fp);
    }
498274
protected function ensureFileIsReadable()
    {
        $filePath = $this->filePath;
        if (!is_readable($filePath) || !is_file($filePath)) {
            throw new InvalidArgumentException(sprintf(
                'Dotenv: Environment file .env not found or not readable. '.
                'Create file with your environment settings at %s',
                $filePath
            ));
        }
    }
202767
function isSeekable()
    {
        $this->assertMetaData();
        
        if ($this->getMetaKey('wrapper_type')) {
            // This is a wrapper resource; challenge seekable ...
            if (false === $pos = ftell($this->rHandler))
                throw new \RuntimeException('Unable to determine stream position');

            try {
                // TODO Stream Reach EOF When Seek On it Sometimes!!
                if (-1 === @fseek($this->rHandler, $pos, SEEK_SET))
                    return false;
            } catch (\Exception $e) {
                // Maybe Fallback into wrapper handler for seek
                return false;
            }
        }
        
        return (bool) $this->getMetaKey('seekable');
    }
966454
protected function fileExistsAndReadable ()
	{
		if ($this->isDataStream === true)
		{
			return;
		}
		
		if (stristr($this->fileName, 'http://') !== false)
		{
			$this->remoteImage = true;
			return;
		}
		
		if (!file_exists($this->fileName))
		{
			$this->triggerError('Image file not found: ' . $this->fileName);
		}
		elseif (!is_readable($this->fileName))
		{
			$this->triggerError('Image file not readable: ' . $this->fileName);
		}
	}
651366
public function remove()
    {
        if ($this->removed) {
            return;
        }

        if (!\unlink($this->file)) {
            throw new RuntimeException(sprintf('Impossible to remove file "%s"', $this->file));
        }

        $this->removed = true;
    }
152847
public function read() {
		if (!$this->exists()) {
			throw new FileException(sprintf('File does not exist: %s', $this->getFilename()));
		}

		if (!$this->isReadable()) {
			throw new FileException(sprintf('You don\'t have permissions to access %s file', $this->getFilename()));
		}

		return file_get_contents($this->pathname);
	}
1139290
public static function checkPcreSupport(): void
	{
		static $supported = null;

		if ($supported === null) {
			$supported = @preg_match('/\pL/u', 'a') === 1;
		}

		if (!$supported) {
			throw new \RuntimeException(sprintf('Missing PCRE unicode support'));
		}
	}
1259520
protected function _ensureReadable($bytePerByte = true)
    {
        if (!$this->valid()) {
            throw new RuntimeException('Cannot read from a closed stream.');
        }
        if (!$this->isReadable()) {
            $mode = $this->meta('mode');
            throw new RuntimeException("Cannot read on a non-readable stream (mode is `'{$mode}'`).");
        }
    }
1116576
public function readFrom($input)
    {
        if ($this->isFile($input)) {
            if (is_readable($input) === false) {
                throw new \RuntimeException(
                    sprintf('Unable to parse "%s" as the file is not readable.', $input)
                );
            }

            // $input is a filepath, so we load that file
            $input = file_get_contents($input);
        }

        return trim($input);
    }
318774
private function readNumber()
    {
        // Open
        if (false === $this->handle = fopen($this->path, 'c+')) {
            throw new RuntimeException("Failed to open file {$this->path}.");
        }
        // Exclusive lock
        if (!flock($this->handle, LOCK_EX)) {
            throw new RuntimeException("Failed to lock file {$this->path}.");
        }

        if (false !== $number = fread($this->handle, 10)) {
            return intval($number);
        }

        return 0;
    }
180883
public function getFile($fileName) {
        $files = $this->getFiles();
        $found = null;
        foreach ($files as $file) {
            if($file->getRelativePathname() === $fileName){
                if(!$file->isReadable()){
                    throw new RuntimeException(sprintf("The file '%s' could not be read.",$fileName));
                }
                $found = $file;
                break;
            }
        }
        return $found;
    }
368359
public static function readFile(string $file): string
    {
        $contents = @\file_get_contents($file);
        
        if ($contents === false) {
            throw new \RuntimeException(\sprintf('File not found: "%s"', $file));
        }
        
        return $contents;
    }
337714
public function configure()
    {
        if (!file_exists($this->file) || !is_readable($this->file)) {
            throw new RuntimeException(sprintf('The file "%s" does not exists or is not readable.', $this->file));
        }

        try {
            $parser = new Parser();
            $config = $parser->parse(file_get_contents($this->file));
        } catch (ParseException $exception) {
            throw new RuntimeException(sprintf('Error parsing the file "%s".', $this->file));
        }

        if (array_key_exists('magephp', $config) && is_array($config['magephp'])) {
            $logger = null;
            if (array_key_exists('log_dir', $config['magephp']) && file_exists($config['magephp']['log_dir']) && is_dir($config['magephp']['log_dir'])) {
                $logfile = sprintf('%s/%s.log', $config['magephp']['log_dir'], date('Ymd_His'));
                $config['magephp']['log_file'] = $logfile;

                $logger = new Logger('magephp');
                $logger->pushHandler(new StreamHandler($logfile));

            } elseif (array_key_exists('log_dir', $config['magephp']) && !is_dir($config['magephp']['log_dir'])) {
                throw new RuntimeException(sprintf('The configured log_dir "%s" does not exists or is not a directory.', $config['magephp']['log_dir']));
            }

            $this->runtime->setConfiguration($config['magephp']);
            $this->runtime->setLogger($logger);
            return;
        }

        throw new RuntimeException(sprintf('The file "%s" does not have a valid Magallanes configuration.', $this->file));
    }
782
public function handleError()
    {
        // get error info
        list ($errno, $message, $file, $line) = func_get_args();

        // construct error message
        $msg = "ERROR ($errno): $message";
        if ($line !== null) {
            $file = "$file:$line";
        }

        if ($file !== null) {
            $msg .= " [$file]";
        }

        static::$lastErrno = $errno;

        // throw the exception
        throw new RuntimeException($msg, $errno);
    }
264847
public function run(): void
    {
        if (!$this->config->isLoadedFromFile()) {
            throw new \RuntimeException('No configuration to edit');
        }

        $this->checkHook();
        $this->checkChange();

        $change = $this->createChange();
        $change->applyTo($this->config);
        Config\Util::writeToDisk($this->config);

        $this->io->write('Configuration successfully updated');
    }