Skip to content

Commit

Permalink
Simplify writing streams on local filesystem
Browse files Browse the repository at this point in the history
  • Loading branch information
frankdejonge committed Nov 28, 2021
1 parent 22b57f2 commit f9b5d82
Showing 1 changed file with 12 additions and 19 deletions.
31 changes: 12 additions & 19 deletions src/Local/LocalFilesystemAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace League\Flysystem\Local;

use function file_put_contents;
use const DIRECTORY_SEPARATOR;
use const LOCK_EX;
use DirectoryIterator;
Expand Down Expand Up @@ -97,36 +98,28 @@ public function __construct(

public function write(string $path, string $contents, Config $config): void
{
$prefixedLocation = $this->prefixer->prefixPath($path);
$this->ensureDirectoryExists(
dirname($prefixedLocation),
$this->resolveDirectoryVisibility($config->get(Config::OPTION_DIRECTORY_VISIBILITY))
);
error_clear_last();

if (@file_put_contents($prefixedLocation, $contents, $this->writeFlags) === false) {
throw UnableToWriteFile::atLocation($path, error_get_last()['message'] ?? '');
}

if ($visibility = $config->get(Config::OPTION_VISIBILITY)) {
$this->setVisibility($path, (string) $visibility);
}
$this->writeToFile($path, $contents, $config);
}

public function writeStream(string $path, $contents, Config $config): void
{
$this->writeToFile($path, $contents, $config);
}

/**
* @param resource|string $contents
*/
private function writeToFile(string $path, $contents, Config $config): void
{
$prefixedLocation = $this->prefixer->prefixPath($path);
$this->ensureDirectoryExists(
dirname($prefixedLocation),
$this->resolveDirectoryVisibility($config->get(Config::OPTION_DIRECTORY_VISIBILITY))
);

error_clear_last();
$stream = @fopen($prefixedLocation, 'w+b');

if ( ! ($stream && false !== stream_copy_to_stream($contents, $stream) && fclose($stream))) {
$reason = error_get_last()['message'] ?? '';
throw UnableToWriteFile::atLocation($prefixedLocation, $reason);
if (@file_put_contents($prefixedLocation, $contents, $this->writeFlags) === false) {
throw UnableToWriteFile::atLocation($path, error_get_last()['message'] ?? '');
}

if ($visibility = $config->get(Config::OPTION_VISIBILITY)) {
Expand Down

0 comments on commit f9b5d82

Please sign in to comment.