11
11
12
12
namespace Http \Discovery \MessageFactory ;
13
13
14
+ use Http \Discovery \StreamFactory \DiactorosStreamFactory ;
14
15
use Http \Message \MessageFactory ;
15
16
use Psr \Http \Message \StreamInterface ;
16
17
use Zend \Diactoros \Request ;
17
18
use Zend \Diactoros \Response ;
18
- use Zend \Diactoros \Stream ;
19
19
20
20
/**
21
21
* @author GeLo <[email protected] >
22
22
*/
23
23
class DiactorosFactory implements MessageFactory
24
24
{
25
+ /**
26
+ * @var DiactorosStreamFactory
27
+ */
28
+ private $ streamFactory ;
29
+
30
+ public function __construct ()
31
+ {
32
+ $ this ->streamFactory = new DiactorosStreamFactory ();
33
+ }
34
+
25
35
/**
26
36
* {@inheritdoc}
27
37
*/
@@ -35,7 +45,7 @@ public function createRequest(
35
45
return (new Request (
36
46
$ uri ,
37
47
$ method ,
38
- $ this ->createStream ($ body ),
48
+ $ this ->streamFactory -> createStream ($ body ),
39
49
$ headers
40
50
))->withProtocolVersion ($ protocolVersion );
41
51
}
@@ -51,39 +61,9 @@ public function createResponse(
51
61
$ protocolVersion = '1.1 '
52
62
) {
53
63
return (new Response (
54
- $ this ->createStream ($ body ),
64
+ $ this ->streamFactory -> createStream ($ body ),
55
65
$ statusCode ,
56
66
$ headers
57
67
))->withProtocolVersion ($ protocolVersion );
58
68
}
59
-
60
- /**
61
- * Creates a stream
62
- *
63
- * @param string|resource|StreamInterface|null $body
64
- *
65
- * @return StreamInterface
66
- *
67
- * @throws \InvalidArgumentException If the stream body is invalid
68
- */
69
- protected function createStream ($ body = null )
70
- {
71
- if (!$ body instanceof StreamInterface) {
72
- if (is_resource ($ body )) {
73
- $ body = new Stream ($ body );
74
- } else {
75
- $ stream = new Stream ('php://memory ' , 'rw ' );
76
-
77
- if (isset ($ body )) {
78
- $ stream ->write ((string ) $ body );
79
- }
80
-
81
- $ body = $ stream ;
82
- }
83
- }
84
-
85
- $ body ->rewind ();
86
-
87
- return $ body ;
88
- }
89
69
}
0 commit comments