diff --git a/.travis.yml b/.travis.yml index 95980fc..1d51b60 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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: diff --git a/CHANGELOG.md b/CHANGELOG.md index ce2c410..99f4cce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/composer.json b/composer.json index 0efd08a..9dd876b 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/phpspec.ci.yml b/phpspec.ci.yml index f08e148..a4a288f 100644 --- a/phpspec.ci.yml +++ b/phpspec.ci.yml @@ -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 diff --git a/phpspec.yml.dist b/phpspec.yml.dist index 8d38a4e..56d3c89 100644 --- a/phpspec.yml.dist +++ b/phpspec.yml.dist @@ -5,3 +5,4 @@ suites: formatter.name: pretty extensions: - Coduo\PhpSpec\DataProvider\DataProviderExtension + - Akeneo\SkipExampleExtension diff --git a/spec/MessageFactory/SlimMessageFactorySpec.php b/spec/MessageFactory/SlimMessageFactorySpec.php new file mode 100644 index 0000000..e39452e --- /dev/null +++ b/spec/MessageFactory/SlimMessageFactorySpec.php @@ -0,0 +1,18 @@ +shouldHaveType('Http\Message\MessageFactory\SlimMessageFactory'); + } +} diff --git a/spec/StreamFactory/SlimStreamFactorySpec.php b/spec/StreamFactory/SlimStreamFactorySpec.php new file mode 100644 index 0000000..c3bfef3 --- /dev/null +++ b/spec/StreamFactory/SlimStreamFactorySpec.php @@ -0,0 +1,26 @@ +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'); + } +} diff --git a/spec/UriFactory/SlimUriFactorySpec.php b/spec/UriFactory/SlimUriFactorySpec.php new file mode 100644 index 0000000..b373f5f --- /dev/null +++ b/spec/UriFactory/SlimUriFactorySpec.php @@ -0,0 +1,27 @@ +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); + } +} diff --git a/src/MessageFactory/SlimMessageFactory.php b/src/MessageFactory/SlimMessageFactory.php new file mode 100644 index 0000000..cdad2ec --- /dev/null +++ b/src/MessageFactory/SlimMessageFactory.php @@ -0,0 +1,72 @@ + + */ +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); + } +} diff --git a/src/StreamFactory/SlimStreamFactory.php b/src/StreamFactory/SlimStreamFactory.php new file mode 100644 index 0000000..e779f3b --- /dev/null +++ b/src/StreamFactory/SlimStreamFactory.php @@ -0,0 +1,40 @@ + + */ +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; + } +} diff --git a/src/UriFactory/SlimUriFactory.php b/src/UriFactory/SlimUriFactory.php new file mode 100644 index 0000000..c013d54 --- /dev/null +++ b/src/UriFactory/SlimUriFactory.php @@ -0,0 +1,31 @@ + + */ +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'); + } +}