Skip to content

Commit f4c6c24

Browse files
feat(Storage): implement touch on streams (#7144)
1 parent 05a3670 commit f4c6c24

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

Storage/src/StreamWrapper.php

+34-1
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,39 @@ public function stream_set_option()
127127
return false;
128128
}
129129

130+
/**
131+
* This is called when touch is used on a stream. See:
132+
* https://www.php.net/manual/en/streamwrapper.stream-metadata.php
133+
*/
134+
public function stream_metadata($path, $option, $value)
135+
{
136+
if ($option == STREAM_META_TOUCH) {
137+
$this->openPath($path);
138+
return $this->touch();
139+
}
140+
141+
return false;
142+
}
143+
144+
/**
145+
* Creates an empty file if it does not exist.
146+
* @return bool Returns true if file exists or has been created, false otherwise.
147+
*/
148+
private function touch()
149+
{
150+
$object = $this->bucket->object($this->file);
151+
try {
152+
if (!$object->exists()) {
153+
$this->bucket->upload('', [
154+
'name' => $this->file
155+
]);
156+
}
157+
return true;
158+
} catch (NotFoundException $e) {
159+
}
160+
return false;
161+
}
162+
130163
/**
131164
* Register a StreamWrapper for reading and writing to Google Storage
132165
*
@@ -186,7 +219,7 @@ public static function getClient($protocol = null)
186219
*/
187220
public function stream_open($path, $mode, $flags, &$openedPath)
188221
{
189-
$client = $this->openPath($path);
222+
$this->openPath($path);
190223

191224
// strip off 'b' or 't' from the mode
192225
$mode = rtrim($mode, 'bt');

Storage/tests/System/StreamWrapper/WriteTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ public function testFwrite()
5959
$this->assertFileExists($this->fileUrl);
6060
}
6161

62+
public function testTouch()
63+
{
64+
$this->assertFileDoesNotExist($this->fileUrl);
65+
66+
$this->assertTrue(touch($this->fileUrl));
67+
68+
$this->assertFileExists($this->fileUrl);
69+
}
70+
6271
public function testStreamingWrite()
6372
{
6473
$this->assertFileDoesNotExist($this->fileUrl);

0 commit comments

Comments
 (0)