-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
22e4b36
commit 7e08871
Showing
7 changed files
with
154 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |