private function createSink(StreamInterface $stream, array $options) { if (!empty($options['stream'])) { return $stream; } $sink = isset($options['sink']) ? $options['sink'] : fopen('php://temp', 'r+'); return is_string($sink) ? new Psr7\Stream(Psr7\try_fopen($sink, 'r+')) : Psr7\stream_for($sink); }
/** * Opens file stream. * * @return resource * * @throws TelegramSDKException */ public function open() { if (is_resource($this->path)) { return $this->path; } if (!$this->isRemoteFile() && !is_readable($this->path)) { throw new TelegramSDKException('Failed to create InputFile entity. Unable to read resource: ' . $this->path . '.'); } return Psr7\try_fopen($this->path, 'r'); }
/** * Turns the provided source into a stream and stores it. * * If a string is provided, it is assumed to be a filename, otherwise, it * passes the value directly to `Psr7\stream_for()`. * * @param mixed $source * * @return Stream */ private function determineSource($source) { // Use the contents of a file as the data source. if (is_string($source)) { $source = Psr7\try_fopen($source, 'r'); } // Create a source stream. $stream = Psr7\stream_for($source); if (!$stream->isReadable()) { throw new IAE('Source stream must be readable.'); } return $stream; }
/** * @expectedException \RuntimeException * @expectedExceptionMessage Unable to open /path/to/does/not/exist using mode r */ public function testThrowsExceptionNotWarning() { Psr7\try_fopen('/path/to/does/not/exist', 'r'); }
protected function createPart($seekable, $number) { $data = []; $firstByte = $this->source->tell(); // Read from the source to create the body stream. This also // calculates the linear and tree hashes as the data is read. if ($seekable) { // Case 1: Stream is seekable, can make stream from new handle. $body = Psr7\try_fopen($this->source->getMetadata('uri'), 'r'); $body = $this->limitPartStream(Psr7\stream_for($body)); // Create another stream decorated with hashing streams and read // through it, so we can get the hash values for the part. $decoratedBody = $this->decorateWithHashes($body, $data); while (!$decoratedBody->eof()) { $decoratedBody->read(1048576); } // Seek the original source forward to the end of the range. $this->source->seek($this->source->tell() + $body->getSize()); } else { // Case 2: Stream is not seekable, must store part in temp stream. $source = $this->limitPartStream($this->source); $source = $this->decorateWithHashes($source, $data); $body = Psr7\stream_for(); Psr7\copy_to_stream($source, $body); } // Do not create a part if the body size is zero. if ($body->getSize() === 0) { return false; } $body->seek(0); $data['body'] = $body; $lastByte = $this->source->tell() - 1; $data['range'] = "bytes {$firstByte}-{$lastByte}/*"; return $data; }
/** * Create a new stream based on the input type. * * @param resource|string|StreamInterface $source path to a local file, resource or stream * * @return StreamInterface */ protected function toStream($source) { if (is_string($source)) { $source = Psr7\try_fopen($source, 'r+'); } return Psr7\stream_for($source); }