Skip to content

Commit

Permalink
added traits to avoid repetition
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed Aug 2, 2018
1 parent 22e4b36 commit 7e08871
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 1 deletion.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [2.1.0] - 2018-08-02

### Added

- New trait `HasResponseFactory` used by many middlewares that need to configure the PSR-17 response factory.
- New trait `HasStreamFactory` used by many middlewares that need to configure the PSR-17 stream factory.

## [2.0.0] - 2018-08-01

### Added
Expand Down Expand Up @@ -193,6 +200,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Stream factory


[2.1.0]: https://github.com/middlewares/utils/compare/v2.0.0...v2.1.0
[2.0.0]: https://github.com/middlewares/utils/compare/v1.2.0...v2.0.0
[1.2.0]: https://github.com/middlewares/utils/compare/v1.1.0...v1.2.0
[1.1.0]: https://github.com/middlewares/utils/compare/v1.0.0...v1.1.0
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ $exception = HttpErrorException::create(500, [
$context = $exception->getContext();
```

## Traits

Traits containing common utilities shared between many middlewares like the ability to customize PSR-17 factories to create PSR-7 objects.

* `HasResponseFactory`
* `HasStreamFactory`

---

Please see [CHANGELOG](CHANGELOG.md) for more information about recent changes and [CONTRIBUTING](CONTRIBUTING.md) for contributing details.
Expand Down
36 changes: 36 additions & 0 deletions src/Traits/HasResponseFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
declare(strict_types = 1);

namespace Middlewares\Utils\Traits;

use Middlewares\Utils\Factory;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;

/**
* Simple class to create instances of PSR-7 classes.
*/
trait HasResponseFactory
{
/**
* @var ResponseFactoryInterface
*/
private $responseFactory;

/**
* Set the response factory used.
*/
public function responseFactory(ResponseFactoryInterface $responseFactory): self
{
$this->responseFactory = $responseFactory;

return $this;
}

private function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface
{
$responseFactory = $this->responseFactory ?: Factory::getResponseFactory();

return $responseFactory->createResponse($code, $reasonPhrase);
}
}
50 changes: 50 additions & 0 deletions src/Traits/HasStreamFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
declare(strict_types = 1);

namespace Middlewares\Utils\Traits;

use Middlewares\Utils\Factory;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\StreamInterface;

/**
* Simple class to create instances of PSR-7 classes.
*/
trait HasStreamFactory
{
/**
* @var StreamFactoryInterface
*/
private $streamFactory;

/**
* Set the stream factory used.
*/
public function streamFactory(StreamFactoryInterface $streamFactory): self
{
$this->streamFactory = $streamFactory;

return $this;
}

private function createStream(string $content = ''): StreamInterface
{
$streamFactory = $this->streamFactory ?: Factory::getStreamFactory();

return $streamFactory->createStream($content);
}

private function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface
{
$streamFactory = $this->streamFactory ?: Factory::getStreamFactory();

return $streamFactory->createStreamFromFile($filename, $mode);
}

private function createStreamFromResource($resource): StreamInterface
{
$streamFactory = $this->streamFactory ?: Factory::getStreamFactory();

return $streamFactory->createStreamFromResource($resource);
}
}
25 changes: 25 additions & 0 deletions tests/Assets/MiddlewareWithTraits.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
declare(strict_types = 1);

namespace Middlewares\Tests\Assets;

use Middlewares\Utils\Traits\HasResponseFactory;
use Middlewares\Utils\Traits\HasStreamFactory;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;

class MiddlewareWithTraits implements MiddlewareInterface
{
use HasResponseFactory;
use HasStreamFactory;

/**
* Process a request and return a response.
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
return $this->createResponse()->withBody($this->createStream());
}
}
2 changes: 1 addition & 1 deletion tests/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public function strategiesDataProvider(): array
'response' => DiactorosFactory::class,
'stream' => SlimFactory::class,
'uri' => GuzzleFactory::class,
]
],
],
SlimFactory::class,
DiactorosFactory::class,
Expand Down
27 changes: 27 additions & 0 deletions tests/TraitsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
declare(strict_types = 1);

namespace Middlewares\Tests;

use GuzzleHttp\Psr7\Stream;
use Middlewares\Tests\Assets\MiddlewareWithTraits;
use Middlewares\Utils\Dispatcher;
use Middlewares\Utils\Factory\GuzzleFactory;
use Middlewares\Utils\Factory\SlimFactory;
use PHPUnit\Framework\TestCase;
use Slim\Http\Response;

class TraitsTest extends TestCase
{
public function testCreation()
{
$response = Dispatcher::run([
(new MiddlewareWithTraits())
->streamFactory(new GuzzleFactory())
->responseFactory(new SlimFactory()),
]);

$this->assertInstanceOf(Stream::class, $response->getBody());
$this->assertInstanceOf(Response::class, $response);
}
}

0 comments on commit 7e08871

Please sign in to comment.