diff --git a/src/Client.php b/src/Client.php index a74c13e..e174f6b 100644 --- a/src/Client.php +++ b/src/Client.php @@ -5,6 +5,7 @@ use GuzzleHttp\Client as HttpClient; use GuzzleHttp\Psr7\Response; use InvalidArgumentException; +use Psr\Http\Message\StreamInterface; /** * Class GitlabAdapter @@ -80,6 +81,21 @@ public function read($path) return $this->responseContents($response); } + + /** + * @param $path + * + * @return resource|null + * @throws \GuzzleHttp\Exception\GuzzleException + */ + public function readStream($path) + { + $path = urlencode($path); + + $response = $this->request('GET', "files/$path/raw"); + + return $response->getBody()->detach(); + } /** * @param $path diff --git a/src/GitlabAdapter.php b/src/GitlabAdapter.php index 9633206..683e1ab 100644 --- a/src/GitlabAdapter.php +++ b/src/GitlabAdapter.php @@ -147,17 +147,15 @@ public function read(string $path): string */ public function readStream(string $path) { - if (false === ($fp = fopen('php://memory', 'r+'))) { - throw UnableToReadFile::fromLocation($path, 'Unable to open memory stream'); - } + try { + if (null === ($resource = $this->client->readStream($this->prefixer->prefixPath($path)))) { + throw UnableToReadFile::fromLocation($path, 'Empty content'); + } - if (false === fwrite($fp, $this->read($path))) { - throw UnableToReadFile::fromLocation($path, 'Unable to write stream'); + return $resource; + } catch (Throwable $e) { + throw UnableToReadFile::fromLocation($path, $e->getMessage(), $e); } - - fseek($fp, 0); - - return $fp; } /** diff --git a/tests/GitlabAdapterTest.php b/tests/GitlabAdapterTest.php index 1ebc237..a0805c9 100644 --- a/tests/GitlabAdapterTest.php +++ b/tests/GitlabAdapterTest.php @@ -78,7 +78,7 @@ public function it_can_read_a_file_into_a_stream() $stream = $this->gitlabAdapter->readStream('README.md'); $this->assertIsResource($stream); - $this->assertEquals(stream_get_contents($stream, null, 0), $this->gitlabAdapter->read('README.md')); + $this->assertEquals(stream_get_contents($stream, -1, 0), $this->gitlabAdapter->read('README.md')); } /**