Skip to content

Commit

Permalink
Merge pull request aws#659 from aws/custom-protocol
Browse files Browse the repository at this point in the history
Allow custom stream wrapper protocol
  • Loading branch information
mtdowling committed Jun 26, 2015
2 parents a00fe9b + 31c22f4 commit a8c22bb
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
31 changes: 29 additions & 2 deletions src/S3/StreamWrapper.php
Expand Up @@ -91,6 +91,9 @@ class StreamWrapper
/** @var CacheInterface Cache for object and dir lookups */
private $cache;

/** @var string The opened protocol (e.g., "s3") */
private $protocol = 's3';

/**
* Register the 's3://' stream wrapper
*
Expand Down Expand Up @@ -129,6 +132,7 @@ public function stream_close()

public function stream_open($path, $mode, $options, &$opened_path)
{
$this->initProtocol($path);
$this->params = $this->getBucketKey($path);
$this->mode = rtrim($mode, 'bt');

Expand Down Expand Up @@ -202,6 +206,8 @@ public function stream_write($data)

public function unlink($path)
{
$this->initProtocol($path);

return $this->boolCall(function () use ($path) {
$this->clearCacheKey($path);
$this->getClient()->deleteObject($this->withPath($path));
Expand All @@ -225,6 +231,8 @@ public function stream_stat()
*/
public function url_stat($path, $flags)
{
$this->initProtocol($path);

// Some paths come through as S3:// for some reason.
$split = explode('://', $path);
$path = strtolower($split[0]) . '://' . $split[1];
Expand All @@ -243,8 +251,20 @@ public function url_stat($path, $flags)
return $stat;
}

/**
* Parse the protocol out of the given path.
*
* @param $path
*/
private function initProtocol($path)
{
$parts = explode('://', $path, 2);
$this->protocol = $parts[0] ?: 's3';
}

private function createStat($path, $flags)
{
$this->initProtocol($path);
$parts = $this->withPath($path);

if (!$parts['Key']) {
Expand Down Expand Up @@ -308,6 +328,7 @@ private function statDirectory($parts, $path, $flags)
*/
public function mkdir($path, $mode, $options)
{
$this->initProtocol($path);
$params = $this->withPath($path);
$this->clearCacheKey($path);
if (!$params['Bucket']) {
Expand All @@ -325,6 +346,7 @@ public function mkdir($path, $mode, $options)

public function rmdir($path, $options)
{
$this->initProtocol($path);
$this->clearCacheKey($path);
$params = $this->withPath($path);
$client = $this->getClient();
Expand Down Expand Up @@ -359,6 +381,7 @@ public function rmdir($path, $options)
*/
public function dir_opendir($path, $options)
{
$this->initProtocol($path);
$this->openedPath = $path;
$params = $this->withPath($path);
$delimiter = $this->getOption('delimiter');
Expand Down Expand Up @@ -574,11 +597,15 @@ private function getOptions($removeContextData = false)
$options = [];
} else {
$options = stream_context_get_options($this->context);
$options = isset($options['s3']) ? $options['s3'] : [];
$options = isset($options[$this->protocol])
? $options[$this->protocol]
: [];
}

$default = stream_context_get_options(stream_context_get_default());
$default = isset($default['s3']) ? $default['s3'] : [];
$default = isset($default[$this->protocol])
? $default[$this->protocol]
: [];
$result = $this->params + $options + $default;

if ($removeContextData) {
Expand Down
17 changes: 17 additions & 0 deletions tests/S3/StreamWrapperTest.php
Expand Up @@ -833,4 +833,21 @@ public function testReturnsStreamSizeFromHeaders()
$resource = fopen('s3://foo/bar', 'r');
$this->assertEquals(5, fstat($resource)['size']);
}

public function testCanUseCustomProtocol()
{
StreamWrapper::register($this->client, 'foo');
$stream = fopen('php://temp', 'r+');
fwrite($stream, 'bar');
fseek($stream, 0);

$this->addMockResults($this->client, [
new Result([
'Body' => new Psr7\NoSeekStream(new Psr7\Stream($stream))
])
]);

$s = fopen('foo://bucket/key', 'r');
$this->assertEquals('bar', fread($s, 4));
}
}

0 comments on commit a8c22bb

Please sign in to comment.