Skip to content

Add message, stream and URI factories for Slim Framework #53

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 8 commits into from
Oct 14, 2016
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
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ matrix:

before_install:
- if [[ $COVERAGE != true ]]; then phpenv config-rm xdebug.ini || true; fi
- if [[ "${TRAVIS_PHP_VERSION}" == "5.4" ]]; then composer remove slim/slim --dev --no-update; fi
- travis_retry composer self-update

install:
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Change Log

## Unreleased

### Added

- Message, stream and URI factories for [Slim Framework](https://github.com/slimphp/Slim)

## 1.3.1 - 2016-07-15

Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@
"ext-zlib": "*",
"phpspec/phpspec": "^2.4",
"henrikbjorn/phpspec-code-coverage" : "^1.0",
"coduo/phpspec-data-provider-extension": "^1.0"
"coduo/phpspec-data-provider-extension": "^1.0",
"akeneo/phpspec-skip-example-extension": "^1.0",
"slim/slim": "^3.5"
},
"suggest": {
"zendframework/zend-diactoros": "Used with Diactoros Factories",
"guzzlehttp/psr7": "Used with Guzzle PSR-7 Factories",
"slim/slim": "Used with Slim Framework PSR-7 implementation",
"ext-zlib": "Used with compressor/decompressor streams"
},
"autoload": {
Expand Down
1 change: 1 addition & 0 deletions phpspec.ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ formatter.name: pretty
extensions:
- PhpSpec\Extension\CodeCoverageExtension
- Coduo\PhpSpec\DataProvider\DataProviderExtension
- Akeneo\SkipExampleExtension
code_coverage:
format: clover
output: build/coverage.xml
1 change: 1 addition & 0 deletions phpspec.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ suites:
formatter.name: pretty
extensions:
- Coduo\PhpSpec\DataProvider\DataProviderExtension
- Akeneo\SkipExampleExtension
18 changes: 18 additions & 0 deletions spec/MessageFactory/SlimMessageFactorySpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace spec\Http\Message\MessageFactory;

use PhpSpec\ObjectBehavior;

/**
* @require Slim\Http\Request
*/
class SlimMessageFactorySpec extends ObjectBehavior
{
use MessageFactoryBehavior;

function it_is_initializable()
{
$this->shouldHaveType('Http\Message\MessageFactory\SlimMessageFactory');
}
}
26 changes: 26 additions & 0 deletions spec/StreamFactory/SlimStreamFactorySpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace spec\Http\Message\StreamFactory;

use Slim\Http\Stream;
use PhpSpec\ObjectBehavior;

/**
* @require Slim\Http\Stream
*/
class SlimStreamFactorySpec extends ObjectBehavior
{
use StreamFactoryBehavior;

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

function it_creates_a_stream_from_stream()
{
$resource = fopen('php://memory', 'rw');
$this->createStream(new Stream($resource))
->shouldHaveType('Psr\Http\Message\StreamInterface');
}
}
27 changes: 27 additions & 0 deletions spec/UriFactory/SlimUriFactorySpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace spec\Http\Message\UriFactory;

use Psr\Http\Message\UriInterface;
use PhpSpec\ObjectBehavior;

/**
* @require Slim\Http\Uri
*/
class SlimUriFactorySpec extends ObjectBehavior
{
use UriFactoryBehavior;

function it_is_initializable()
{
$this->shouldHaveType('Http\Message\UriFactory\SlimUriFactory');
}

/**
* TODO: Remove this when https://github.com/phpspec/phpspec/issues/825 is resolved
*/
function it_creates_a_uri_from_uri(UriInterface $uri)
{
$this->createUri($uri)->shouldReturn($uri);
}
}
72 changes: 72 additions & 0 deletions src/MessageFactory/SlimMessageFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace Http\Message\MessageFactory;

use Http\Message\StreamFactory\SlimStreamFactory;
use Http\Message\UriFactory\SlimUriFactory;
use Http\Message\MessageFactory;
use Slim\Http\Request;
use Slim\Http\Response;
use Slim\Http\Headers;

/**
* Creates Slim 3 messages.
*
* @author Mika Tuupola <[email protected]>
*/
final class SlimMessageFactory implements MessageFactory
{
/**
* @var SlimStreamFactory
*/
private $streamFactory;

/**
* @var SlimUriFactory
*/
private $uriFactory;

public function __construct()
{
$this->streamFactory = new SlimStreamFactory();
$this->uriFactory = new SlimUriFactory();
}

/**
* {@inheritdoc}
*/
public function createRequest(
$method,
$uri,
array $headers = [],
$body = null,
$protocolVersion = '1.1'
) {
return (new Request(
$method,
$this->uriFactory->createUri($uri),
new Headers($headers),
[],
[],
$this->streamFactory->createStream($body),
[]
))->withProtocolVersion($protocolVersion);
}

/**
* {@inheritdoc}
*/
public function createResponse(
$statusCode = 200,
$reasonPhrase = null,
array $headers = [],
$body = null,
$protocolVersion = '1.1'
) {
return (new Response(
$statusCode,
new Headers($headers),
$this->streamFactory->createStream($body)
))->withProtocolVersion($protocolVersion);
}
}
40 changes: 40 additions & 0 deletions src/StreamFactory/SlimStreamFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Http\Message\StreamFactory;

use Http\Message\StreamFactory;
use Psr\Http\Message\StreamInterface;
use Slim\Http\Stream;

/**
* Creates Slim 3 streams.
*
* @author Mika Tuupola <[email protected]>
*/
final class SlimStreamFactory implements StreamFactory
{
/**
* {@inheritdoc}
*/
public function createStream($body = null)
{
if ($body instanceof StreamInterface) {
return $body;
}

if (is_resource($body)) {
$stream = new Stream($body);
} else {
$resource = fopen('php://memory', 'r+');
$stream = new Stream($resource);

if (null !== $body) {
$stream->write((string) $body);
}
}

$stream->rewind();

return $stream;
}
}
31 changes: 31 additions & 0 deletions src/UriFactory/SlimUriFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Http\Message\UriFactory;

use Http\Message\UriFactory;
use Psr\Http\Message\UriInterface;
use Slim\Http\Uri;

/**
* Creates Slim 3 URI.
*
* @author Mika Tuupola <[email protected]>
*/
final class SlimUriFactory implements UriFactory
{
/**
* {@inheritdoc}
*/
public function createUri($uri)
{
if ($uri instanceof UriInterface) {
return $uri;
}

if (is_string($uri)) {
return Uri::createFromString($uri);
}

throw new \InvalidArgumentException('URI must be a string or UriInterface');
}
}