File tree Expand file tree Collapse file tree 6 files changed +265
-0
lines changed
Expand file tree Collapse file tree 6 files changed +265
-0
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace spec \Http \Discovery \StreamFactory ;
4+
5+ use PhpSpec \Exception \Example \SkippingException ;
6+ use PhpSpec \ObjectBehavior ;
7+ use Zend \Diactoros \Stream ;
8+
9+ class DiactorosStreamFactorySpec extends ObjectBehavior
10+ {
11+ function let ()
12+ {
13+ if (!class_exists ('Zend\Diactoros\Stream ' )) {
14+ throw new SkippingException ('Diactoros is not available ' );
15+ }
16+ }
17+
18+ function it_is_initializable ()
19+ {
20+ $ this ->shouldHaveType ('Http\Discovery\StreamFactory\DiactorosStreamFactory ' );
21+ }
22+
23+ function it_is_a_stream_factory ()
24+ {
25+ $ this ->shouldImplement ('Http\Message\StreamFactory ' );
26+ }
27+
28+ function it_creates_a_stream_from_string ()
29+ {
30+ $ this ->createStream ('foo ' )->shouldHaveType ('Psr\Http\Message\StreamInterface ' );
31+ }
32+
33+ function it_creates_a_stream_from_resource ()
34+ {
35+ $ this ->createStream (fopen ('php://memory ' , 'rw ' ))
36+ ->shouldHaveType ('Psr\Http\Message\StreamInterface ' );
37+ }
38+
39+ function it_creates_a_stream_from_stream ()
40+ {
41+ $ this ->createStream (new Stream ('php://memory ' ))
42+ ->shouldHaveType ('Psr\Http\Message\StreamInterface ' );
43+ }
44+
45+ function it_creates_a_stream_from_null ()
46+ {
47+ $ this ->createStream (null )->shouldHaveType ('Psr\Http\Message\StreamInterface ' );
48+ }
49+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace spec \Http \Discovery \StreamFactory ;
4+
5+ use GuzzleHttp \Psr7 \Stream ;
6+ use PhpSpec \Exception \Example \SkippingException ;
7+ use PhpSpec \ObjectBehavior ;
8+
9+ class GuzzleStreamFactorySpec extends ObjectBehavior
10+ {
11+ public function let ()
12+ {
13+ if (!class_exists ('GuzzleHttp\Psr7\Stream ' )) {
14+ throw new SkippingException ('GuzzleHttp is not available ' );
15+ }
16+ }
17+
18+ public function it_is_initializable ()
19+ {
20+ $ this ->shouldHaveType ('Http\Discovery\StreamFactory\GuzzleStreamFactory ' );
21+ }
22+
23+ public function it_is_a_stream_factory ()
24+ {
25+ $ this ->shouldImplement ('Http\Message\StreamFactory ' );
26+ }
27+
28+ public function it_creates_a_stream_from_string ()
29+ {
30+ $ this ->createStream ('foo ' )->shouldHaveType ('Psr\Http\Message\StreamInterface ' );
31+ }
32+
33+ public function it_creates_a_stream_from_resource ()
34+ {
35+ $ this ->createStream (fopen ('php://memory ' , 'rw ' ))
36+ ->shouldHaveType ('Psr\Http\Message\StreamInterface ' );
37+ }
38+
39+ public function it_creates_a_stream_from_stream ()
40+ {
41+ $ this ->createStream (new Stream (fopen ('php://memory ' , 'rw ' )))
42+ ->shouldHaveType ('Psr\Http\Message\StreamInterface ' );
43+ }
44+
45+ public function it_creates_a_stream_from_null ()
46+ {
47+ $ this ->createStream (null )->shouldHaveType ('Psr\Http\Message\StreamInterface ' );
48+ }
49+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace spec \Http \Discovery ;
4+
5+ use PhpSpec \ObjectBehavior ;
6+
7+ class StreamFactoryDiscoverySpec extends ObjectBehavior
8+ {
9+ function it_is_initializable ()
10+ {
11+ $ this ->shouldHaveType ('Http\Discovery\StreamFactoryDiscovery ' );
12+ }
13+
14+ function it_is_a_class_discovery ()
15+ {
16+ $ this ->shouldHaveType ('Http\Discovery\ClassDiscovery ' );
17+ }
18+
19+ function it_finds_an_http_stream_factory ()
20+ {
21+ $ this ->find ()->shouldHaveType ('Http\Message\StreamFactory ' );
22+ }
23+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ /*
4+ * This file is part of the Http Discovery package.
5+ *
6+ * (c) PHP HTTP Team <[email protected] > 7+ *
8+ * For the full copyright and license information, please read the LICENSE
9+ * file that was distributed with this source code.
10+ */
11+
12+ namespace Http \Discovery \StreamFactory ;
13+
14+ use Http \Message \StreamFactory ;
15+ use Psr \Http \Message \StreamInterface ;
16+ use Zend \Diactoros \Request ;
17+ use Zend \Diactoros \Response ;
18+ use Zend \Diactoros \Stream ;
19+
20+ /**
21+ * @author Михаил Красильников <[email protected] > 22+ */
23+ class DiactorosStreamFactory implements StreamFactory
24+ {
25+ /**
26+ * Creates a stream
27+ *
28+ * @param string|resource|StreamInterface|null $body
29+ *
30+ * @return StreamInterface
31+ *
32+ * @throws \InvalidArgumentException If the stream body is invalid
33+ * @throws \RuntimeException If cannot write into stream
34+ */
35+ public function createStream ($ body = null )
36+ {
37+ if (!$ body instanceof StreamInterface) {
38+ if (is_resource ($ body )) {
39+ $ body = new Stream ($ body );
40+ } else {
41+ $ stream = new Stream ('php://memory ' , 'rw ' );
42+
43+ if (null !== $ body ) {
44+ $ stream ->write ((string ) $ body );
45+ }
46+
47+ $ body = $ stream ;
48+ }
49+ }
50+
51+ $ body ->rewind ();
52+
53+ return $ body ;
54+ }
55+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ /*
4+ * This file is part of the Http Discovery package.
5+ *
6+ * (c) PHP HTTP Team <[email protected] > 7+ *
8+ * For the full copyright and license information, please read the LICENSE
9+ * file that was distributed with this source code.
10+ */
11+
12+ namespace Http \Discovery \StreamFactory ;
13+
14+ use Http \Message \StreamFactory ;
15+ use Psr \Http \Message \StreamInterface ;
16+
17+ /**
18+ * @author Михаил Красильников <[email protected] > 19+ */
20+ class GuzzleStreamFactory implements StreamFactory
21+ {
22+ /**
23+ * Creates a stream
24+ *
25+ * @param string|resource|StreamInterface|null $body
26+ *
27+ * @return StreamInterface
28+ *
29+ * @throws \InvalidArgumentException if the $body arg is not valid.
30+ */
31+ public function createStream ($ body = null )
32+ {
33+ return \GuzzleHttp \Psr7 \stream_for ($ body );
34+ }
35+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ /*
4+ * This file is part of the Http Discovery package.
5+ *
6+ * (c) PHP HTTP Team <[email protected] > 7+ *
8+ * For the full copyright and license information, please read the LICENSE
9+ * file that was distributed with this source code.
10+ */
11+
12+ namespace Http \Discovery ;
13+
14+ use Http \Message \StreamFactory ;
15+
16+ /**
17+ * Finds a Stream Factory
18+ *
19+ * @author Михаил Красильников <[email protected] > 20+ */
21+ class StreamFactoryDiscovery extends ClassDiscovery
22+ {
23+ /**
24+ * @var StreamFactory
25+ */
26+ protected static $ cache ;
27+
28+ /**
29+ * @var array
30+ */
31+ protected static $ classes = [
32+ 'guzzle ' => [
33+ 'class ' => 'Http\Discovery\StreamFactory\GuzzleStreamFactory ' ,
34+ 'condition ' => 'GuzzleHttp\Psr7\Stream ' ,
35+ ],
36+ 'diactoros ' => [
37+ 'class ' => 'Http\Discovery\StreamFactory\DiactorosStreamFactory ' ,
38+ 'condition ' => 'Zend\Diactoros\Stream ' ,
39+ ],
40+ ];
41+
42+ /**
43+ * Finds a Stream Factory
44+ *
45+ * @return StreamFactory
46+ *
47+ * @throws NotFoundException
48+ */
49+ public static function find ()
50+ {
51+ // Override only used for return type declaration
52+ return parent ::find ();
53+ }
54+ }
You can’t perform that action at this time.
0 commit comments