public function __construct(StreamInterface $stream) { // Trick to get underlying stream $socket = $stream->detach(); $this->socket = $socket; $this->stream = new Stream($socket); }
/** * Get data. * * @throws RuntimeException * * @return string */ public function getData() { $offset = $this->stream->tell(); if (!is_int($offset)) { throw new RuntimeException('Unable to get profile data'); } $this->stream->seek(0); $data = $this->stream->getContents(); $this->stream->seek($offset); return $data; }
/** * Add a stream to the AppendStream * * @param StreamInterface $stream Stream to append. Must be readable. * * @throws \InvalidArgumentException if the stream is not readable */ public function addStream(StreamInterface $stream) { if (!$stream->isReadable()) { throw new \InvalidArgumentException('Each stream must be readable'); } // The stream is only seekable if all streams are seekable if (!$stream->isSeekable()) { $this->seekable = false; } $this->streams[] = $stream; }
private function matchesHash(CompleteEvent $event, $hash, StreamInterface $body) { $body->seek(0); while (!$body->eof()) { $this->hash->update($body->read(16384)); } $result = $this->hash->complete(); if ($hash !== $result) { throw new MessageIntegrityException(sprintf('Message integrity check failure. Expected "%s" but' . ' got "%s"', $hash, $result), $event->getRequest(), $event->getResponse()); } }
private function addExpectHeader(RequestInterface $request, StreamInterface $body) { // Determine if the Expect header should be used if ($request->hasHeader('Expect')) { return; } $expect = $request->getConfig()['expect']; // Return if disabled or if you're not using HTTP/1.1 if ($expect === false || $request->getProtocolVersion() !== '1.1') { return; } // The expect header is unconditionally enabled if ($expect === true) { $request->setHeader('Expect', '100-Continue'); return; } // By default, send the expect header when the payload is > 1mb if ($expect === null) { $expect = 1048576; } // Always add if the body cannot be rewound, the size cannot be // determined, or the size is greater than the cutoff threshold $size = $body->getSize(); if ($size === null || $size >= (int) $expect || !$body->isSeekable()) { $request->setHeader('Expect', '100-Continue'); } }
function it_listens_to_events(StreamInterface $inputStream, StreamInterface $outputStream) { $handler = new Handler(); $header = ''; $inputStream->eof()->willReturn(false); $inputStream->read(1)->will(function ($args) use(&$header) { if (empty($header)) { $header = str_split("ver:3.0 server:supervisor serial:21 pool:listener poolserial:10 eventname:PROCESS_COMMUNICATION_STDOUT len:86\n"); return "\n"; } return array_shift($header); }); $inputStream->read(86)->will(function ($args) use(&$header) { $header = str_split("ver:3.0 server:supervisor serial:21 pool:listener poolserial:10 eventname:PROCESS_COMMUNICATION_STDOUT len:86\n"); return "processname:foo groupname:bar pid:123\nThis is the data that was sent between the tags"; }); $outputStream->write("READY\n")->shouldBeCalledTimes(4); $outputStream->write("RESULT 2\nOK")->shouldBeCalled(); $outputStream->write("RESULT 4\nFAIL")->shouldBeCalled(); $this->listen($handler); }
/** * Create the aggregate stream that will be used to upload the POST data */ private function createStream(array $fields, array $files) { $this->stream = new Stream\AppendStream(); foreach ($fields as $name => $field) { $this->stream->addStream(Stream\create($this->getFieldString($name, $field))); } foreach ($files as $file) { if (!$file instanceof PostFileInterface) { throw new \InvalidArgumentException('All POST fields must ' . 'implement PostFieldInterface'); } $this->stream->addStream(Stream\create($this->getFileHeaders($file))); $this->stream->addStream($file->getContent()); $this->stream->addStream(Stream\create("\r\n")); } // Add the trailing boundary $this->stream->addStream(Stream\create("--{$this->boundary}--")); }
private function write($text) { $this->output->write(date('c') . ': ' . $text . PHP_EOL); }
/** * Determines if the body should be uploaded using PutObject or the * Multipart Upload System. It also modifies the passed-in $body as needed * to support the upload. * * @param StreamInterface $body Stream representing the body. * @param integer $threshold Minimum bytes before using Multipart. * * @return bool */ private function requiresMultipart(StreamInterface &$body, $threshold) { // If body size known, compare to threshold to determine if Multipart. if ($body->getSize() !== null) { return $body->getSize() < $threshold ? false : true; } // Handle the situation where the body size is unknown. // Read up to 5MB into a buffer to determine how to upload the body. $buffer = Stream::factory(); Utils::copyToStream($body, $buffer, 5242880); // If body < 5MB, use PutObject with the buffer. if ($buffer->getSize() < 5242880) { $buffer->seek(0); $body = $buffer; return false; } // If >= 5 MB, and seekable, use Multipart with rewound body. if ($body->isSeekable()) { $body->seek(0); return true; } // If >= 5 MB, and non-seekable, use Multipart, but stitch the // buffer and the body together into one stream. This avoids // needing to seek and unnecessary disc usage, while requiring // only the 5 MB buffer to be re-read by the Multipart system. $buffer->seek(0); $body = new AppendStream([$buffer, $body]); return true; }
public function tell() { return $this->body ? $this->body->tell() : 0; }
/** * Creates a document object of the specified type (given by the cmis:objectTypeId property) * in the (optionally) specified location. * * @param string $repositoryId the identifier for the repository * @param PropertiesInterface $properties the property values that must be applied to the newly * created document object * @param string|null $folderId if specified, the identifier for the folder that must be the parent * folder for the newly created document object * @param StreamInterface|null $contentStream the content stream that must be stored for the newly * created document object * @param VersioningState|null $versioningState specifies what the versioning state of the newly created object * must be (default is <code>VersioningState::MAJOR</code>) * @param string[] $policies a list of policy IDs that must be applied to the newly created document object * @param AclInterface|null $addAces a list of ACEs that must be added to the newly created document object, * either using the ACL from folderId if specified, or being applied if no folderId is specified * @param AclInterface|null $removeAces a list of ACEs that must be removed from the newly created document object, * either using the ACL from folderId if specified, or being ignored if no folderId is specified * @param ExtensionDataInterface|null $extension * @return string|null Returns the new object id or <code>null</code> if the repository sent an empty * result (which should not happen) */ public function createDocument($repositoryId, PropertiesInterface $properties, $folderId = null, StreamInterface $contentStream = null, VersioningState $versioningState = null, array $policies = array(), AclInterface $addAces = null, AclInterface $removeAces = null, ExtensionDataInterface $extension = null) { if ($folderId === null) { $url = $this->getRepositoryUrl($repositoryId); } else { $url = $this->getObjectUrl($repositoryId, $folderId); } $url->getQuery()->modify($this->convertPropertiesToQueryArray($properties)); $url->getQuery()->modify(array(Constants::CONTROL_CMISACTION => Constants::CMISACTION_CREATE_DOCUMENT, Constants::PARAM_SUCCINCT => $this->getSuccinct() ? 'true' : 'false')); if ($versioningState !== null) { $url->getQuery()->modify(array(Constants::PARAM_VERSIONING_STATE => (string) $versioningState)); } $this->appendAddAcesToUrl($url, $addAces); $this->appendRemoveAcesToUrl($url, $removeAces); $this->appendPoliciesToUrl($url, $policies); // Guzzle gets the mime type for a file by the filename extension. Sometimes the filename does not contain // the correct filename extension for example when a file is uploaded in php it gets a temporary name without // a file extension. If the filename does not contain a file extension we use the given 'cmis:name' property // as filename. See also https://github.com/guzzle/guzzle/issues/571 if ($contentStream !== null && pathinfo($contentStream->getMetadata('uri'), PATHINFO_EXTENSION) === '') { $contentStream = new PostFile('content', $contentStream, $properties->getProperties()['cmis:name']->getFirstValue()); } $responseData = $this->post($url, array('content' => $contentStream))->json(); $newObject = $this->getJsonConverter()->convertObject($responseData); return $newObject === null ? null : $newObject->getId(); }
/** * Read a line from the stream up to the maximum allowed buffer length * * @param StreamInterface $stream Stream to read from * @param int $maxLength Maximum buffer length * * @return string|bool */ public static function readline(StreamInterface $stream, $maxLength = null) { $buffer = ''; $size = 0; while (!$stream->eof()) { if (false === ($byte = $stream->read(1))) { return $buffer; } $buffer .= $byte; // Break when a new line is found or the max length - 1 is reached if ($byte == PHP_EOL || ++$size == $maxLength - 1) { break; } } return $buffer; }
/** * Read a line from the stream up to the maximum allowed buffer length * * @param StreamInterface $stream Stream to read from * @param int $maxLength Maximum buffer length * @param string $eol Line ending * * @return string|bool */ public static function readline(StreamInterface $stream, $maxLength = null, $eol = PHP_EOL) { $buffer = ''; $size = 0; $negEolLen = -strlen($eol); while (!$stream->eof()) { if (false === ($byte = $stream->read(1))) { return $buffer; } $buffer .= $byte; // Break when a new line is found or the max length - 1 is reached if (++$size == $maxLength || substr($buffer, $negEolLen) === $eol) { break; } } return $buffer; }
/** * {@inheritdoc} */ public function rewind() { $this->stream->seek(0); $this->moveToNext(); parent::rewind(); }
public function getContents() { return $this->stream->getContents(); }
/** * {@inheritdoc} */ protected function write($value) { return $this->outputStream->write($value); }
/** * {@inheritdoc} */ protected function doGetContents() { return $this->stream->getContents(); }