public function __construct(StreamInterface $stream) { // Skip the first 10 bytes $stream = new LimitStream($stream, -1, 10); $resource = StreamWrapper::getResource($stream); stream_filter_append($resource, 'zlib.inflate', STREAM_FILTER_READ); parent::__construct(new Stream($resource)); }
public function testCanOpenReadonlyStream() { $stream = $this->getMockBuilder('Psr\\Http\\Message\\StreamInterface')->setMethods(['isReadable', 'isWritable'])->getMockForAbstractClass(); $stream->expects($this->once())->method('isReadable')->will($this->returnValue(false)); $stream->expects($this->once())->method('isWritable')->will($this->returnValue(true)); $r = StreamWrapper::getResource($stream); $this->assertInternalType('resource', $r); fclose($r); }
/** * @param Response $response */ function out(Response $response) { header(sprintf('%s %s %s', $response->getProtocolVersion(), $response->getStatusCode(), $response->getReasonPhrase())); foreach ($response->getHeaders() as $name => $values) { foreach ($values as $value) { header(sprintf('%s: %s', $name, $value), false); } } stream_copy_to_stream(\GuzzleHttp\Psr7\StreamWrapper::getResource($response->getBody()), fopen('php://output', 'w')); }
public function __construct(StreamInterface $stream) { // read the first 10 bytes, ie. gzip header $header = $stream->read(10); $filenameHeaderLength = $this->getLengthOfPossibleFilenameHeader($stream, $header); // Skip the header, that is 10 + length of filename + 1 (nil) bytes $stream = new LimitStream($stream, -1, 10 + $filenameHeaderLength); $resource = StreamWrapper::getResource($stream); stream_filter_append($resource, 'zlib.inflate', STREAM_FILTER_READ); $this->stream = new Stream($resource); }
public function testArrayHeaderAccess() { $stream = StreamWrapper::getResource(\GuzzleHttp\Psr7\stream_for('')); $httpEmulator = new HttpEmulator('http://example.com', $stream); $response = 'HTTP/1.1 200 OK' . "\r\n"; $response .= 'Content-Type: application/json' . "\r\n"; $response .= "\r\n"; $response .= 'test123'; $httpEmulator->setResponseStream(\GuzzleHttp\Psr7\stream_for($response)); $this->assertTrue(isset($httpEmulator['headers'])); $this->assertInternalType('array', $httpEmulator['headers']); $this->assertCount(2, $httpEmulator['headers']); $this->assertEquals('HTTP/1.1 200 OK', $httpEmulator['headers'][0]); $this->assertEquals('Content-Type: application/json', $httpEmulator['headers'][1]); unset($httpEmulator['headers']); $this->assertTrue(isset($httpEmulator['headers'])); $httpEmulator['headers'] = []; $this->assertCount(2, $httpEmulator['headers']); }
/** * @param StreamInterface $stream * @param array $options options: * <br> - 'close' — bool, close stream if no more active listeners * @throws \Exception */ public function parse($stream, $options = []) { $close = isset($options['close']) ? $options['close'] : true; $wrapped = self::wrapBodyStream($stream, $close); if ($close) { $this->setCompleteCallback(function () use($wrapped) { $wrapped->close(); }); } try { $resource = StreamWrapper::getResource($wrapped); $parser = new \JsonStreamingParser_Parser($resource, $this); $parser->parse(); } catch (\Exception $e) { if ($close) { $wrapped->close(); } throw $e; } }
/** * Retrieve an object from Manta as a temporary file. * This function uses streams to copy a remote file to the local * filesystem as a temporary file. * * Note: this file is not deleted for you. * * @see http://apidocs.joyent.com/manta/api.html#GetObject * @since 2.0.0 * @api * * @param string $object Path of object * * @return string|MantaStringResponse Path to file where data is stored */ public function getObjectAsFile($object) { $stream = $this->getObjectAsStream($object); $headers = $stream->getHeaders(); $input = StreamWrapper::getResource($stream); $tempDir = sys_get_temp_dir(); $filePath = tempnam($tempDir, 'manta-php-'); $file = fopen($filePath, 'w'); try { stream_copy_to_stream($input, $file); } finally { if (is_resource($file)) { fclose($file); } if (is_resource($input)) { fclose($input); } $stream->close(); } return new MantaStringResponse($filePath, $headers); }
public function testGetContext() { $context = StreamWrapper::getResource(\GuzzleHttp\Psr7\stream_for('')); $emulator = new TestEmulator('http://example.com', $context); $this->assertSame($context, $emulator->getContext()); }
/** * Put a content into $this file, if exists or not * $mixed content has to be string, resource, or PSR Stream * In case of Stream, $mixed has to be seekable and readable * * @param string|Resource|StreamInterface $mixed * @param null $mimeType * @return bool * @throws \common_Exception */ public function put($mixed, $mimeType = null) { \common_Logger::i('Writting in ' . $this->getPrefix()); $config = is_null($mimeType) ? [] : ['ContentType' => $mimeType]; if (is_string($mixed)) { return $this->getFileSystem()->put($this->getPrefix(), $mixed, $config); } if (is_resource($mixed)) { return $this->getFileSystem()->putStream($this->getPrefix(), $mixed, $config); } if ($mixed instanceof StreamInterface) { if (!$mixed->isReadable()) { throw new \common_Exception('Stream is not readable. Write to filesystem aborted.'); } if (!$mixed->isSeekable()) { throw new \common_Exception('Stream is not seekable. Write to filesystem aborted.'); } $mixed->rewind(); $resource = StreamWrapper::getResource($mixed); if (!is_resource($resource)) { throw new \common_Exception('Unable to create resource from the given stream. Write to filesystem aborted.'); } return $this->getFileSystem()->putStream($this->getPrefix(), $resource, $config); } throw new \InvalidArgumentException('Value to be written has to be: string, resource or StreamInterface'); }
/** * Read a file as a stream. * * @param string $path * * @return array|false */ public function readStream($path) { $response = $this->httpClient->get($this->applyPathPrefix($path), ['headers' => ['X-Akamai-ACS-Action' => $this->getAcsActionHeaderValue('download')]]); $stream = \GuzzleHttp\Psr7\StreamWrapper::getResource($response->getBody()); fseek($stream, 0); return ['stream' => $stream]; }
public function testSetEmulator() { $stream = GuzzleStreamWrapper::getResource(\GuzzleHttp\Psr7\stream_for('test123')); stream_context_set_option($stream, 'http', 'content', 'test123'); $streamWrapper = new StreamWrapper(); $streamWrapper->context = $stream; $emulator = new HttpEmulator('http://example.com', $streamWrapper->getContext()); $this->assertSame($streamWrapper, $streamWrapper->setEmulator($emulator)); $this->assertSame($emulator, $streamWrapper->getEmulator()); }
/** * {@inheritdoc} */ public function parse(SourceInterface $source, StreamInterface $stream) { $state = $source->getState($this); $handle = StreamWrapper::getResource($stream); // Resume where we left off. fseek($handle, $state->pointer); // Initial load. if ($state->isFirstRun()) { $state->fileLength = $stream->getSize(); if ($this->hasHeader) { $state->header = $this->readLine($handle); } } // Reset our counter. $this->linesRead = 0; $table = $this->getTableFactory()->create(); while ($data = $this->getCsvLine($handle)) { if ($this->hasHeader) { $data = array_combine($state->header, $data); } $table->getNewRow()->setData($data); } $state->pointer = ftell($handle); return $table; }
protected function normalizeResource($resource, $method) { if ($resource instanceof StreamInterface) { $resource = GuzzleStreamWrapper::getResource($resource); } elseif (!is_resource($resource)) { throw new Ex\InvalidArgumentException($method . ' expects $resource to be a resource or instance of Psr\\Http\\Message\\StreamInterface.'); } return $resource; }