/** * Create color profile object from filename. * * @param string $filename * * @throws RuntimeException * * @return $this */ public static function fromFilename($filename) { if (!file_exists($filename) || !is_file($filename) || !is_readable($filename)) { throw new RuntimeException(sprintf('Filename %s is an invalid profile file or is not readable', $filename)); } return new static(StreamUtils::create(StreamUtils::open($filename, 'r'))); }
/** * Drains the stream into the "save_to" client option. * * @param resource $stream * @param string|resource|StreamInterface $dest * * @return Stream * @throws \RuntimeException when the save_to option is invalid. */ private function drain($stream, $dest) { if (is_resource($stream)) { if (!is_resource($dest)) { $stream = Stream::factory($stream); } else { stream_copy_to_stream($stream, $dest); fclose($stream); rewind($dest); return $dest; } } // Stream the response into the destination stream $dest = is_string($dest) ? new Stream(Utils::open($dest, 'r+')) : Stream::factory($dest); Utils::copyToStream($stream, $dest); $dest->seek(0); $stream->close(); return $dest; }
/** * @expectedException \RuntimeException * @expectedExceptionMessage Unable to open /path/to/does/not/exist using mode r */ public function testThrowsExceptionNotWarning() { Utils::open('/path/to/does/not/exist', 'r'); }
/** * Drain the stream into the destination stream */ private function getSaveToBody(RequestInterface $request, StreamInterface $stream) { if ($saveTo = $request->getConfig()['save_to']) { // Stream the response into the destination stream $saveTo = is_string($saveTo) ? new Stream(Utils::open($saveTo, 'r+')) : Stream::factory($saveTo); } else { // Stream into the default temp stream $saveTo = Stream::factory(); } Utils::copyToStream($stream, $saveTo); $saveTo->seek(0); $stream->close(); return $saveTo; }
protected function getCreatePartFn() { return function ($seekable) { $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 = Utils::open($this->source->getMetadata('uri'), 'r'); $body = $this->limitPartStream(Stream::factory($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 = Stream::factory(); Utils::copyToStream($source, $body); } $body->seek(0); $data['body'] = $body; $lastByte = $this->source->tell() - 1; $data['range'] = "bytes {$firstByte}-{$lastByte}/*"; return $data; }; }