mirror of
https://github.com/shlinkio/shlink.git
synced 2024-11-29 01:18:59 +03:00
Migrated LocaleMiddleware to psr-15 middleware
This commit is contained in:
parent
d1018b6da7
commit
c3c03a3a3b
3 changed files with 54 additions and 38 deletions
|
@ -2,10 +2,11 @@
|
|||
namespace Shlinkio\Shlink\Common\Middleware;
|
||||
|
||||
use Acelaya\ZsmAnnotatedServices\Annotation\Inject;
|
||||
use Interop\Http\ServerMiddleware\DelegateInterface;
|
||||
use Interop\Http\ServerMiddleware\MiddlewareInterface;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Zend\I18n\Translator\Translator;
|
||||
use Zend\Stratigility\MiddlewareInterface;
|
||||
|
||||
class LocaleMiddleware implements MiddlewareInterface
|
||||
{
|
||||
|
@ -25,40 +26,26 @@ class LocaleMiddleware implements MiddlewareInterface
|
|||
$this->translator = $translator;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Process an incoming request and/or response.
|
||||
*
|
||||
* Accepts a server-side request and a response instance, and does
|
||||
* something with them.
|
||||
*
|
||||
* If the response is not complete and/or further processing would not
|
||||
* interfere with the work done in the middleware, or if the middleware
|
||||
* wants to delegate to another process, it can use the `$out` callable
|
||||
* if present.
|
||||
*
|
||||
* If the middleware does not return a value, execution of the current
|
||||
* request is considered complete, and the response instance provided will
|
||||
* be considered the response to return.
|
||||
*
|
||||
* Alternately, the middleware may return a response instance.
|
||||
*
|
||||
* Often, middleware will `return $out();`, with the assumption that a
|
||||
* later middleware will return a response.
|
||||
* Process an incoming server request and return a response, optionally delegating
|
||||
* to the next middleware component to create the response.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Response $response
|
||||
* @param null|callable $out
|
||||
* @return null|Response
|
||||
* @param DelegateInterface $delegate
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function __invoke(Request $request, Response $response, callable $out = null)
|
||||
public function process(Request $request, DelegateInterface $delegate)
|
||||
{
|
||||
if (! $request->hasHeader('Accept-Language')) {
|
||||
return $out($request, $response);
|
||||
return $delegate->process($request);
|
||||
}
|
||||
|
||||
$locale = $request->getHeaderLine('Accept-Language');
|
||||
$this->translator->setLocale($this->normalizeLocale($locale));
|
||||
return $out($request, $response);
|
||||
return $delegate->process($request);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -3,7 +3,7 @@ namespace ShlinkioTest\Shlink\Common\Middleware;
|
|||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Shlinkio\Shlink\Common\Middleware\LocaleMiddleware;
|
||||
use Zend\Diactoros\Response;
|
||||
use ShlinkioTest\Shlink\Common\Util\TestUtils;
|
||||
use Zend\Diactoros\ServerRequestFactory;
|
||||
use Zend\I18n\Translator\Translator;
|
||||
|
||||
|
@ -30,9 +30,7 @@ class LocaleMiddlewareTest extends TestCase
|
|||
public function whenNoHeaderIsPresentLocaleIsNotChanged()
|
||||
{
|
||||
$this->assertEquals('ru', $this->translator->getLocale());
|
||||
$this->middleware->__invoke(ServerRequestFactory::fromGlobals(), new Response(), function ($req, $resp) {
|
||||
return $resp;
|
||||
});
|
||||
$this->middleware->process(ServerRequestFactory::fromGlobals(), TestUtils::createDelegateMock()->reveal());
|
||||
$this->assertEquals('ru', $this->translator->getLocale());
|
||||
}
|
||||
|
||||
|
@ -43,9 +41,7 @@ class LocaleMiddlewareTest extends TestCase
|
|||
{
|
||||
$this->assertEquals('ru', $this->translator->getLocale());
|
||||
$request = ServerRequestFactory::fromGlobals()->withHeader('Accept-Language', 'es');
|
||||
$this->middleware->__invoke($request, new Response(), function ($req, $resp) {
|
||||
return $resp;
|
||||
});
|
||||
$this->middleware->process($request, TestUtils::createDelegateMock()->reveal());
|
||||
$this->assertEquals('es', $this->translator->getLocale());
|
||||
}
|
||||
|
||||
|
@ -54,18 +50,16 @@ class LocaleMiddlewareTest extends TestCase
|
|||
*/
|
||||
public function localeGetsNormalized()
|
||||
{
|
||||
$delegate = TestUtils::createDelegateMock();
|
||||
|
||||
$this->assertEquals('ru', $this->translator->getLocale());
|
||||
|
||||
$request = ServerRequestFactory::fromGlobals()->withHeader('Accept-Language', 'es_ES');
|
||||
$this->middleware->__invoke($request, new Response(), function ($req, $resp) {
|
||||
return $resp;
|
||||
});
|
||||
$this->middleware->process($request, $delegate->reveal());
|
||||
$this->assertEquals('es', $this->translator->getLocale());
|
||||
|
||||
$request = ServerRequestFactory::fromGlobals()->withHeader('Accept-Language', 'en-US');
|
||||
$this->middleware->__invoke($request, new Response(), function ($req, $resp) {
|
||||
return $resp;
|
||||
});
|
||||
$this->middleware->process($request, $delegate->reveal());
|
||||
$this->assertEquals('en', $this->translator->getLocale());
|
||||
}
|
||||
}
|
||||
|
|
35
module/Common/test/Util/TestUtils.php
Normal file
35
module/Common/test/Util/TestUtils.php
Normal file
|
@ -0,0 +1,35 @@
|
|||
<?php
|
||||
namespace ShlinkioTest\Shlink\Common\Util;
|
||||
|
||||
use Interop\Http\ServerMiddleware\DelegateInterface;
|
||||
use Prophecy\Argument;
|
||||
use Prophecy\Prophet;
|
||||
use Psr\Http\Message\RequestInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Zend\Diactoros\Response;
|
||||
|
||||
class TestUtils
|
||||
{
|
||||
private static $prophet;
|
||||
|
||||
public static function createDelegateMock(ResponseInterface $response = null, RequestInterface $request = null)
|
||||
{
|
||||
$argument = $request ?: Argument::any();
|
||||
$delegate = static::getProphet()->prophesize(DelegateInterface::class);
|
||||
$delegate->process($argument)->willReturn($response ?: new Response());
|
||||
|
||||
return $delegate;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Prophet
|
||||
*/
|
||||
private static function getProphet()
|
||||
{
|
||||
if (static::$prophet === null) {
|
||||
static::$prophet = new Prophet();
|
||||
}
|
||||
|
||||
return static::$prophet;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue