-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from lcobucci/add-base-functionality
Add base functionality
- Loading branch information
Showing
9 changed files
with
737 additions
and
2 deletions.
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,10 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Lcobucci\ContentNegotiation; | ||
|
||
use RuntimeException; | ||
|
||
final class ContentCouldNotBeFormatted extends RuntimeException | ||
{ | ||
} |
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,113 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Lcobucci\ContentNegotiation; | ||
|
||
use Fig\Http\Message\StatusCodeInterface; | ||
use Middlewares\ContentType; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Message\StreamInterface; | ||
use Psr\Http\Server\MiddlewareInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
use Zend\Diactoros\Stream; | ||
use function strpos; | ||
use function substr; | ||
|
||
final class ContentTypeMiddleware implements MiddlewareInterface | ||
{ | ||
/** | ||
* @var MiddlewareInterface | ||
*/ | ||
private $negotiator; | ||
|
||
/** | ||
* @var callable | ||
*/ | ||
private $streamFactory; | ||
|
||
/** | ||
* @var Formatter[] | ||
*/ | ||
private $formatters; | ||
|
||
/** | ||
* @param Formatter[] $formatters | ||
*/ | ||
public function __construct( | ||
MiddlewareInterface $negotiator, | ||
array $formatters, | ||
callable $streamFactory | ||
) { | ||
$this->negotiator = $negotiator; | ||
$this->formatters = $formatters; | ||
$this->streamFactory = $streamFactory; | ||
} | ||
|
||
/** | ||
* @param mixed[] $formats | ||
* @param Formatter[] $formatters | ||
*/ | ||
public static function fromRecommendedSettings( | ||
array $formats, | ||
array $formatters, | ||
?callable $streamFactory = null | ||
): self { | ||
return new self( | ||
new ContentType($formats), | ||
$formatters, | ||
function () use ($streamFactory): StreamInterface { | ||
return $streamFactory !== null ? $streamFactory() : new Stream('php://temp', 'wb+'); | ||
} | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @throws ContentCouldNotBeFormatted | ||
*/ | ||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface | ||
{ | ||
$response = $this->negotiator->process($request, $handler); | ||
|
||
if (! $response instanceof UnformattedResponse) { | ||
return $response; | ||
} | ||
|
||
$contentType = $this->extractContentType($response->getHeaderLine('Content-Type')); | ||
$formatter = $this->formatters[$contentType] ?? null; | ||
|
||
return $this->formatResponse($response, $formatter); | ||
} | ||
|
||
private function extractContentType(string $contentType): string | ||
{ | ||
$charsetSeparatorPosition = strpos($contentType, ';'); | ||
|
||
if ($charsetSeparatorPosition === false) { | ||
return $contentType; | ||
} | ||
|
||
return substr($contentType, 0, $charsetSeparatorPosition); | ||
} | ||
|
||
/** | ||
* @throws ContentCouldNotBeFormatted | ||
*/ | ||
private function formatResponse(UnformattedResponse $response, ?Formatter $formatter): ResponseInterface | ||
{ | ||
/** @var StreamInterface $body */ | ||
$body = ($this->streamFactory)(); | ||
$response = $response->withBody($body); | ||
|
||
if ($formatter === null) { | ||
return $response->withStatus(StatusCodeInterface::STATUS_NOT_ACCEPTABLE); | ||
} | ||
|
||
$body->write($formatter->format($response->getUnformattedContent())); | ||
$body->rewind(); | ||
|
||
return $response; | ||
} | ||
} |
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,14 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Lcobucci\ContentNegotiation; | ||
|
||
interface Formatter | ||
{ | ||
/** | ||
* @param mixed $content | ||
* | ||
* @throw ContentCouldNotBeFormatted | ||
*/ | ||
public function format($content): string; | ||
} |
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,167 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Lcobucci\ContentNegotiation; | ||
|
||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\StreamInterface; | ||
|
||
final class UnformattedResponse implements ResponseInterface | ||
{ | ||
/** | ||
* @var ResponseInterface | ||
*/ | ||
private $decoratedResponse; | ||
|
||
/** | ||
* @var mixed | ||
*/ | ||
private $unformattedContent; | ||
|
||
/** | ||
* @param mixed $unformattedContent | ||
*/ | ||
public function __construct(ResponseInterface $decoratedResponse, $unformattedContent) | ||
{ | ||
$this->decoratedResponse = $decoratedResponse; | ||
$this->unformattedContent = $unformattedContent; | ||
} | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
public function getUnformattedContent() | ||
{ | ||
return $this->unformattedContent; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getProtocolVersion() | ||
{ | ||
return $this->decoratedResponse->getProtocolVersion(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function withProtocolVersion($version) | ||
{ | ||
return new self( | ||
$this->decoratedResponse->withProtocolVersion($version), | ||
$this->unformattedContent | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getHeaders() | ||
{ | ||
return $this->decoratedResponse->getHeaders(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function hasHeader($name) | ||
{ | ||
return $this->decoratedResponse->hasHeader($name); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getHeader($name) | ||
{ | ||
return $this->decoratedResponse->getHeader($name); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getHeaderLine($name) | ||
{ | ||
return $this->decoratedResponse->getHeaderLine($name); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function withHeader($name, $value) | ||
{ | ||
return new self( | ||
$this->decoratedResponse->withHeader($name, $value), | ||
$this->unformattedContent | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function withAddedHeader($name, $value) | ||
{ | ||
return new self( | ||
$this->decoratedResponse->withAddedHeader($name, $value), | ||
$this->unformattedContent | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function withoutHeader($name) | ||
{ | ||
return new self( | ||
$this->decoratedResponse->withoutHeader($name), | ||
$this->unformattedContent | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getBody() | ||
{ | ||
return $this->decoratedResponse->getBody(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function withBody(StreamInterface $body) | ||
{ | ||
return new self( | ||
$this->decoratedResponse->withBody($body), | ||
$this->unformattedContent | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getStatusCode() | ||
{ | ||
return $this->decoratedResponse->getStatusCode(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function withStatus($code, $reasonPhrase = '') | ||
{ | ||
return new self( | ||
$this->decoratedResponse->withStatus($code, $reasonPhrase), | ||
$this->unformattedContent | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getReasonPhrase() | ||
{ | ||
return $this->decoratedResponse->getReasonPhrase(); | ||
} | ||
} |
Oops, something went wrong.