Skip to content

Commit 45a1cd7

Browse files
committed
Filesystem 2.2.0
1 parent 6cceff8 commit 45a1cd7

File tree

3 files changed

+72
-4
lines changed

3 files changed

+72
-4
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1+
<a name="2.2.0"></a>
2+
# [2.2.0](https://github.com/atomastic/filesystem) (2021-09-28)
3+
* add `replace` method for File.
4+
* add `sharedGet` method for File.
5+
* add ability to `get` method for File.
6+
17
<a name="2.1.0"></a>
2-
# [2.0.0](https://github.com/atomastic/filesystem) (2021-08-06)
8+
# [2.1.0](https://github.com/atomastic/filesystem) (2021-08-06)
39
* add `ensureExists` method for Directory.
410

511
<a name="2.0.0"></a>

src/File.php

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,48 @@ public function put(string $data, bool $lock = false)
6969

7070
/**
7171
* Get the contents of a file.
72+
*
73+
* @param bool $lock Acquire an exclusive lock on the file while proceeding to the reading.
7274
*
7375
* @return string|false The file contents or false on failure.
7476
*/
75-
public function get()
77+
public function get($lock = false)
7678
{
77-
$contents = file_get_contents($this->path);
79+
if ($this->isFile($this->path)) {
80+
$contents = $lock ? $this->sharedGet() : file_get_contents($this->path);
81+
}
82+
83+
if ($contents === false) {
84+
return false;
85+
}
86+
87+
return $contents;
88+
}
89+
90+
/**
91+
* Get contents of a file with shared access.
92+
*
93+
* @return string|false The file contents or false on failure.
94+
*/
95+
public function sharedGet()
96+
{
97+
$contents = false;
98+
99+
$handle = fopen($this->path, 'rb');
100+
101+
if ($handle) {
102+
try {
103+
if (flock($handle, LOCK_SH)) {
104+
clearstatcache(true, $this->path);
105+
106+
$contents = fread($handle, $this->size($this->path) ?: 1);
107+
108+
flock($handle, LOCK_UN);
109+
}
110+
} finally {
111+
fclose($handle);
112+
}
113+
}
78114

79115
if ($contents === false) {
80116
return false;
@@ -111,6 +147,19 @@ public function append(string $data)
111147
return file_put_contents($this->path, $data, FILE_APPEND);
112148
}
113149

150+
/**
151+
* Replace the value with the string in a given file.
152+
*
153+
* @param string $search Search
154+
* @param mixed $replace Replace
155+
*
156+
* @return void
157+
*/
158+
public function replace(string $search, $replace): void
159+
{
160+
file_put_contents($this->path, str_replace($search, $replace, file_get_contents($this->path)));
161+
}
162+
114163
/**
115164
* Delete the file at a given path.
116165
*

tests/FilesystemTest.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@
3636
$this->assertTrue($filesystem->directory($this->tempDir . '/1')->delete());
3737
});
3838

39+
test('test replace() method', function (): void {
40+
file_put_contents($this->tempDir . '/replace.txt', 'foo');
41+
$filesystem = new Filesystem();
42+
$filesystem->file($this->tempDir . '/replace.txt')->replace('foo', 'boo');
43+
$this->assertEquals('boo', file_get_contents($this->tempDir . '/replace.txt'));
44+
});
45+
3946
test('test put() method', function (): void {
4047
$filesystem = new Filesystem();
4148
$this->assertEquals(4, $filesystem->file($this->tempDir . '/2.txt')->put('test'));
@@ -141,11 +148,17 @@
141148
$this->assertEquals('098f6bcd4621d373cade4e832627b4f6', $filesystem->file($this->tempDir . '/1.txt')->hash());
142149
});
143150

144-
145151
test('test get() method', function (): void {
146152
$filesystem = new Filesystem();
147153
$filesystem->file($this->tempDir . '/1.txt')->put('test');
148154
$this->assertEquals('test', $filesystem->file($this->tempDir . '/1.txt')->get());
155+
$this->assertEquals('test', $filesystem->file($this->tempDir . '/1.txt')->get(true));
156+
});
157+
158+
test('test sharedGet() method', function (): void {
159+
$filesystem = new Filesystem();
160+
$filesystem->file($this->tempDir . '/shared.txt')->put('test');
161+
$this->assertEquals('test', $filesystem->file($this->tempDir . '/shared.txt')->sharedGet());
149162
});
150163

151164
test('test prepend() method', function (): void {

0 commit comments

Comments
 (0)