@@ -22,16 +22,15 @@ abstract class AbstractPipes implements PipesInterface
22
22
public $ pipes = array ();
23
23
24
24
/** @var string */
25
- protected $ inputBuffer = '' ;
26
- /** @var resource|null */
27
- protected $ input ;
28
-
25
+ private $ inputBuffer = '' ;
26
+ /** @var resource|\Iterator|null */
27
+ private $ input ;
29
28
/** @var bool */
30
29
private $ blocked = true ;
31
30
32
31
public function __construct ($ input )
33
32
{
34
- if (is_resource ($ input )) {
33
+ if (is_resource ($ input ) || $ input instanceof \Iterator ) {
35
34
$ this ->input = $ input ;
36
35
} elseif (is_string ($ input )) {
37
36
$ this ->inputBuffer = $ input ;
@@ -76,7 +75,7 @@ protected function unblock()
76
75
foreach ($ this ->pipes as $ pipe ) {
77
76
stream_set_blocking ($ pipe , 0 );
78
77
}
79
- if (null !== $ this ->input ) {
78
+ if (is_resource ( $ this ->input ) ) {
80
79
stream_set_blocking ($ this ->input , 0 );
81
80
}
82
81
@@ -91,9 +90,21 @@ protected function write()
91
90
if (!isset ($ this ->pipes [0 ])) {
92
91
return ;
93
92
}
93
+ $ input = $ this ->input ;
94
+
95
+ if ($ input instanceof \Iterator) {
96
+ if (!$ input ->valid ()) {
97
+ $ input = null ;
98
+ } elseif (is_resource ($ input = $ input ->current ())) {
99
+ stream_set_blocking ($ input , 0 );
100
+ } else {
101
+ $ this ->inputBuffer .= $ input ;
102
+ $ this ->input ->next ();
103
+ $ input = null ;
104
+ }
105
+ }
94
106
95
- $ e = array ();
96
- $ r = null !== $ this ->input ? array ($ this ->input ) : $ e ;
107
+ $ r = $ e = array ();
97
108
$ w = array ($ this ->pipes [0 ]);
98
109
99
110
// let's have a look if something changed in streams
@@ -109,8 +120,7 @@ protected function write()
109
120
return array ($ this ->pipes [0 ]);
110
121
}
111
122
}
112
-
113
- foreach ($ r as $ input ) {
123
+ if ($ input ) {
114
124
for (;;) {
115
125
$ data = fread ($ input , self ::CHUNK_SIZE );
116
126
if (!isset ($ data [0 ])) {
@@ -124,16 +134,19 @@ protected function write()
124
134
return array ($ this ->pipes [0 ]);
125
135
}
126
136
}
127
- if (!isset ($ data [0 ]) && feof ($ input )) {
128
- // no more data to read on input resource
129
- // use an empty buffer in the next reads
130
- $ this ->input = null ;
137
+ if (feof ($ input )) {
138
+ if ($ this ->input instanceof \Iterator) {
139
+ $ this ->input ->next ();
140
+ } else {
141
+ $ this ->input = null ;
142
+ }
131
143
}
132
144
}
133
145
}
134
146
135
147
// no input to read on resource, buffer is empty
136
- if (null === $ this ->input && !isset ($ this ->inputBuffer [0 ])) {
148
+ if (!isset ($ this ->inputBuffer [0 ]) && !($ this ->input instanceof \Iterator ? $ this ->input ->valid () : $ this ->input )) {
149
+ $ this ->input = null ;
137
150
fclose ($ this ->pipes [0 ]);
138
151
unset($ this ->pipes [0 ]);
139
152
}
0 commit comments