From 45a1cd78d23235ca113215fde28f6f6f590ba380 Mon Sep 17 00:00:00 2001 From: Awilum Date: Tue, 28 Sep 2021 19:43:31 +0300 Subject: [PATCH] Filesystem 2.2.0 --- CHANGELOG.md | 8 +++++- src/File.php | 53 ++++++++++++++++++++++++++++++++++++++-- tests/FilesystemTest.php | 15 +++++++++++- 3 files changed, 72 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c29217..2ea73ed 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ + +# [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. + -# [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. diff --git a/src/File.php b/src/File.php index dcfd9a2..14b3af9 100644 --- a/src/File.php +++ b/src/File.php @@ -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; @@ -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. * diff --git a/tests/FilesystemTest.php b/tests/FilesystemTest.php index 18148d2..fa7f8c7 100644 --- a/tests/FilesystemTest.php +++ b/tests/FilesystemTest.php @@ -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')); @@ -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 {