Autarky\Files\LockingFilesystem::write PHP Метод

write() публичный Метод

Write to the file.
public write ( string $path, string $contents, boolean $blocking = false ) : void
$path string Path to the file.
$contents string Contents to write to the file.
$blocking boolean Wait for other locks to expire rather than throwing an error when a lock cannot be aquired.
Результат void
    public function write($path, $contents, $blocking = false)
    {
        $flockFlags = $blocking ? LOCK_EX : LOCK_EX | LOCK_NB;
        $file = fopen($path, 'c');
        if (!flock($file, $flockFlags)) {
            fclose($file);
            throw new IOException("Could not aquire file lock for file: {$path}");
        }
        ftruncate($file, 0);
        fwrite($file, $contents);
        fflush($file);
        flock($file, LOCK_UN | LOCK_NB);
        fclose($file);
    }

Usage Example

Пример #1
0
 protected function generateDispatchData()
 {
     $data = $this->routeCollector->getData();
     if ($this->cachePath !== null) {
         $filesys = new LockingFilesystem();
         $php = '<?php return ' . var_export($data, true) . ";\n";
         $filesys->write($this->cachePath, $php);
     }
     return $data;
 }
LockingFilesystem