Skip to content

Commit

Permalink
feat(Storage): implement touch on streams (googleapis#7144)
Browse files Browse the repository at this point in the history
  • Loading branch information
vishwarajanand authored Mar 14, 2024
1 parent 05a3670 commit f4c6c24
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
35 changes: 34 additions & 1 deletion Storage/src/StreamWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,39 @@ public function stream_set_option()
return false;
}

/**
* This is called when touch is used on a stream. See:
* https://www.php.net/manual/en/streamwrapper.stream-metadata.php
*/
public function stream_metadata($path, $option, $value)
{
if ($option == STREAM_META_TOUCH) {
$this->openPath($path);
return $this->touch();
}

return false;
}

/**
* Creates an empty file if it does not exist.
* @return bool Returns true if file exists or has been created, false otherwise.
*/
private function touch()
{
$object = $this->bucket->object($this->file);
try {
if (!$object->exists()) {
$this->bucket->upload('', [
'name' => $this->file
]);
}
return true;
} catch (NotFoundException $e) {
}
return false;
}

/**
* Register a StreamWrapper for reading and writing to Google Storage
*
Expand Down Expand Up @@ -186,7 +219,7 @@ public static function getClient($protocol = null)
*/
public function stream_open($path, $mode, $flags, &$openedPath)
{
$client = $this->openPath($path);
$this->openPath($path);

// strip off 'b' or 't' from the mode
$mode = rtrim($mode, 'bt');
Expand Down
9 changes: 9 additions & 0 deletions Storage/tests/System/StreamWrapper/WriteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ public function testFwrite()
$this->assertFileExists($this->fileUrl);
}

public function testTouch()
{
$this->assertFileDoesNotExist($this->fileUrl);

$this->assertTrue(touch($this->fileUrl));

$this->assertFileExists($this->fileUrl);
}

public function testStreamingWrite()
{
$this->assertFileDoesNotExist($this->fileUrl);
Expand Down

0 comments on commit f4c6c24

Please sign in to comment.