Skip to content

Commit 0b764a6

Browse files
committed
Add StreamFactoryDiscovery
Supports Guzzle and Zend Diactoros.
1 parent 81784c2 commit 0b764a6

File tree

6 files changed

+270
-0
lines changed

6 files changed

+270
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
}

spec/StreamFactoryDiscoverySpec.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
* Zend Diactoros StreamFactory
22+
*
23+
* @author GeLo <[email protected]>
24+
* @author Михаил Красильников <[email protected]>
25+
*/
26+
class DiactorosStreamFactory implements StreamFactory
27+
{
28+
/**
29+
* Creates a stream
30+
*
31+
* @param string|resource|StreamInterface|null $body
32+
*
33+
* @return StreamInterface
34+
*
35+
* @throws \InvalidArgumentException If the stream body is invalid
36+
* @throws \RuntimeException If cannot write into stream
37+
*/
38+
public function createStream($body = null)
39+
{
40+
if (!$body instanceof StreamInterface) {
41+
if (is_resource($body)) {
42+
$body = new Stream($body);
43+
} else {
44+
$stream = new Stream('php://memory', 'rw');
45+
46+
if (null !== $body) {
47+
$stream->write((string) $body);
48+
}
49+
50+
$body = $stream;
51+
}
52+
}
53+
54+
$body->rewind();
55+
56+
return $body;
57+
}
58+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
* Guzzle Stream Factory
19+
*
20+
* @author Михаил Красильников <[email protected]>
21+
*/
22+
class GuzzleStreamFactory implements StreamFactory
23+
{
24+
/**
25+
* Creates a stream
26+
*
27+
* @param string|resource|StreamInterface|null $body
28+
*
29+
* @return StreamInterface
30+
*
31+
* @throws \InvalidArgumentException if the $body arg is not valid.
32+
*/
33+
public function createStream($body = null)
34+
{
35+
return \GuzzleHttp\Psr7\stream_for($body);
36+
}
37+
}

src/StreamFactoryDiscovery.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
}

0 commit comments

Comments
 (0)