/** * @param string $fileName * @param string $content * @throws FileSystemException * @return bool */ public function writeResultFile($fileName, $content) { /** * Rename is atomic on *nix systems, while file_put_contents is not. Writing to a * temporary file whose name is process-unique and renaming to the real location helps * avoid race conditions. Race condition can occur if the compiler has not been run, when * multiple processes are attempting to access the generated file simultaneously. */ $content = "<?php\n" . $content; $tmpFile = $fileName . "." . getmypid(); $this->filesystemDriver->filePutContents($tmpFile, $content); try { $success = $this->filesystemDriver->rename($tmpFile, $fileName); } catch (FileSystemException $e) { if (!$this->fileExists($fileName)) { throw $e; } else { /** * Due to race conditions, file may have already been written, causing rename to fail. As long as * the file exists, everything is okay. */ $success = true; } } return $success; }