Skip to content

Commit

Permalink
Simplify operations by caching before uploading
Browse files Browse the repository at this point in the history
  • Loading branch information
Slamdunk committed Dec 9, 2021
1 parent b3729ad commit 29ebd56
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 126 deletions.
10 changes: 2 additions & 8 deletions infection.json.dist
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,11 @@
"mutators": {
"@default": true,
"global-ignoreSourceCodeByRegex": [
"\\\\assert\\(.+\\);",
"\\$consumed ..?= .+;"
"\\\\assert\\(.+\\);"
],
"ConcatOperandRemoval": {
"ignoreSourceCodeByRegex": [
"\\$this->tmpFilename = \\$this->filename . '.tmp';"
]
},
"TrueValue": {
"ignoreSourceCodeByRegex": [
"self::\\$filterRegistered = true;"
"\\$tmpFile = uniqid\\(\\$destination . '_'\\);"
]
}
},
Expand Down
29 changes: 14 additions & 15 deletions src/LocalCacheProxyAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ public function __construct(
) {
$this->localCacheAdapter = new LocalFilesystemAdapter($location);
$this->pathPrefixer = new PathPrefixer($location, \DIRECTORY_SEPARATOR);

LocalCacheStreamFilter::register();
}

/**
Expand Down Expand Up @@ -57,12 +55,15 @@ public function writeStream(string $path, $contents, Config $config): void
{
$this->localCacheAdapter->createDirectory(\dirname($path), new Config());

LocalCacheStreamFilter::appendWrite(
$this->pathPrefixer->prefixPath($path),
$contents
);
$destination = $this->pathPrefixer->prefixPath($path);
$tmpFile = uniqid($destination.'_');
file_put_contents($tmpFile, $contents);
$tmpStream = fopen($tmpFile, 'r');

$this->remoteAdapter->writeStream($path, $tmpStream, $config);

$this->remoteAdapter->writeStream($path, $contents, $config);
fclose($tmpStream);
rename($tmpFile, $destination);
}

/**
Expand All @@ -76,7 +77,7 @@ public function read(string $path): string
return $this->localCacheAdapter->read($path);
}

return $this->remoteAdapter->read($path);
return stream_get_contents($this->readStream($path));
}

/**
Expand All @@ -90,14 +91,12 @@ public function readStream(string $path)
return $this->localCacheAdapter->readStream($path);
}

$stream = $this->remoteAdapter->readStream($path);

LocalCacheStreamFilter::appendWrite(
$this->pathPrefixer->prefixPath($path),
$stream
);
$destination = $this->pathPrefixer->prefixPath($path);
$tmpFile = uniqid($destination.'_');
file_put_contents($tmpFile, $this->remoteAdapter->readStream($path));
rename($tmpFile, $destination);

return $stream;
return $this->localCacheAdapter->readStream($path);
}

/**
Expand Down
88 changes: 0 additions & 88 deletions src/LocalCacheStreamFilter.php

This file was deleted.

28 changes: 13 additions & 15 deletions test/LocalCacheAdapterProxyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

/**
* @covers \SlamFlysystem\LocalCache\LocalCacheProxyAdapter
* @covers \SlamFlysystem\LocalCache\LocalCacheStreamFilter
*
* @internal
*/
Expand Down Expand Up @@ -128,15 +127,6 @@ public function stream_that_goes_wrong_doesnt_create_a_false_positive_local_file
} catch (RuntimeException $runtimeException) {
}

// Our StreamThatGoesWrongFilter is triggeres only after the
// fopen call by the remote LocalFilesystemAdapter, so the
// remote file exists, but this doesn't matter to this test
$fileExists = $adapter->fileExists('path.txt');
static::assertTrue($fileExists);

$contents = $adapter->read('path.txt');
static::assertSame('', $contents);

$fileExists = $this->localCacheAdapter->fileExists('path.txt');
static::assertFalse($fileExists);
}
Expand All @@ -162,19 +152,27 @@ public function file_exists_reply_with_local_cache_first(): void
/**
* @test
*/
public function read_reads_local_cache_first(): void
public function read_reads_saves_remote_read_and_cache_response(): void
{
$adapter = $this->adapter();

$adapter->write('path.txt', 'contents', new Config());
static::assertFalse($adapter->fileExists('path.txt'));

$fileExists = $adapter->fileExists('path.txt');
static::assertTrue($fileExists);
$this->remoteAdapter->write('path.txt', 'foobar', new Config());

static::assertFalse($this->localCacheAdapter->fileExists('path.txt'));

$contents = $adapter->read('path.txt');
static::assertSame('foobar', $contents);

static::assertTrue($this->localCacheAdapter->fileExists('path.txt'));

$this->remoteAdapter->delete('path.txt');

static::assertTrue($adapter->fileExists('path.txt'));

$contents = $adapter->read('path.txt');
static::assertSame('contents', $contents);
static::assertSame('foobar', $contents);
}

/**
Expand Down

0 comments on commit 29ebd56

Please sign in to comment.