Skip to content

Commit

Permalink
Merge pull request #11 from ElGigi/stream
Browse files Browse the repository at this point in the history
Allow to read into stream (create a memory stream)
  • Loading branch information
RoyVoetman authored Dec 1, 2020
2 parents 4f0525e + 3b220f1 commit b300610
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 29 deletions.
12 changes: 11 additions & 1 deletion src/GitlabAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,17 @@ public function read(string $path): string
*/
public function readStream(string $path)
{
throw new UnableToCreateStream(get_class($this).' Gitlab API does not support reading a file into a stream.');
if (false === ($fp = fopen('php://memory', 'r+'))) {
throw UnableToReadFile::fromLocation($path, 'Unable to open memory stream');
}

if (false === fwrite($fp, $this->read($path))) {
throw UnableToReadFile::fromLocation($path, 'Unable to write stream');
}

fseek($fp, 0);

return $fp;
}

/**
Expand Down
24 changes: 0 additions & 24 deletions src/UnableToCreateStream.php

This file was deleted.

8 changes: 4 additions & 4 deletions tests/GitlabAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use League\Flysystem\UnableToWriteFile;
use RoyVoetman\FlysystemGitlab\Client;
use RoyVoetman\FlysystemGitlab\GitlabAdapter;
use RoyVoetman\FlysystemGitlab\UnableToCreateStream;

class GitlabAdapterTest extends TestCase
{
Expand Down Expand Up @@ -74,11 +73,12 @@ public function it_can_read_a_file()
/**
* @test
*/
public function it_throws_when_reading_a_file_into_a_stream()
public function it_can_read_a_file_into_a_stream()
{
$this->expectException(UnableToCreateStream::class);
$stream = $this->gitlabAdapter->readStream('README.md');

$this->gitlabAdapter->readStream('README.md');
$this->assertIsResource($stream);
$this->assertEquals(stream_get_contents($stream, null, 0), $this->gitlabAdapter->read('README.md'));
}

/**
Expand Down

0 comments on commit b300610

Please sign in to comment.