Autarky\Files\LockingFilesystem::read PHP Method

read() public method

Read from a file.
public read ( string $path, boolean $blocking = false ) : string
$path string Path to the file.
$blocking boolean Wait for other locks to expire rather than throwing an error when a lock cannot be aquired.
return string
    public function read($path, $blocking = false)
    {
        $size = filesize($path);
        if ($size === 0) {
            return '';
        }
        $flockFlags = $blocking ? LOCK_SH : LOCK_SH | LOCK_NB;
        $file = fopen($path, 'r');
        if (!flock($file, $flockFlags)) {
            fclose($file);
            throw new IOException("Could not aquire file lock for file: {$path}");
        }
        $contents = fread($file, $size);
        flock($file, LOCK_UN | LOCK_NB);
        fclose($file);
        return $contents;
    }

Usage Example

Beispiel #1
0
 /**
  * {@inheritdoc}
  */
 public function load($path)
 {
     $yaml = $this->filesys->read($path);
     try {
         return $this->parser->parse($yaml);
     } catch (ParseException $e) {
         throw new LoadException($e->getMessage(), $e->getCode(), $e);
     }
 }
LockingFilesystem