-
Notifications
You must be signed in to change notification settings - Fork 47
/
DiactorosStreamFactory.php
58 lines (50 loc) · 1.39 KB
/
DiactorosStreamFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
/*
* This file is part of the Http Discovery package.
*
* (c) PHP HTTP Team <team@php-http.org>
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code.
*/
namespace Http\Discovery\StreamFactory;
use Http\Message\StreamFactory;
use Psr\Http\Message\StreamInterface;
use Zend\Diactoros\Request;
use Zend\Diactoros\Response;
use Zend\Diactoros\Stream;
/**
* Zend Diactoros StreamFactory
*
* @author GeLo <geloen.eric@gmail.com>
* @author Михаил Красильников <m.krasilnikov@yandex.ru>
*/
class DiactorosStreamFactory implements StreamFactory
{
/**
* Creates a stream
*
* @param string|resource|StreamInterface|null $body
*
* @return StreamInterface
*
* @throws \InvalidArgumentException If the stream body is invalid
* @throws \RuntimeException If cannot write into stream
*/
public function createStream($body = null)
{
if (!$body instanceof StreamInterface) {
if (is_resource($body)) {
$body = new Stream($body);
} else {
$stream = new Stream('php://memory', 'rw');
if (null !== $body) {
$stream->write((string) $body);
}
$body = $stream;
}
}
$body->rewind();
return $body;
}
}