Skip to content

Add StreamFactoryDiscovery #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 11, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions spec/StreamFactory/DiactorosStreamFactorySpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace spec\Http\Discovery\StreamFactory;

use PhpSpec\Exception\Example\SkippingException;
use PhpSpec\ObjectBehavior;
use Zend\Diactoros\Stream;

class DiactorosStreamFactorySpec extends ObjectBehavior
{
function let()
{
if (!class_exists('Zend\Diactoros\Stream')) {
throw new SkippingException('Diactoros is not available');
}
}

function it_is_initializable()
{
$this->shouldHaveType('Http\Discovery\StreamFactory\DiactorosStreamFactory');
}

function it_is_a_stream_factory()
{
$this->shouldImplement('Http\Message\StreamFactory');
}

function it_creates_a_stream_from_string()
{
$this->createStream('foo')->shouldHaveType('Psr\Http\Message\StreamInterface');
}

function it_creates_a_stream_from_resource()
{
$this->createStream(fopen('php://memory', 'rw'))
->shouldHaveType('Psr\Http\Message\StreamInterface');
}

function it_creates_a_stream_from_stream()
{
$this->createStream(new Stream('php://memory'))
->shouldHaveType('Psr\Http\Message\StreamInterface');
}

function it_creates_a_stream_from_null()
{
$this->createStream(null)->shouldHaveType('Psr\Http\Message\StreamInterface');
}
}
49 changes: 49 additions & 0 deletions spec/StreamFactory/GuzzleStreamFactorySpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace spec\Http\Discovery\StreamFactory;

use GuzzleHttp\Psr7\Stream;
use PhpSpec\Exception\Example\SkippingException;
use PhpSpec\ObjectBehavior;

class GuzzleStreamFactorySpec extends ObjectBehavior
{
public function let()
{
if (!class_exists('GuzzleHttp\Psr7\Stream')) {
throw new SkippingException('GuzzleHttp is not available');
}
}

public function it_is_initializable()
{
$this->shouldHaveType('Http\Discovery\StreamFactory\GuzzleStreamFactory');
}

public function it_is_a_stream_factory()
{
$this->shouldImplement('Http\Message\StreamFactory');
}

public function it_creates_a_stream_from_string()
{
$this->createStream('foo')->shouldHaveType('Psr\Http\Message\StreamInterface');
}

public function it_creates_a_stream_from_resource()
{
$this->createStream(fopen('php://memory', 'rw'))
->shouldHaveType('Psr\Http\Message\StreamInterface');
}

public function it_creates_a_stream_from_stream()
{
$this->createStream(new Stream(fopen('php://memory', 'rw')))
->shouldHaveType('Psr\Http\Message\StreamInterface');
}

public function it_creates_a_stream_from_null()
{
$this->createStream(null)->shouldHaveType('Psr\Http\Message\StreamInterface');
}
}
23 changes: 23 additions & 0 deletions spec/StreamFactoryDiscoverySpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace spec\Http\Discovery;

use PhpSpec\ObjectBehavior;

class StreamFactoryDiscoverySpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->shouldHaveType('Http\Discovery\StreamFactoryDiscovery');
}

function it_is_a_class_discovery()
{
$this->shouldHaveType('Http\Discovery\ClassDiscovery');
}

function it_finds_an_http_stream_factory()
{
$this->find()->shouldHaveType('Http\Message\StreamFactory');
}
}
55 changes: 55 additions & 0 deletions src/StreamFactory/DiactorosStreamFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/*
* This file is part of the Http Discovery package.
*
* (c) PHP HTTP Team <[email protected]>
*
* 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;

/**
* @author Михаил Красильников <[email protected]>
*/
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;
}
}
35 changes: 35 additions & 0 deletions src/StreamFactory/GuzzleStreamFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/*
* This file is part of the Http Discovery package.
*
* (c) PHP HTTP Team <[email protected]>
*
* 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;

/**
* @author Михаил Красильников <[email protected]>
*/
class GuzzleStreamFactory implements StreamFactory
{
/**
* Creates a stream
*
* @param string|resource|StreamInterface|null $body
*
* @return StreamInterface
*
* @throws \InvalidArgumentException if the $body arg is not valid.
*/
public function createStream($body = null)
{
return \GuzzleHttp\Psr7\stream_for($body);
}
}
54 changes: 54 additions & 0 deletions src/StreamFactoryDiscovery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

/*
* This file is part of the Http Discovery package.
*
* (c) PHP HTTP Team <[email protected]>
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code.
*/

namespace Http\Discovery;

use Http\Message\StreamFactory;

/**
* Finds a Stream Factory
*
* @author Михаил Красильников <[email protected]>
*/
class StreamFactoryDiscovery extends ClassDiscovery
{
/**
* @var StreamFactory
*/
protected static $cache;

/**
* @var array
*/
protected static $classes = [
'guzzle' => [
'class' => 'Http\Discovery\StreamFactory\GuzzleStreamFactory',
'condition' => 'GuzzleHttp\Psr7\Stream',
],
'diactoros' => [
'class' => 'Http\Discovery\StreamFactory\DiactorosStreamFactory',
'condition' => 'Zend\Diactoros\Stream',
],
];

/**
* Finds a Stream Factory
*
* @return StreamFactory
*
* @throws NotFoundException
*/
public static function find()
{
// Override only used for return type declaration
return parent::find();
}
}