本文整理汇总了PHP中Psr\Http\Message\StreamInterface::rewind方法的典型用法代码示例。如果您正苦于以下问题:PHP StreamInterface::rewind方法的具体用法?PHP StreamInterface::rewind怎么用?PHP StreamInterface::rewind使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Psr\Http\Message\StreamInterface
的用法示例。
在下文中一共展示了StreamInterface::rewind方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: stream_open
/**
* Open pre-mocked StreamInterface by it's unique uri.
*
* @param string $path
* @param int $mode
* @param int $options
* @param string &$opened_path
* @return bool
*/
public function stream_open($path, $mode, $options, &$opened_path)
{
if (!isset(self::$uris[$path])) {
return false;
}
$this->stream = self::$uris[$path];
$this->mode = $mode;
$this->stream->rewind();
return true;
}
示例2: __toString
/**
* {@inheritdoc}
*/
public function __toString() : string
{
$str = $this->getStartLine();
foreach ($this->getHeaders() as $name => $values) {
$str .= sprintf("%s: %s\r\n", $name, implode(', ', $values));
}
$str .= "\r\n";
$this->body->rewind();
$str .= $this->body->getContents();
return $str;
}
示例3: moveTo
/**
* Move the uploaded file to a new location.
*
* Use this method as an alternative to move_uploaded_file(). This method is
* guaranteed to work in both SAPI and non-SAPI environments.
* Implementations must determine which environment they are in, and use the
* appropriate method (move_uploaded_file(), rename(), or a stream
* operation) to perform the operation.
*
* $targetPath may be an absolute path, or a relative path. If it is a
* relative path, resolution should be the same as used by PHP's rename()
* function.
*
* The original file or stream MUST be removed on completion.
*
* If this method is called more than once, any subsequent calls MUST raise
* an exception.
*
* When used in an SAPI environment where $_FILES is populated, when writing
* files via moveTo(), is_uploaded_file() and move_uploaded_file() SHOULD be
* used to ensure permissions and upload status are verified correctly.
*
* If you wish to move to a stream, use getStream(), as SAPI operations
* cannot guarantee writing to stream destinations.
*
* @see http://php.net/is_uploaded_file
* @see http://php.net/move_uploaded_file
* @param string $targetPath Path to which to move the uploaded file.
* @throws \InvalidArgumentException if the $path specified is invalid.
* @throws \RuntimeException on any error during the move operation, or on the second or subsequent call to the method.
*/
public function moveTo($targetPath)
{
if (!is_string($targetPath) || empty($targetPath)) {
throw new \InvalidArgumentException('Invalid path while moving an uploaded file.', 1436717307);
}
if ($this->moved) {
throw new \RuntimeException('Cannot move uploaded file, as it was already moved.', 1436717308);
}
// Check if the target path is inside the allowed paths of TYPO3, and make it absolute.
$targetPath = GeneralUtility::getFileAbsFileName($targetPath);
if (empty($targetPath)) {
throw new \RuntimeException('Cannot move uploaded file, as it was already moved.', 1436717309);
}
if (!empty($this->file) && is_uploaded_file($this->file)) {
if (GeneralUtility::upload_copy_move($this->file, $targetPath . basename($this->file)) === false) {
throw new \RuntimeException('An error occurred while moving uploaded file', 1436717310);
}
} elseif ($this->stream) {
$handle = fopen($targetPath, 'wb+');
if ($handle === false) {
throw new \RuntimeException('Unable to write to target path.', 1436717311);
}
$this->stream->rewind();
while (!$this->stream->eof()) {
fwrite($handle, $this->stream->read(4096));
}
fclose($handle);
}
$this->moved = true;
}
示例4: copy
/**
* Copy stream to another stream.
*
* @param StreamInterface $src Source stream.
* @param StreamInterface $dest Target stream.
*
* @return void
*/
public static function copy(StreamInterface $src, StreamInterface $dest)
{
if ($src->isSeekable()) {
$src->rewind();
}
while (!$src->eof()) {
$dest->write($src->read(4096));
}
}
示例5: moveTo
public function moveTo($targetPath)
{
if (!PHP_SAPI || PHP_SAPI == 'cli') {
$handle = fopen($targetPath, Stream::MODE_READ_REPLACE);
if ($handle === false) {
throw new \RuntimeException('Unable to write to: ' . $targetPath);
}
$this->stream->rewind();
while (!$this->stream->eof()) {
fwrite($handle, $this->stream->read(4096));
}
fclose($handle);
} else {
if (move_uploaded_file($this->filePath, $targetPath) === false) {
throw new \RuntimeException('Error moving uploaded file');
}
}
}
示例6: body
/**
* @param StreamInterface $stream
*/
protected function body($stream)
{
if ($stream instanceof Messages\Stream\Implementation) {
$stream->rewind();
$this->fpassthru($stream->resource());
} else {
$this->output((string) $stream);
}
}
示例7: fromStream
/**
* Parse a response from a stream.
*
* @param StreamInterface $stream
* @return ResponseInterface
* @throws InvalidArgumentException when the stream is not readable.
* @throws UnexpectedValueException when errors occur parsing the message.
*/
public static function fromStream(StreamInterface $stream)
{
if (!$stream->isReadable() || !$stream->isSeekable()) {
throw new InvalidArgumentException('Message stream must be both readable and seekable');
}
$stream->rewind();
list($version, $status, $reasonPhrase) = self::getStatusLine($stream);
list($headers, $body) = self::splitStream($stream);
return (new Response($body, $status, $headers))->withProtocolVersion($version)->withStatus((int) $status, $reasonPhrase);
}
示例8: testReadAndWrite
public function testReadAndWrite()
{
$this->assertEquals(0, $this->stream->getSize());
$this->stream->write("Hello World, And All Developers!");
$this->assertEquals(32, $this->stream->getSize());
// size
$this->assertEquals(32, $this->stream->tell());
// pointer
$this->stream->rewind();
$this->assertEquals(0, $this->stream->tell());
$this->assertFalse($this->stream->eof());
$this->assertEquals("Hell", $this->stream->read(4));
$this->assertEquals("o World, ", $this->stream->read(9));
$this->assertEquals("And All Developers!", $this->stream->getContents());
$this->assertTrue($this->stream->eof());
$this->stream->seek(12);
$this->assertEquals(6, $this->stream->write('Hum...'));
$this->assertEquals("ll Developers!", $this->stream->getContents());
$this->assertEquals("Hello World,Hum...ll Developers!", $this->stream->__toString());
}
示例9: fromStream
/**
* Deserialize a request stream to a request instance.
*
* @param StreamInterface $stream
* @return Request
* @throws UnexpectedValueException when errors occur parsing the message.
*/
public static function fromStream(StreamInterface $stream)
{
if (!$stream->isReadable() || !$stream->isSeekable()) {
throw new InvalidArgumentException('Message stream must be both readable and seekable');
}
$stream->rewind();
list($method, $requestTarget, $version) = self::getRequestLine($stream);
$uri = self::createUriFromRequestTarget($requestTarget);
list($headers, $body) = self::splitStream($stream);
return (new Request($uri, $method, $body, $headers))->withProtocolVersion($version)->withRequestTarget($requestTarget);
}
示例10: writeFile
/**
* Write internal stream to given path
*
* @param string $path
*/
protected function writeFile($path)
{
$handle = fopen($path, Stream::MODE_READ_WRITE_RESET);
if ($handle === false) {
throw new \RuntimeException('Unable to write to path: ' . $path);
}
$this->stream->rewind();
while (!$this->stream->eof()) {
fwrite($handle, $this->stream->read(4096));
}
fclose($handle);
}
示例11: writeFile
/**
* Write internal stream to given path
*
* @param string $path
*/
private function writeFile($path)
{
$handle = fopen($path, 'wb+');
if (false === $handle) {
throw new RuntimeException('Unable to write to designated path');
}
$this->stream->rewind();
while (!$this->stream->eof()) {
fwrite($handle, $this->stream->read(4096));
}
fclose($handle);
}
示例12: writeStream
/**
* Write the stream to the given path.
*
* @param StreamInterface $stream
* @param string $path
*/
protected static function writeStream(StreamInterface $stream, $path)
{
$dir = dirname($path);
if (!is_dir($dir)) {
mkdir($dir, 0777, true);
}
$handle = fopen($path, 'wb+');
if (false === $handle) {
throw new RuntimeException('Unable to write to designated path');
}
$stream->rewind();
while (!$stream->eof()) {
fwrite($handle, $stream->read(4096));
}
fclose($handle);
}
示例13: doCreateStream
/**
* Creates a stream.
*
* @param null|resource|string|\Psr\Http\Message\StreamInterface|null $body The body
*
* @return \Psr\Http\Message\StreamInterface The stream
*/
private function doCreateStream($body)
{
if ($body instanceof StreamInterface) {
$body->rewind();
return $body;
}
if (is_resource($body)) {
return $this->doCreateStream(new Stream($body));
}
$stream = new Stream('php://memory', 'rw');
if ($body === null) {
return $stream;
}
$stream->write((string) $body);
return $this->doCreateStream($stream);
}
示例14: writeResponse
/**
* Write a response to the connection as FCGI_STDOUT records.
*
* @param int $requestId The request id to write to
* @param string $headerData The header data to write (including terminating CRLFCRLF)
* @param StreamInterface $stream The stream to write
*/
private function writeResponse($requestId, $headerData, StreamInterface $stream)
{
$data = $headerData;
$eof = false;
$stream->rewind();
do {
$dataLength = strlen($data);
if ($dataLength < 65535 && !$eof && !($eof = $stream->eof())) {
$readLength = 65535 - $dataLength;
$data .= $stream->read($readLength);
$dataLength = strlen($data);
}
$writeSize = min($dataLength, 65535);
$writeData = substr($data, 0, $writeSize);
$data = substr($data, $writeSize);
$this->writeRecord($requestId, DaemonInterface::FCGI_STDOUT, $writeData);
} while ($writeSize === 65535);
$this->writeRecord($requestId, DaemonInterface::FCGI_STDOUT);
}
示例15: outputBody
private function outputBody(StreamInterface $body)
{
if ($this->chunkSize > 0) {
if ($body->isSeekable()) {
$body->rewind();
}
while (!$body->eof()) {
print $body->read($this->chunkSize);
}
} else {
print (string) $body;
}
}