Atomic file writer.

@param string $file
@param string $contents
@param int $permissions
@return string The location of the generated file.
505303
public static function writeFile(string $file, string $contents, int $permissions = 0777): string
    {
        self::createDirectory(\dirname($file));
        
        $tempFile = \tempnam(\sys_get_temp_dir(), self::TEMP_FILE_PREFIX);
        
        \file_put_contents($tempFile, $contents);
        
        if (\DIRECTORY_SEPARATOR == '\\') {
            if (\is_file($file)) {
                @\unlink($file);
            }
        }
        
        if (!@\rename($tempFile, $file)) {
            throw new \RuntimeException(\sprintf('Could not move temp file to "%s"%s', $file, self::getErrorInfo()));
        }
        
        if (\DIRECTORY_SEPARATOR != '\\') {
            self::changePermissions($file, $permissions);
        }
        
        \clearstatcache(true, $file);
        
        return $file;
    }
834657
public function create($permissions = 0755)
    {
        Filesystem::checkNotExists($this->path);
        Filesystem::checkWritable(dirname($this->path));
        mkdir($this->path, $permissions, true);
    }
870263
protected function setPermissions($permissions)
    {
        foreach ($permissions as $action => $permission) {
            $this->setPermission($action, $permission);
        }
    }
50099
protected function writeCacheFile($file, $content)
  {
    parent::writeCacheFile($file, $content);

    // Compile cached-file into bytecode-cache
    if (function_exists('apc_compile_file') === true) {
      apc_compile_file($file);
    }
  }
1038517
public static function write($file = false, $content = false) {

        if ($file && $content) {

            if (file_put_contents($file, $content)) {

                return true;
            }
        }

        return false;
    }
264239
public static function writeFile($file, $content) {
		if (!@is_file($file)) {
			$changePermissions = TRUE;
		}

		if ($fd = fopen($file, 'wb')) {
			$res = fwrite($fd, $content);
			fclose($fd);

			if ($res === FALSE) {
				return FALSE;
			}

			if ($changePermissions) { // Change the permissions only if the file has just been created
				self::fixPermissions($file);
			}

			return TRUE;
		}

		return FALSE;
	}
946851
public static function generateFile($file, array $content) {
		$string = static::buildIniString($content);

		return (bool)file_put_contents($file, $string);
	}
1000445
public function writePoFile(string $file): void
    {
        $source = $this->dumpString();
        $testName = file_exists($file) ? $file : dirname($file);
        $status = is_writable($testName);
        if ($status === true) {
            $status = file_put_contents($file, $source);
        }
        if (false === $status) {
            throw new FileNotWritableException($file);
        }
    }
45174
protected function save($contents)
    {
        $fp = fopen($this->fileName, $this->openMode);
        fwrite($fp, $contents);
        fclose($fp);

        chmod($this->fileName, $this->getPermission());
    }
896202
public function compactContents($file, $contents)
    {
        foreach ($this->compactors as $compactor) {
            /** @var $compactor CompactorInterface */
            if ($compactor->supports($file)) {
                $contents = $compactor->compact($contents);
            }
        }

        return $contents;
    }
90361
public function canAll($permissions, &$failed = null)
    {
        $this->canAny($permissions, $failed);

        return $failed->isEmpty();
    }
770237
public function chmod($permission,$file)
    {
        if (! ftp_chmod($this->session, $permission, $file)) {
            throw new \RuntimeException('Chmod can not be changed');
        }
        return true;
    }
362236
public function mkdir($path, $permissions = null)
	{
		if ( ! $this->_is_conn())
		{
			return false;
		}

		$result = ftp_mkdir($this->_conn_id, $path);

		if ($result === false)
		{
			if ($this->_debug == true)
			{
				throw new \FtpFileAccessException('Unable to create directory');
			}
			return false;
		}

		// Set file permissions if needed
		if ($permissions !== null)
		{
			$this->chmod($path, (int) $permissions);
		}

		return true;
	}
886444
private function writeManifest($file, ResourcesInterface $resources)
    {
        $mask = 0775 & ~umask();
        if (!is_dir($dir = dirname($file))) {
            if (false === mkdir($dir, $mask, true)) {
                throw new RuntimeException('Creating manifest for router cache failed.');
            }
        } elseif (false === @chmod($dir, $mask)) {
            throw new RuntimeException('Cannot apply permissions on cache directory.');
        }

        file_put_contents($file, serialize($resources), LOCK_EX);
    }
132267
protected function writeFile(string $contents, string $file): GraphvizWriter
    {
        if (!file_put_contents($file, $contents)) {
            throw ExportException::create(
                'Couldn\'t write to file "'.$file.'".'
            );
        }

        return $this;
    }
353323
public function chmod($permissions = 0, $remote_file = null)
    {
        if (\ftp_chmod($this->_stream, $permissions, $remote_file)) {
            return true;
        }

        $this->error = 'Failed to set file permissions for "'.$remote_file.'"';

        return false;
    }
1125577
protected function writeCacheFile($file, $content)
    {
        $dir = dirname($file);

        if (!is_dir($dir)) {
            if (false === @mkdir($dir, 0777, true) && !is_dir($dir)) {
                throw new RuntimeException("Unable to create the cache directory ($dir).");
            }
        } elseif (!is_writable($dir)) {
            throw new RuntimeException("Unable to write in the cache directory ($dir).");
        }

        if (!file_put_contents($file, $content)) {
            throw new RuntimeException("Failed to write cache file ($file).");
        }
    }