Skip to content

Commit

Permalink
Filesystem 2.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Awilum committed Sep 28, 2021
1 parent 6cceff8 commit 45a1cd7
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 4 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
<a name="2.2.0"></a>
# [2.2.0](https://github.com/atomastic/filesystem) (2021-09-28)
* add `replace` method for File.
* add `sharedGet` method for File.
* add ability to `get` method for File.

<a name="2.1.0"></a>
# [2.0.0](https://github.com/atomastic/filesystem) (2021-08-06)
# [2.1.0](https://github.com/atomastic/filesystem) (2021-08-06)
* add `ensureExists` method for Directory.

<a name="2.0.0"></a>
Expand Down
53 changes: 51 additions & 2 deletions src/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,48 @@ public function put(string $data, bool $lock = false)

/**
* Get the contents of a file.
*
* @param bool $lock Acquire an exclusive lock on the file while proceeding to the reading.
*
* @return string|false The file contents or false on failure.
*/
public function get()
public function get($lock = false)
{
$contents = file_get_contents($this->path);
if ($this->isFile($this->path)) {
$contents = $lock ? $this->sharedGet() : file_get_contents($this->path);
}

if ($contents === false) {
return false;
}

return $contents;
}

/**
* Get contents of a file with shared access.
*
* @return string|false The file contents or false on failure.
*/
public function sharedGet()
{
$contents = false;

$handle = fopen($this->path, 'rb');

if ($handle) {
try {
if (flock($handle, LOCK_SH)) {
clearstatcache(true, $this->path);

$contents = fread($handle, $this->size($this->path) ?: 1);

flock($handle, LOCK_UN);
}
} finally {
fclose($handle);
}
}

if ($contents === false) {
return false;
Expand Down Expand Up @@ -111,6 +147,19 @@ public function append(string $data)
return file_put_contents($this->path, $data, FILE_APPEND);
}

/**
* Replace the value with the string in a given file.
*
* @param string $search Search
* @param mixed $replace Replace
*
* @return void
*/
public function replace(string $search, $replace): void
{
file_put_contents($this->path, str_replace($search, $replace, file_get_contents($this->path)));
}

/**
* Delete the file at a given path.
*
Expand Down
15 changes: 14 additions & 1 deletion tests/FilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@
$this->assertTrue($filesystem->directory($this->tempDir . '/1')->delete());
});

test('test replace() method', function (): void {
file_put_contents($this->tempDir . '/replace.txt', 'foo');
$filesystem = new Filesystem();
$filesystem->file($this->tempDir . '/replace.txt')->replace('foo', 'boo');
$this->assertEquals('boo', file_get_contents($this->tempDir . '/replace.txt'));
});

test('test put() method', function (): void {
$filesystem = new Filesystem();
$this->assertEquals(4, $filesystem->file($this->tempDir . '/2.txt')->put('test'));
Expand Down Expand Up @@ -141,11 +148,17 @@
$this->assertEquals('098f6bcd4621d373cade4e832627b4f6', $filesystem->file($this->tempDir . '/1.txt')->hash());
});


test('test get() method', function (): void {
$filesystem = new Filesystem();
$filesystem->file($this->tempDir . '/1.txt')->put('test');
$this->assertEquals('test', $filesystem->file($this->tempDir . '/1.txt')->get());
$this->assertEquals('test', $filesystem->file($this->tempDir . '/1.txt')->get(true));
});

test('test sharedGet() method', function (): void {
$filesystem = new Filesystem();
$filesystem->file($this->tempDir . '/shared.txt')->put('test');
$this->assertEquals('test', $filesystem->file($this->tempDir . '/shared.txt')->sharedGet());
});

test('test prepend() method', function (): void {
Expand Down

0 comments on commit 45a1cd7

Please sign in to comment.