-
Notifications
You must be signed in to change notification settings - Fork 3
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 #9 from chubbyphp/middleware
Middleware
- Loading branch information
Showing
24 changed files
with
915 additions
and
18 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,39 @@ | ||
# AcceptLanguageMiddlewareFactory | ||
|
||
## without name (default) | ||
|
||
```php | ||
<?php | ||
|
||
use Chubbyphp\Negotiation\AcceptLanguageMiddleware; | ||
use Chubbyphp\Negotiation\ServiceFactory\AcceptLanguageMiddlewareFactory; | ||
use Psr\Container\ContainerInterface; | ||
|
||
/** @var ContainerInterface $container */ | ||
$container = ...; | ||
|
||
// $container->get(AcceptLanguageNegotiator::class) | ||
|
||
$factory = new AcceptLanguageMiddlewareFactory(); | ||
|
||
$acceptLanguageMiddleware = $factory($container); | ||
``` | ||
|
||
## with name `default` | ||
|
||
```php | ||
<?php | ||
|
||
use Chubbyphp\Negotiation\AcceptLanguageMiddleware; | ||
use Chubbyphp\Negotiation\ServiceFactory\AcceptLanguageMiddlewareFactory; | ||
use Psr\Container\ContainerInterface; | ||
|
||
/** @var ContainerInterface $container */ | ||
$container = ...; | ||
|
||
// $container->get(AcceptLanguageNegotiator::class.'default') | ||
|
||
$factory = [AcceptLanguageMiddlewareFactory::class, 'default']; | ||
|
||
$acceptLanguageMiddleware = $factory($container); | ||
``` |
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,39 @@ | ||
# AcceptMiddlewareFactory | ||
|
||
## without name (default) | ||
|
||
```php | ||
<?php | ||
|
||
use Chubbyphp\Negotiation\AcceptMiddleware; | ||
use Chubbyphp\Negotiation\ServiceFactory\AcceptMiddlewareFactory; | ||
use Psr\Container\ContainerInterface; | ||
|
||
/** @var ContainerInterface $container */ | ||
$container = ...; | ||
|
||
// $container->get(AcceptNegotiator::class) | ||
|
||
$factory = new AcceptMiddlewareFactory(); | ||
|
||
$acceptMiddleware = $factory($container); | ||
``` | ||
|
||
## with name `default` | ||
|
||
```php | ||
<?php | ||
|
||
use Chubbyphp\Negotiation\AcceptMiddleware; | ||
use Chubbyphp\Negotiation\ServiceFactory\AcceptMiddlewareFactory; | ||
use Psr\Container\ContainerInterface; | ||
|
||
/** @var ContainerInterface $container */ | ||
$container = ...; | ||
|
||
// $container->get(AcceptNegotiator::class.'default') | ||
|
||
$factory = [AcceptMiddlewareFactory::class, 'default']; | ||
|
||
$acceptMiddleware = $factory($container); | ||
``` |
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,39 @@ | ||
# ContentTypeMiddlewareFactory | ||
|
||
## without name (default) | ||
|
||
```php | ||
<?php | ||
|
||
use Chubbyphp\Negotiation\ContentTypeMiddleware; | ||
use Chubbyphp\Negotiation\ServiceFactory\ContentTypeMiddlewareFactory; | ||
use Psr\Container\ContainerInterface; | ||
|
||
/** @var ContainerInterface $container */ | ||
$container = ...; | ||
|
||
// $container->get(ContentTypeNegotiator::class) | ||
|
||
$factory = new ContentTypeMiddlewareFactory(); | ||
|
||
$contentTypeMiddleware = $factory($container); | ||
``` | ||
|
||
## with name `default` | ||
|
||
```php | ||
<?php | ||
|
||
use Chubbyphp\Negotiation\ContentTypeMiddleware; | ||
use Chubbyphp\Negotiation\ServiceFactory\ContentTypeMiddlewareFactory; | ||
use Psr\Container\ContainerInterface; | ||
|
||
/** @var ContainerInterface $container */ | ||
$container = ...; | ||
|
||
// $container->get(ContentTypeNegotiator::class.'default') | ||
|
||
$factory = [ContentTypeMiddlewareFactory::class, 'default']; | ||
|
||
$contentTypeMiddleware = $factory($container); | ||
``` |
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,55 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Chubbyphp\Negotiation\Middleware; | ||
|
||
use Chubbyphp\HttpException\HttpException; | ||
use Chubbyphp\Negotiation\AcceptLanguageNegotiatorInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\MiddlewareInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
|
||
final class AcceptLanguageMiddleware implements MiddlewareInterface | ||
{ | ||
public function __construct( | ||
private AcceptLanguageNegotiatorInterface $acceptLanguageNegotiator, | ||
) {} | ||
|
||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface | ||
{ | ||
if (null === $acceptLanguage = $this->acceptLanguageNegotiator->negotiate($request)) { | ||
throw HttpException::createNotAcceptable( | ||
$this->aggregateData( | ||
'acceptLanguage', | ||
$request->getHeaderLine('Accept-Language'), | ||
$this->acceptLanguageNegotiator->getSupportedLocales() | ||
) | ||
); | ||
} | ||
|
||
$request = $request->withAttribute('acceptLanguage', $acceptLanguage->getValue()); | ||
|
||
return $handler->handle($request); | ||
} | ||
|
||
/** | ||
* @param array<string> $supportedValues | ||
* | ||
* @return array<string, array<string>|string> | ||
*/ | ||
private function aggregateData(string $header, string $value, array $supportedValues): array | ||
{ | ||
return [ | ||
'detail' => sprintf( | ||
'%s %s, supportedValues: "%s"', | ||
'' !== $value ? 'Not supported' : 'Missing', | ||
$header, | ||
implode('", ', $supportedValues) | ||
), | ||
'value' => $value, | ||
'supportedValues' => $supportedValues, | ||
]; | ||
} | ||
} |
Oops, something went wrong.